Search code examples
twiliotwilio-api

I created a REST Api flow to make outbound calls but I can't run it with python


So i created a REST Api flow on stwilio studio the thing is that i dont know how to run it to start making calls.

I have a python script to make calls and leave a message. Do I have to change this script to run it with the flow? how am i supposed to make it work. I cant find any documentation on this.

The idea is to call a list of contacts and play a pre-recorded audio if they press 1 the call will be forwarded to our office, if they say something the app will record a voicemail.

This is the python script to make calls:

from twilio.rest import Client
import keys

client = Client(keys.account_sid, keys.auth_token)

call = client.calls.create(
    url="https://handler.twilio.com/twiml/EH498d38b38ad2faa90dfec86ec1fd43d0",
    to=keys.target_number,
    from_=keys.twilio_number
)

print(call.sid)

This is the REST Api flow I created


Solution

  • You code triggers a normal call with TwiML instructions. Are you planning to trigger a Studio flow execution from Python code? If so, this code will help you:

    # Download the helper library from https://www.twilio.com/docs/python/install
    import os
    from twilio.rest import Client
    
    
    # Find your Account SID and Auth Token at twilio.com/console
    # and set the environment variables. See http://twil.io/secure
    account_sid = os.environ['TWILIO_ACCOUNT_SID']
    auth_token = os.environ['TWILIO_AUTH_TOKEN']
    client = Client(account_sid, auth_token)
    
    execution = client.studio \
                      .v2 \
                      .flows('FWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
                      .executions \
                      .create(to='+15558675310', from_='+15017122661')
    
    print(execution.sid)
    

    Docs