Exemplo n.º 1
0
def inbound():
    body = json.loads(request.data)
    event = body.get("data").get("event_type")

    try:
        if event == "call.initiated":
            call = telnyx.Call(connection_id=os.getenv("TELNYX_CONNECTION_ID"))
            call.call_control_id = body.get("data").get("payload").get(
                "call_control_id")
            call.answer()

        elif event == "call.answered":
            call = telnyx.Call(connection_id=os.getenv("TELNYX_CONNECTION_ID"))
            call.call_control_id = body.get("data").get("payload").get(
                "call_control_id")
            call.speak(
                payload=
                "Hello, Telnyx user! Welcome to this call control demonstration",
                language="en-US",
                voice="female")
        elif event == "call.speak.ended":
            call = telnyx.Call(connection_id=os.getenv("TELNYX_CONNECTION_ID"))
            call.call_control_id = body.get("data").get("payload").get(
                "call_control_id")
            call.hangup()
    except:
        return json.dumps({"success": False}), 500, {
            "ContentType": "application/json"
        }
    return json.dumps({"success": True}), 200, {
        "ContentType": "application/json"
    }
    async def startup_handler(app):
        # Save dependencies in the HTTP app.

        # Mock the telnyx SDK
        telnyx = Mock()
        telnyx.Call().return_value = Mock()

        app[constants.TELNYX] = telnyx
Exemplo n.º 3
0
 def test_can_call_transcription_stop(self, request_mock):
     resource = telnyx.Call()
     resource.call_control_id = CALL_CONTROL_ID
     resource.transcription_stop()
     request_mock.assert_requested(
         "post",
         "/v2/calls/%s/actions/transcription_stop" % CALL_CONTROL_ID)
     assert isinstance(resource, telnyx.Call)
Exemplo n.º 4
0
def inbound_call():
    # store some id values JUST IN CASE for troubleshooting purposes
    body = json.loads(request.data)
    calling_number = body["data"]["payload"]["from"]
    called_number = body["data"]["payload"]["to"]
    payload = call_control_id = body["data"]["payload"]
    call_control_id = body["data"]["payload"]["call_control_id"]
    call_session_id = body["data"]["payload"]["call_session_id"]
    call_leg_id = body["data"]["payload"]["call_leg_id"]
    event_type = body["data"]["event_type"]
    webhook_url = urlunsplit(
        (request.scheme, request.host, "/call-control/outbound", "", ""))

    # construct call object, which is needed for initial call control commands
    call = telnyx.Call()
    call.call_control_id = call_control_id

    # main logic response based on inbound webhook events
    try:
        if event_type == "call.initiated":
            call = telnyx.Call(connection_id=os.getenv("TELNYX_CONNECTION_ID"))
            call.call_control_id = body.get("data").get("payload").get(
                "call_control_id")
            call.answer()
            print(calling_number)
            print(called_number)
        elif event_type == "call.answered":
            handle_call_answered(call, called_number)
        elif event_type == "call.hangup":
            print(body)
            cnam_info = telnyx_cnam_lookup(calling_number)
            # time difference
            end_time = ''.join(body.get("data").get("payload").get("end_time"))
            start_time = ''.join(
                body.get("data").get("payload").get("start_time"))
            duration, date = difference(start_time, end_time)
            forward_number = db_number_forward_fetch(called_number)
            db_call_insert(cnam_info, calling_number, called_number,
                           forward_number, date, duration)

    except Exception as e:
        print("Error processing webhook")
        print(e)
    return Response(status=200)
Exemplo n.º 5
0
def respond():
    #Check record_type of object
    data = request.json['data']
    call_control_id = data.get('payload').get('call_control_id')
    call = telnyx.Call()
    call.call_control_id = call_control_id
    direction = data.get('payload').get('direction')
    transcription_url = f'https://api.telnyx.com/v2/calls/{call.call_control_id}/actions/transcription_start'
    headers2 = {
        'Content-Type': 'application/json',
        'Accept': 'application/json;charset=utf-8',
        'Authorization': f'Bearer {telnyx.api_key}',
    }
    data2 = '{"language":"en"}'
    if data.get('event_type') == 'call.initiated' and direction == 'incoming':
        print("Incoming call")
        client_state_incoming = str(
            base64.b64encode(direction.encode('ascii')), "utf-8")
        res = call.answer(client_state=client_state_incoming)
        print(res)
    if data.get('event_type') == 'call.answered':
        call.speak(payload="Please leave a voicemail now.",
                   language="en-US",
                   voice="female")
    if data.get('event_type') == 'call.speak.ended':
        response2 = requests.post(transcription_url,
                                  headers=headers2,
                                  data=data2)
        start_recording = call.record_start(format="mp3", channels="single")
        print(start_recording)
    if data.get('event_type') == 'transcription':
        transcript = data.get('payload').get('transcription_data').get(
            'transcript')
        with open("transcript.txt", "a") as file_object:
            # Append transcript to the same file
            file_object.write(transcript + "\n")
        print(transcript)
    if data.get('event_type') == 'call.recording.saved':
        recording_url = data.get('payload').get('recording_urls').get('mp3')
        recording_name = data.get('payload').get('recording_id')
        urllib.request.urlretrieve(recording_url, recording_name + ".mp3")
    print(data)
    return Response(status=200)