Queue using Twilio – Part IV

Part III has lambda function which keeps the user in the queue and waits for an agent to answer the call or the user to provide DTMF digit 1 to register a callback or simply to times out from the queue.

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)

This part will show a lambda function which registers the user number for a callback.

from twilio.twiml.voice_response import VoiceResponse, Gather
import json

def lambda_handler(event, context):

    params = event['body']
    res  = {}
    for pair in params.split('&'):
        key, _, value = pair.partition('=')
        res[key] = str(value)

    Digits = res['Digits']
    if len(Digits) == 1 and int(Digits) == 1:
        response = VoiceResponse()
        response.say('Please enter your callback number')
        gather = Gather(input='dtmf')
        response.append(gather)

    elif len(Digits) > 1:
        response = VoiceResponse()
        response.say('Your callback number is ' + Digits + 'To confirm press 2. To change your callback number press 1')
        gather = Gather(input='dtmf')
        response.append(gather)

    elif len(Digits) == 1 and int(Digits) == 2:
        response = VoiceResponse()
        response.say('We have registered your callback request. We will get back to you at the earliest')
        response.hangup()

    else:
        response = VoiceResponse()
        response.say('Sorry I couldn\'t understand. If you would like to have a callback at any time press 1')
        response.redirect(url='/v1/keepinqueue/', method='POST')

    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'text/xml'
        },
        'body': str(response)
    }

Since the callbackregister request is a HTTP POST method, we will extract the DTMF digit pressed from the body of the HTTP request as below

params = event['body']
    res  = {}
    for pair in params.split('&'):
        key, _, value = pair.partition('=')
        res[key] = str(value)

    Digits = res['Digits']

Once the Digit(s) extracted, we will check whether the user pressed ‘1’ or other. If the DTMF digit is 1, then we will request for the callback number where the user wants us to call. The equivalent python code is:

if len(Digits) == 1 and int(Digits) == 1:
        response = VoiceResponse()
        response.say('Please enter your callback number')
        gather = Gather(input='dtmf')
        response.append(gather)

If you notice ‘Gather‘ we do not have an action URL like we have in the previous function (letkeepinQ). Which means the action URL will be the same URL where the Gather originates or in Twilio words: Twilio will POST to the URL that houses the active TwiML document. When the user inputs the callback number, then the gathered input is sent to the same URL and we will extract the callback number in the following code

 elif len(Digits) > 1:
        response = VoiceResponse()
        response.say('Your callback number is ' + Digits + 'To confirm press 2. To change your callback number press 1')
        gather = Gather(input='dtmf')
        response.append(gather)

In the above code we get the call number in Digits variable and prompt it back to the user and ask them to confirm by pressing DTMF digit 2 or to change with digit 1. If the user presses 1 we will go back to the previous block where we will again start to collect the callback number

elif len(Digits) == 1 and int(Digits) == 2:
        response = VoiceResponse()
        response.say('We have registered your callback request. We will get back to you at the earliest')
        response.hangup()

In this block we confirm the callback number when the user sends DTMF digit 2 and hence we will prompt that the callback request has been registered. Since this is a test project I haven’t further processed the callback request. However, in actual scenario you can write the callback number collected in ‘Digits’ variable along with Timestamp, call sid of this call etc to a database

Finally if none of the expected Digits or logic received for the above blocks, we will prompt a message and send back the user to the queue:

 else:
        response = VoiceResponse()
        response.say('Sorry I couldn\'t understand. If you would like to have a callback at any time press 1')
        response.redirect(url='/v1/keepinqueue/', method='POST')

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top