예제 #1
0
def menu(request: request.HttpRequest):
    try:
        _ = request.META['HTTP_X_TWILIO_SIGNATURE']
        digit = request.POST.get('Digits')
        from_number = request.POST.get("From").strip("+")
        call_sid = request.POST.get("CallSid")

        channel_layer = channels.layers.get_channel_layer()
        resp = VoiceResponse()
        #record
        if digit == '1':
            resp.say("Please leave a message. Press the pound or hash key to end the recording.")
            #without an action or recording_status_callback attribute then you will have an endless loop of calling into the view
            resp.record(play_beep=True, max_length=30, finish_on_key="#", recording_status_callback="/api/record/", action="/api/hangup/")

            async_to_sync(channel_layer.group_send)(f"chat_{from_number}", {"type": "chat_message", "message": 'Incoming recording...'}) 
        #stream
        elif digit == '2':
            caller: Caller = Caller.objects.get(number=from_number)
            Call(sid=call_sid, length_of_call=0, caller_id=caller.id, call_type="L").save()
            redisController.set(key=call_sid,value=from_number)

            async_to_sync(channel_layer.group_send)(f"chat_{from_number}", {"type": "chat_message", "stream": True, "message": 'Incoming stream...'})

            resp.say("Please begin speaking...")
            connect = Connect()
            connect.stream(url=ws_url)
            resp.append(connect)
        else:
            resp.say("Incorrect entry. Please try again.")
            resp.redirect('/api/voice/')
        
        return HttpResponse(str(resp))
    except KeyError:
        return
예제 #2
0
from twilio.twiml.voice_response import Connect, VoiceResponse, Stream

response = VoiceResponse()
connect = Connect()
connect.stream(url='wss://mystream.ngrok.io/audiostream')
response.append(connect)

print(response)