Search code examples
twiliotwilio-apitwilio-functions

Twilio tell the queued callers which number in a queue are they in,


Am trying to notify the queued callers about their position in the queue,

here is my current twilio function which enqueues calls

  let twiml = new Twilio.twiml.VoiceResponse();

    twiml.say("Please hold, while we connect you to one of our available agent ");
    twiml.enqueue({
    workflowSid: context.WORKFLOW_SID,
    })
    .task({}, `{"selected_skill":"operator","name":"${event.name}","email":"${event.email}"}`);
  callback(null, twiml);
so here i want to add something like twiml.say("You are now number ${number} in the queue");
};

but i'm stuck. In php i used twiml like this:

header("Content-type: text/xml");


$var = $_POST['QueuePosition'];

$message = '<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Play>http://com.twilio.sounds.music.s3.amazonaws.com/ClockworkWaltz.mp3</Play>
    <Say>You are currently in position '.$var.' in the queue.</Say>
    <Queue url="about_to_connect.php">support</Queue>
</Response>';
echo $message;

and it works, but now i want to use/am using twilio function.

Please help

Thanks in advance


Solution

  • Twilio developer evangelist here.

    The equivalent to $_POST['QueuePosition'] in a Twilio Function is event.QueuePosition.

    If you are receiving the queue position like that, you can insert that into the <Say> with the following code:

    const number = event.QueuePosition;
    twiml.say(`You are now number ${number} in the queue`);
    

    Note the use of backticks for the string to allow for interpolation using ${}.