python-3.xgoogle-cloud-storagetwilioflask-restfultwilio-twiml

TwiML™ Voice: <Play> not playing google cloud storage url


I am trying to use Twilio Voice to play an mp3 file. The mp3 file is uploaded to GCS bucket and the url of the mp3 file is in the below given format:

https://storage.googleapis.com/project_name/folder_name/file_name.mp3

Below is the code snippet in python flask which is invoked using an hhtp call:

@app.route('/hello', methods=['GET', 'POST'])
def welcome():
    name = 'messi'
    url = GCSAgent().search_mp3_file(name)
    print(f'Logging url:{url}')
    response = VoiceResponse()
    response.play(url)
    print(response)
    return '200'

The value of print(response) is below:

<?xml version="1.0" encoding="UTF-8"?><Response><Play>https://storage.googleapis.com/project_name/folder_name/file_name.mp3</Play></Response>

Below is the property of the mp3 file: enter image description here

When i am picking up the call there is no mp3 file which is played but when I use the Say/Play widget and I give the above url, the mp3 is getting played. What is the issue with Twilio or there is something which I am doing wrong?


Solution

  • I was able to solve the issue, below is the code snippet:

    @app.route('/hello', methods=['GET', 'POST'])
    def welcome():
        name = 'messi'
        url = GCSAgent().search_mp3_file(name)
        print(f'Logging url:{url}')
        response = VoiceResponse()
        response.play(url)
        print(response)
        return str(response)
    

    The only change we need to do is return the response as string instead of returning the status code.

    So in Twilio what happens is, when you call the endpoint, the TwiML is returned which is then get processed. See the screen shot below for one of the call flows:

    Twilio Call Flow

    What Twilio does is, it gets the TwiML and then performs a GET request for the mp3 URL which is mentioned in the TwiML and then plays it but this will not happen if we return something apart from the response.