oncall-engine/engine/apps/twilioapp/gather.py
Innokentii Konstantinov 306f842963
TwilioPhoneProvider optimizations (#2034)
# What this PR does
This PR does three improvements in twilio_phone_provider:
1.
[Speed-up](https://github.com/grafana/oncall/pull/2034/files#diff-7a311767169c024e60e2b4e35fd531dd6e2f1ea785cfc84263e11e7932d622af)
query which calculates amount of phone_calls/sms left.
2. Remove code which was needed only for backward compatibility during
the release of PhoneProvider refactoring and improves logging for
handling status/gather updates.
3. Add db_index to twilio_sid. We are doing lot of lookups by sid and
with increasing amount of data it became resource consuming.
2023-06-08 05:13:54 +00:00

89 lines
2.9 KiB
Python

import logging
from django.urls import reverse
from twilio.twiml.voice_response import Gather, VoiceResponse
from apps.alerts.constants import ActionSource
from apps.twilioapp.models import TwilioPhoneCall
from common.api_helpers.utils import create_engine_url
logger = logging.getLogger(__name__)
def process_gather_data(call_sid: str, digit: str) -> VoiceResponse:
"""
The function processes pressed digit at call time
Args:
call_sid (str):
digit (str): user pressed digit
Returns:
response (VoiceResponse)
"""
response = VoiceResponse()
if digit in ["1", "2", "3"]:
# Success case
response.say(f"You have pressed digit {digit}")
process_digit(call_sid, digit)
else:
# Error wrong digit pressing
gather = Gather(action=get_gather_url(), method="POST", num_digits=1)
response.say("Wrong digit")
gather.say(get_gather_message())
response.append(gather)
return response
def process_digit(call_sid, digit):
"""
The function get Phone Call instance according to call_sid
and run process of pressed digit
Args:
call_sid (str):
digit (str):
Returns:
"""
if call_sid and digit:
logger.info(f"twilioapp.process_digit: processing sid={call_sid} digit={digit}")
twilio_phone_call = TwilioPhoneCall.objects.filter(sid=call_sid).first()
if twilio_phone_call is None:
logger.info(f"twilioapp.process_digit: twilio_phone_call not found sid={call_sid}")
return
logger.info(f"twilioapp.process_digit: found twilio_phone_call sid={call_sid} digit={digit}")
phone_call_record = twilio_phone_call.phone_call_record
if phone_call_record is None:
logger.info(f"twilioapp.process_digit: twilio_phone_call has no phone_call_record sid={call_sid}")
return
logger.info(f"twilioapp.process_digit: found phone_call_record id={phone_call_record.id} sid={call_sid}")
alert_group = phone_call_record.represents_alert_group
user = phone_call_record.receiver
logger.info(
f"twilioapp.process_digit: processing digit phone_call_record id={phone_call_record.id} "
f"twilio_phone_call_sid={call_sid} digit={digit} alert_group_id={alert_group.id} user_id={user.id}"
)
if digit == "1":
alert_group.acknowledge_by_user(user, action_source=ActionSource.PHONE)
elif digit == "2":
alert_group.resolve_by_user(user, action_source=ActionSource.PHONE)
elif digit == "3":
alert_group.silence_by_user(user, silence_delay=1800, action_source=ActionSource.PHONE)
def get_gather_url():
return create_engine_url(reverse("twilioapp:gather"))
def get_gather_message():
return "Press 1 to acknowledge, 2 to resolve, 3 to silence to 30 minutes"