twilio / twilio-python

A Python module for communicating with the Twilio API and generating TwiML.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not able to stream audio back to Twilio in a phone call

CourageousJoe opened this issue · comments

Issue Summary

I'm trying to find a way to stream audio back to Twilio via websockets.
I'm referring this documentation: https://www.twilio.com/docs/voice/twiml/stream#websocket-messages-to-twilio

Steps to Reproduce

  1. Make a phone call using Twilio API and initiate callback as below:
    response = VoiceResponse()

    start = Start()
    start.stream(url=f'wss://some-local-wss-url/twilio')
    response.append(start)
    response.say('Hello there!')
    response.pause(length=60)
    return str(response), 200, {'Content-Type': 'text/xml'}

Code Snippet

async def twilio_sender(twilio_ws):
    print('twilio_sender started')
    streamsid = await streamsid_queue.get()
    while True:
        chunk = 'xyz' # a base64 encoded string of 8000/mulaw
        message = {
            'event': 'media',
            'streamSid': streamsid,
            'media': {
                'payload': base64.b64encode(chunk).decode()
            }
        }
        res = await twilio_ws.send(json.dumps(message))

Exception/Log

No exceptions. The value of the res above is None.

# paste exception/log here

Technical details:

  • twilio-python version: 8.8.0
  • python version: 3.10.4

I faced the same issue, how did you fix it?