oncall-engine/engine/apps/twilioapp/views.py
Innokentii Konstantinov 1f786e8d2a
Phone provider refactoring (#1713)
# What this PR does
This PR moves phone notification logic into separate object PhoneBackend
and introduces PhoneProvider interface to hide actual implementation of
external phone services provider. It should allow add new phone
providers just by implementing one class (See SimplePhoneProvider for
example).
# Why 
[Asterisk PR](https://github.com/grafana/oncall/pull/1282) showed that
our phone notification system is not flexible. However this is one of
the most frequent community questions - how to add "X" phone provider.
Also, this refactoring move us one step closer to unifying all
notification backends, since with PhoneBackend all phone notification
logic is collected in one place and independent from concrete
realisation.
# Highligts
1. PhoneBackend object - contains all phone notifications business
logic.
2. PhoneProvider - interface to  external phone services provider.
3. TwilioPhoneProvider and SimplePhoneProvider - two examples of
PhoneProvider implementation.
4. PhoneCallRecord and SMSRecord models. I introduced these models to
keep phone notification limits logic decoupled from external providers.
Existing TwilioPhoneCall and TwilioSMS objects will be migrated to the
new table to not to reset limits counter. To be able to receive status
callbacks and gather from Twilio TwilioPhoneCall and TwilioSMS still
exists, but they are linked to PhoneCallRecord and SMSRecord via fk, to
not to leat twilio logic into core code.

---------

Co-authored-by: Yulia Shanyrova <yulia.shanyrova@grafana.com>
2023-05-24 06:27:48 +00:00

75 lines
2.6 KiB
Python

import logging
from django.http import HttpResponse
from rest_framework import status
from rest_framework.permissions import BasePermission
from rest_framework.response import Response
from rest_framework.views import APIView
from twilio.request_validator import RequestValidator
from apps.base.utils import live_settings
from common.api_helpers.utils import create_engine_url
from .gather import process_gather_data
from .status_callback import update_twilio_call_status, update_twilio_sms_status
logger = logging.getLogger(__name__)
class AllowOnlyTwilio(BasePermission):
def has_permission(self, request, view):
# https://www.twilio.com/docs/usage/tutorials/how-to-secure-your-django-project-by-validating-incoming-twilio-requests
# https://www.django-rest-framework.org/api-guide/permissions/
if live_settings.TWILIO_AUTH_TOKEN:
validator = RequestValidator(live_settings.TWILIO_AUTH_TOKEN)
location = create_engine_url(request.get_full_path())
request_valid = validator.validate(
request.build_absolute_uri(location=location),
request.POST,
request.META.get("HTTP_X_TWILIO_SIGNATURE", ""),
)
return request_valid
else:
return live_settings.TWILIO_ACCOUNT_SID == request.data["AccountSid"]
class HealthCheckView(APIView):
def get(self, request):
return Response("OK")
class GatherView(APIView):
permission_classes = [AllowOnlyTwilio]
def post(self, request):
call_sid = request.POST.get("CallSid")
digit = request.POST.get("Digits")
response = process_gather_data(call_sid, digit)
return HttpResponse(str(response), content_type="application/xml; charset=utf-8")
# Receive SMS Status Update from Twilio
class SMSStatusCallback(APIView):
permission_classes = [AllowOnlyTwilio]
def post(self, request):
message_sid = request.POST.get("MessageSid")
message_status = request.POST.get("MessageStatus")
update_twilio_sms_status(message_sid=message_sid, message_status=message_status)
return Response(data="", status=status.HTTP_204_NO_CONTENT)
# Receive Call Status Update from Twilio
class CallStatusCallback(APIView):
permission_classes = [AllowOnlyTwilio]
def post(self, request):
call_sid = request.POST.get("CallSid")
call_status = request.POST.get("CallStatus")
logging.info(f"CallStatusCallback: SID: {call_sid}, Status: {call_status}")
update_twilio_call_status(call_sid=call_sid, call_status=call_status)
return Response(data="", status=status.HTTP_204_NO_CONTENT)