Esempio n. 1
0
    def test_webhook_lookup(self):
        self.assertEqual(SQLTelerivetBackend.by_webhook_secret('abc'),
                         self.backend1)

        self.assertEqual(SQLTelerivetBackend.by_webhook_secret('def'),
                         self.backend2)

        self.assertIsNone(SQLTelerivetBackend.by_webhook_secret('ghi'))
    def test_webhook_lookup(self):
        self.assertEqual(
            SQLTelerivetBackend.by_webhook_secret('abc'),
            self.backend1
        )

        self.assertEqual(
            SQLTelerivetBackend.by_webhook_secret('def'),
            self.backend2
        )

        self.assertIsNone(SQLTelerivetBackend.by_webhook_secret('ghi'))
Esempio n. 3
0
def message_status(request, message_id):
    status = request.POST.get('status')
    logger.info(
        f'Updating Telerivet message status: message_id={message_id}, status={status}'
    )

    request_secret = request.POST.get('secret', '')
    backend = None
    if request_secret:
        backend = SQLTelerivetBackend.by_webhook_secret(request_secret)

    if backend is None:
        return HttpResponseForbidden('Invalid secret')

    try:
        sms = SMS.objects.get(couch_id=message_id)
    except SMS.DoesNotExist:
        raise Http404()
    else:
        process_message_status(
            sms,
            status,
            error_message=request.POST.get('error_message', ''),
        )
        return HttpResponse()
Esempio n. 4
0
def process_incoming_message(*args, **kwargs):
    try:
        from corehq.messaging.smsbackends.telerivet.views import TELERIVET_INBOUND_FIELD_MAP
        fields = {a: kwargs[a] for (a, b) in TELERIVET_INBOUND_FIELD_MAP}
        log = IncomingRequest(**fields)
        log.save()
    except Exception as e:
        notify_exception(None, "Could not save Telerivet log entry")
        pass

    backend = SQLTelerivetBackend.by_webhook_secret(kwargs["secret"])
    if backend is None:
        # Ignore the message if the webhook secret is not recognized
        return

    if kwargs["from_number_e164"]:
        from_number = strip_plus(kwargs["from_number_e164"])
    else:
        from_number = strip_plus(kwargs["from_number"])

    if kwargs["event"] == EVENT_INCOMING:
        if kwargs["message_type"] == MESSAGE_TYPE_SMS:
            domain_scope = backend.domain if not backend.is_global else None
            incoming_sms(from_number, kwargs["content"], SQLTelerivetBackend.get_api_id(),
                domain_scope=domain_scope, backend_id=backend.couch_id)
        elif kwargs["message_type"] == MESSAGE_TYPE_CALL:
            log_call(from_number, "TELERIVET-%s" % kwargs["message_id"], backend=backend)
Esempio n. 5
0
def process_incoming_message(*args, **kwargs):
    try:
        from corehq.messaging.smsbackends.telerivet.views import TELERIVET_INBOUND_FIELD_MAP
        fields = {a: kwargs[a] for (a, b) in TELERIVET_INBOUND_FIELD_MAP}
        log = IncomingRequest(**fields)
        log.save()
    except Exception as e:
        notify_exception(None, "Could not save Telerivet log entry")
        pass

    backend = SQLTelerivetBackend.by_webhook_secret(kwargs["secret"])
    if backend is None:
        # Ignore the message if the webhook secret is not recognized
        return

    if kwargs["from_number_e164"]:
        from_number = strip_plus(kwargs["from_number_e164"])
    else:
        from_number = strip_plus(kwargs["from_number"])

    if kwargs["event"] == EVENT_INCOMING:
        if kwargs["message_type"] == MESSAGE_TYPE_SMS:
            domain_scope = backend.domain if not backend.is_global else None
            incoming_sms(from_number,
                         kwargs["content"],
                         SQLTelerivetBackend.get_api_id(),
                         domain_scope=domain_scope,
                         backend_id=backend.couch_id)
        elif kwargs["message_type"] == MESSAGE_TYPE_CALL:
            incoming_ivr(from_number, "TELERIVET-%s" % kwargs["message_id"],
                         None)
Esempio n. 6
0
 def clean_webhook_secret(self):
     # Circular import
     from corehq.messaging.smsbackends.telerivet.models import SQLTelerivetBackend
     value = self.cleaned_data['webhook_secret']
     backend = SQLTelerivetBackend.by_webhook_secret(value)
     if backend and backend.pk != self._cchq_backend_id:
         raise ValidationError(_("Already in use."))
     return value
Esempio n. 7
0
 def clean_webhook_secret(self):
     # Circular import
     from corehq.messaging.smsbackends.telerivet.models import SQLTelerivetBackend
     value = self.cleaned_data['webhook_secret']
     backend = SQLTelerivetBackend.by_webhook_secret(value)
     if backend and backend.pk != self._cchq_backend_id:
         raise ValidationError(_("Already in use."))
     return value