1

The goal I've been trying to reach is:

  1. User calls our Twilio number
  2. Twilio connects to the WebSocket we're hosting
  3. We can 'listen' to live audio stream & send messages in 'real-time'

From what I've read online, we need first an endpoint that returns the TWiML. I put this on AWS lambda in a simple function that seems fine

def hello(event, context):
    voice_response = VoiceResponse()
    start = Start()
    connect = Connect()
    voice_response.say("Hi this is Charles, one moment while I start the stream")
    # NOTE: Remember to change this each time as needed if local/dev
    local_stream_url = "ws://2.tcp.ngrok.io:15337"
    connect.stream(url=local_stream_url)
    start.append(connect)
    voice_response.append(start)
    # Theoretically need to give it time to handle connection etc.
    voice_response.pause(length=10)

    response = {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'text/xml'
        },
        'body': str(voice_response)
    }

    return response

Then I have a very simple web Socket server to start with

#!/usr/bin/env python

import asyncio
import websockets

async def handler(websocket):
    while True:
        message = await websocket.recv()
        print(message)


async def main():
    async with websockets.serve(handler, "", 3000):
        await asyncio.Future()  # run forever

if __name__ == "__main__":
    asyncio.run(main())

I expose it to the world with ngrok

Announcing ngrok-go: embed ngrok into your Go apps as a net.Listener: https://ngrok.com/golang                                                                                    
                                                                                                                                                                                  
Session Status                online                                                                                                                                              
Account                       <me> (Plan: Personal)                                                                                                                       
Version                       3.2.1                                                                                                                                               
Region                        United States (us)                                                                                                                                  
Latency                       22ms                                                                                                                                                
Web Interface                 http://127.0.0.1:4040                                                                                                                               
Forwarding                    tcp://2.tcp.ngrok.io:15337 -> localhost:3000                                                                                                        

The latest error i got from Twilio is "Error - 11100" which doesnt make sense to me because they're docs say possible solutions are "Possible Solutions Make sure you submit a fully qualified URL including: protocol (http:// or https://) " But this should be a ws / wss URL, right?

Please let me know what I should look at or change. Happy to share more info.

https://www.twilio.com/docs/voice/twiml/stream https://www.twilio.com/docs/voice/twiml/stream#bi-directional-media-streams

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.