Artie Meyer
Artie Meyer

Reputation: 13

Twilio - Play music during long process

Twilio newbie here.

I have a Twilio voice application that collects a bunch of data (international topup sales) - and there is a point where the actual process of purchasing the topup takes place.

This process can last anywhere from 10 to 30 seconds, where most of them are about 15 seconds. Sounds to me like I need to use the Twilio <ENQUEUE> tag (https://www.twilio.com/docs/voice/twiml/enqueue), but it does not work.

I am simply calling it like this (happens to be ColdFusion):

<Enqueue 
  waitUrl="processtopup.cfm" 
  method="POST" 
  action="topupdone.cfm">processTopup</Enqueue>

Within the processtopup.cfm file is the <PLAY> tag (which won't work because that is the page that takes more than 15 seconds.

Sorry - but I'm just confused on ho this should work. Thanks in advance!

Upvotes: 1

Views: 1105

Answers (1)

Alex Baban
Alex Baban

Reputation: 11732

Here is a possible solution. I've tested this and it works.

The main idea is to play some message/music in a loop until ColdFusion does the job, then, when ColdFusion is done, instruct the call to execute a different Twilio XML by making a POST request to Twilio's API call resource.

When a call comes in, and Twilio hits your endpoint, capture the call id, it will be used to switch the call to a different XML. The call id it's passed as FORM.CALLSID or URL.CALLSID depending on your webhook configuration at Twilio.

The call id looks something like CAdecbfa7e8e2a9d09336abcd57044cf74.

Pass the call id trough your flow (as url parameter should be fine) so it reaches processtopup.cfm.

Move the long running code from processtopup.cfm to let's say processtopup-action.cfm


Code in processtopup.cfm should now return immediately XML for playing loop (or you can play some .mp3), I'm showing with a message:

<cfoutput><?xml version="1.0" encoding="UTF-8"?>

<Response>
    <Say loop="0">Please wait while we process your request...</Say>
</Response>

</cfoutput>

<cfhttp 
    url="http://www.yourwebsite.com/processtopup-action.cfm?callsid=#FORM.CALLSID#" 
    method="get" 
    timeout="1" />

The code for processtopup-action.cfm

<!--- // place your long running code here --->

<cfset accountSid = '{your account sid}' />
<cfset authToken = '{your auth token}' />

<cfhttp 
    url="https://api.twilio.com/2010-04-01/Accounts/#variables.accountSid#/Calls/#URL.CALLSID#.json" 
    method="POST" 
    username="#variables.accountSid#" 
    password="#variables.authToken#"
    result="http_result">

    <cfhttpparam 
        name="Url" 
        value="http://www.yourwebsite.com/finish.cfm" 
        type="formfield" />

</cfhttp>

Code for finish.cfm

<cfoutput><?xml version="1.0" encoding="UTF-8"?>

<Response>
    <Say>This is the data you're looking for.</Say>
    <Say>Thank you. Goodbye!</Say>
    <Hangup />
</Response>

</cfoutput>

Of course, you can pass other parameters as you need.


Again, the main idea is that processtopup-action.cfm, after executing your long running code, makes a POST request to Twilio's API and instructs the call to switch to execute new TwiML located at http://www.yourwebsite.com/finish.cfm


Docs:

Upvotes: 2

Related Questions