In Part II, we have accepted the incoming call and enqueued the user to queue named ‘support’. Further we will play a music on hold while waiting for the agent to answer the call or the user to send a DTMF to register a callback or until the timeout which is 10 minutes by default
from twilio.twiml.voice_response import VoiceResponse, Gather
from twilio.rest import Client
import json, os
def lambda_handler(event, context):
params = event['body']
res = {}
for pair in params.split('&'):
key, _, value = pair.partition('=')
res[key] = str(value)
caller = res['Caller']
response = VoiceResponse()
gather = Gather(input='dtmf', num_digits=1, action='/v1/callbackregister', method='POST')
gather.play("http://com.twilio.sounds.music.s3.amazonaws.com/BusyStrings.mp3")
response.append(gather)
return {
'statusCode': 200,
'headers': {
'Content-Type': 'text/xml'
},
'body': str(response)
}
From the POST request we extract the caller value to identify the user’s phone number or client id (if using voiceSDK)
for pair in params.split('&'):
key, _, value = pair.partition('=')
res[key] = str(value)
caller = res['Caller']
gather = Gather(input='dtmf', num_digits=1, action='/v1/callbackregister', method='POST')
gather.play("http://com.twilio.sounds.music.s3.amazonaws.com/BusyStrings.mp3")
response.append(gather)
- Gather function has few options. Here we use the input as ‘dtmf‘ digits. This can be also ‘speech‘ so that we can leverage to recognize user’s speech.
- num_digits says how many maximum number of digits can be obtained before the timeout (default timeout is 5 seconds)
- ‘action’ is the URL where the gathered input will be sent to further processing and the HTTP method to be used to send to the URL is POST
- In this Gather function we will wait for user to provide a single digit DTMF input and send the call to another lambda function using the URL ‘/v1/callbackregister‘ using HTTP POST method
- While waiting for the input we will play a music on hold from the link http://com.twilio.sounds.music.s3.amazonaws.com/BusyStrings.mp3 in loop until an agent answers the call or call times out.
Continue reading Part IV