pythontwiliotwilio-python

Put <Gather> result in variable in twilio.rest


So, i have this code:

import os
from twilio.rest import Client

xml=f'''
<Response>
    <Say language="ru-RU">Здравствуйте, пожалуйста введите код для подтверждения.</Say>
</Response>'''
account_sid = ('AC274461ad47988c753424a3c8735dbcc1')
auth_token =('8ac88e8d5bce419ae3b5cbac4fc255f9')
client = Client(account_sid, auth_token)

call = client.calls.create(                                       twiml=xml,
                        to='+375336412273',
                        from_='+12318247004',
                    )

print(call.sid)

I want to put in xml, that way, so i could put result of (what user typed in) in variable. I want to do it only with python and twilio.rest, on twilio site i only found how to do it with flask, url and twiml.


Solution

  • Twilio developer evangelist here.

    In order to be able to run interactive phone calls, there needs to be a way for your application to give instructions to Twilio, receive responses and then give further instructions. The instructions are the TwiML that you send to Twilio, but the way that Twilio communicates things back to you, like the result of what a user typed during a <Gather>, is via webhooks. Webhooks are HTTP requests from Twilio that include data about the call, like user input, in the body of the request.

    To use that data and provide Twilio with further TwiML instructions your application needs to be able to receive HTTP requests and respond with an HTTP response containing the TwiML.

    There are example in the Twilio documentation using Flask because Flask is a good way to receive and respond to HTTP requests in Python. You could build an application without a framework like Flask, but you would still need to be able to receive an incoming HTTP request and respond to it.