0

I'm attempting to add pauses and DTMF tones (similar to Dial an extension using DTMF tones after a pause / Twilio Studio) via the REST API. I'm using the python helper libraries. I've already attempted the same solution as was suggested above but am getting an error and I'm unsure why.

This works [successfully runs a Studio flow] but does not provide a way to add pauses or tones (as far as I know):

from twilio.rest import Client
import configuration as configuration

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

execution = client.studio \
                  .flows(configuration.flow_sid) \
                  .executions \
                  .create(to=configuration.test_number, from_=configuration.twlio_number)

print(execution.sid)

This is giving me a 401 error in the logs (interestingly when I used the basic same concept with Autopilot I got no errors...however client wants a human voice to be played not a robot voice reading text and Autopilot doesn't have that option as of yet). It does indeed place the call but immediately states ~ 'Sorry an application error occurred'.

from twilio.rest import Client
import configuration as configuration
#This version calls a known number just to test it.  Called via CLI.

account_sid = configuration.account_sid
auth_token = configuration.auth_token

client = Client(account_sid, auth_token)
call = client.calls.create(
                        record=True,
                        send_digits= configuration.insurance_digits,
                        url='https://studio.twilio.com/v1/Flows/'+configuration.flow_sid+'/Executions',
                        to= configuration.test_number,
                        from_= configuration.twlio_number
                    )

print(call.sid)

I'm open to solutions - either a way to add tones/pauses to the execution option (first block) or a way to have the call option (second block) actually work. Truth be told getting the second option working is my preference.

deroses
  • 163
  • 2
  • 9

1 Answers1

0

This is how I ended up doing it - not sure if there is a better way. I used the client.studio (first block of code) since I knew it at least worked.

  1. Recording - this is simple - just set Record Call = true on the widget. There is no need to mess with passing a parameter as part of the incoming REST request.
  2. Sending digits. Slightly more involved.

A. First I passed a parameter of digits as part of the create statement.

    execution = client.studio \
                  .flows(configuration.flow_sid) \
                  .executions \
                  .create(parameters={'digits':configuration.insurance_digits},to=configuration.test_number, from_=configuration.twlio_number)

B. Then I added a function widget after the outgoing call was placed. This widget gets a function parameter where key is digit and value = {{trigger.message.digits}}. Note that there is also a new widget required right after this function widget that gets skipped (see https://support.twilio.com/hc/en-us/articles/360019580493).

C. Finally there is a simple function that receives the digits and creates TwiML and uses the passed variable to set the correct tones. digits=event.digits gives me what I need to say, and then it's just TwiML from there. The end of the function needs to redirect back to your flow, so something like twiml.redirect('https://webhooks.twilio.com/v1/Accounts/ACxxxxxxxxxxxxx/Flows/FWxxxxxxxxxxxxxxxxxx?FlowEvent=audioComplete');

If there is an easier way let me know, but this allows me to pass DTMF tones as a variable, along with my phone number variables.

deroses
  • 163
  • 2
  • 9