(request)
Handles tropo call requests
Handles tropo call requests
Handles tropo call requests
def ivr_in(request):
    """
    Handles tropo call requests
    """
    from tropo import Tropo
    if request.method == "POST":
        data = json.loads(request.body.decode('utf-8'))
        phone_number = data["session"]["from"]["id"]

        if phone_number:
            cleaned_number = strip_plus(phone_number)
            v = PhoneNumber.by_extensive_search(cleaned_number)
        else:
            v = None

        # Save the call entry
        msg = Call(
            phone_number=cleaned_number,
            direction=INCOMING,
            date=datetime.utcnow(),
            backend_api=SQLTropoBackend.get_api_id(),
        )
        if v is not None:
            msg.domain = v.domain
            msg.couch_recipient_doc_type = v.owner_doc_type
            msg.couch_recipient = v.owner_id
        msg.save()

        t = Tropo()
        t.reject()
        return HttpResponse(t.RenderJson())
    else:
        return HttpResponseBadRequest("Bad Request")
def ivr_in ( request ) : from tropo import Tropo if request . method == "POST" : data = json . loads ( request . body . decode ( 'utf-8' ) ) phone_number = data [ "session" ] [ "from" ] [ "id" ] if phone_number : cleaned_number = strip_plus ( phone_number ) v = PhoneNumber . by_extensive_search ( cleaned_number ) else : v = None # Save the call entry msg = Call ( phone_number = cleaned_number , direction = INCOMING , date = datetime . utcnow ( ) , backend_api = SQLTropoBackend . get_api_id ( ) , ) if v is not None : msg . domain = v . domain msg . couch_recipient_doc_type = v . owner_doc_type msg . couch_recipient = v . owner_id msg . save ( ) t = Tropo ( ) t . reject ( ) return HttpResponse ( t . RenderJson ( ) ) else : return HttpResponseBadRequest ( "Bad Request" )