Redirect after handling input from Gather

427 views
Skip to first unread message

James Scaggs

unread,
May 17, 2020, 2:08:21 PM5/17/20
to Twilio Functions
I receive a post request to a gather.js serverless function which creates an outbound call via API.

The gather.js function prompts the receiver of the call to enter some digits using the .gather() method.

gather.js
exports.handler = function (context, event, callback) {
 const response = new Twilio.twiml.VoiceResponse();
 // initial greeting
 response.say("Hello demo gathering starting now");
 console.log(event);
 const gather = response.gather({
   timeout: 20,
   method: "GET",
   numDigits: 1,
 });

  gather.play(
   {
     loop: 1,
   },
 );
 callback(null, response);


I have another function handoff-call.js which is used as the action in .gather() above.

The receiver of the call has two options.

Option 1
If the receiver is available to take an inbound phone call they press 1.
This triggers a .dial() method and connects them to another phone number. This is working as expected.

Option 2
If the receiver is unavailable to take an inbound phone call they press 2.

Here are the next steps I need to take:

Trigger another outbound call via API to the next number in an array and repeat the gather function

Am I on the right track to use the Redirect https://www.twilio.com/docs/voice/twiml/redirect twiml posting back to my original gather.js?

Is there a better/preferred way to handle this?

Thanks in advance,

James

Alan

unread,
May 18, 2020, 5:25:54 AM5/18/20
to Twilio Functions
That sounds like the right approach, you can pass URL query parameters back in as one way to maintain state. Also, check for the case when no digits are entered (for example voicemail picks up).

James Scaggs

unread,
May 18, 2020, 9:03:52 AM5/18/20
to Twilio Functions
Thanks, I'll give it a go and let you know how it goes.

James Scaggs

unread,
May 18, 2020, 5:09:11 PM5/18/20
to Twilio Functions
@alan

I decided to just trigger another outbound API call specifying a url parameter which seems to accomplish what I'm after.

client.calls.create({
to: testNumber,
from: devNumber,
});
Reply all
Reply to author
Forward
0 new messages