Queue using Twilio – Part II

A continuation of Queue with Twilio. Part I is here

TwiML or Twilio Markup Lanaguage

Set of instructions to tell Twilio how to manage the call or message. In this Queue system, we would manage TwiML using Python as an AWS Lambda function.

When the user calls into the system either to a Phone Number assigned to the TwiML app or using a VoiceSDK which is pointed to the TwiML app, it would be sent to AWS lambda function. The TwiML app is initiates a HTTP GET request to AWS Lambda function.

Let’s call this first entry point function as userToQ

from twilio.twiml.voice_response import VoiceResponse
from twilio.rest import Client
import json, os

def lambda_handler(event, context):

    qName = event.get('queryStringParameters')["qName"]
    caller = event.get('queryStringParameters')["Caller"]

    if not qName:
        qName= "support"

    response = VoiceResponse()
    response.say("Thanks for your call. Hold until an agent is available to answer your call")
    response.enqueue(qName, wait_url='/v1/letkeepinQ', action='/v1/QoverFlow', method='GET')

    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'text/xml'
        },
        'body': str(response)
    }
  1. qName is the name of the queue and caller is the user’s client id (if VoiceSDK) or caller-id (if from PSTN). Both the parameters are extracted from the queryStringParameters of the GET request.
  2. if the qName is not provided or available in GET request, we send the call to ‘support’ queue.
  3. Creating a new instance of VoiceResponse() and assign it to ‘response’ which is equivalent to <Response> verb in TwiML
  4. response.say is equivalent to <Say>verb in TwiML. With this verb we just prompt out the phrase to the caller
  5. In response.enqueue which is equivalent to <Enqueue> verb in TwiML, we are adding the user to the support queue. qName is the name of the queue and wait_url is an URL pointing to a TwiML document containing verbs to be executed while the user is in queue. The document will be executed completely and will be looped until the user is dequeued or enqueue times out. A request will be sent to the action attribute URL when the call leaves the queue with deque reason and details about the time spent in the queue. By default the wait_url used HTTP POST method.

Continue reading Part III

Leave a Comment

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

Scroll to Top