Call another function in case of no-answer and Answering Machine Detection

1,121 views
Skip to first unread message

dces...@redhat.com

unread,
Sep 28, 2017, 12:30:06 PM9/28/17
to Twilio Functions
Hello,

I'm writing a function to forward some calls and (as stated in the title) I have two questions.

1 - There is a way to forward the execution to another function in case of no-answer?
  
   I have found some documentation about redirect (https://www.twilio.com/docs/api/twiml/redirect) but I'm not sure how to integrate it. If not possible to redirect to another function, I would be happy at least to provide another number.

    I was expecting that the "Primary handler fails" in the voice configuration, actually was able to execute another function in case of "no-answer", but that is not the case.

2 - There is some way to integrate answering machine detection (https://www.twilio.com/docs/api/voice/answering-machine-detection) with a function?


Thanks in advance


Daniel


Chris Corcoran

unread,
Sep 28, 2017, 2:07:56 PM9/28/17
to Twilio Functions
Hi Daniel,

Thanks for your interest in Twilio Functions, I'd be more than happy to help you get up and running. I've tried to answer your questions below.

1) Yes, you can forward the execution to another Function in the case of a no-answer. The <Redirect/> verb is exactly what you want. The Redirect verb works just like a HTTP redirect and Twilio will redirect the request to the provided URL. I've included some code below that illustrates how it can be used. You can also find out more about the Redirect verb here: https://www.twilio.com/docs/api/twiml/redirect

export.handler = function(context, event, callback) {
  let twiml = new Twilio.twiml.VoiceResponse();

  if (event.AnsweredBy.toLowerCase() == 'machine_start' || event.CallStatus.toLowerCase() == 'no-answer') {
  
    // Redirect to another Function
    twiml.redirect({
      method: 'POST'
    
  } else {
    
     // Add business logic for answered calls
    
  }

  callback(null, twiml);
}

2) Yes, you can integrate answer machine detection parameters with a Function. The event parameter passed into your Function contains all of the parameters that Twilio provides in to the URL you've configured for your Voice or Messaging URL. Answering Machine Detection adds the 'AnsweredBy' parameter to your Voice Webhook when enabled: https://www.twilio.com/docs/api/voice/answering-machine-detection#webhook-parameters. I've included an example below on how you can use AMD together with a Function:

export.handler = function(context, event, callback) {

  let twiml = new Twilio.twiml.VoiceResponse();
  let answeredBy = answeredBy ? answeredBy.toLowerCase() : null;
  
  switch(answeredBy) {
  
    // Redirect if a Human answered
    case 'human':
      twiml.redirect({method:'POST'}, 'http://twilio.com/handle-human);
      break;
      
    // Redirect if a Fax machine answered
    case 'fax':
      wiml.redirect({method:'POST'}, 'http://twilio.com/handle-fax);
       break;
       
    // Redirect if AMD detected a machine
    case 'machine_start':
    case 'machine_end_beep':
    case 'machine_end_silence':
    case 'machine_end_other':
      twiml.redirect({method:'POST'}, 'http://twilio.com/handle-machine);
      break;
     
    // Handle all other cases 
    case 'unknown':
    default:
      twiml.redirect({method:'POST'}, 'http://twilio.com/handle-unknown);
  }

  callback(null, twiml);
}

Hopefully, these answered help clarify the issues you were having. Let me know if you have any other questions or feedback for Twilio Functions.

Thanks Again,
Chris Corcoran
Product Manager, Twilio Runtime

dces...@redhat.com

unread,
Sep 29, 2017, 10:58:10 AM9/29/17
to Twilio Functions
Hi Chris,

Thanks for the Help! I have the integration working!

I have done an integration with Pagerduty to get on call user phone numbers, let me know if you want a snippet of the code for example sake! :)

Thanks again,

Daniel

Chris Corcoran

unread,
Oct 2, 2017, 1:47:47 PM10/2/17
to Twilio Functions
Hey Daniel,

It would be great if you could share your example code! I'm sure it would be helpful to other users who are looking to connect Twilio to PagerDuty

Thanks,
Chris Corcoran
Product Manager, Twilio Runtime

Grayson Cooper

unread,
Nov 23, 2017, 7:20:25 PM11/23/17
to Twilio Functions
Chris and Daniel,

Related question: Can you invoke a Twilio Function from outside of Twilio? Either from a webhook or an API? The introductory page on Twilio Functions seems to suggest this (https://support.twilio.com/hc/en-us/articles/115007737928-building-apps-with-twilio-functions, subhead: What information is the in the event parameter?"), but it doesn't seem explicit. 

If so, it would be great to have an example of using Twilio Functions for this. A couple easy, common use cases would be starting a voice or SMS broadcast, where an external server would be serving phone numbers and variables (e.g. names, message contents, etc.; easier to implement from a database, and with less concerns about latency/uptime) to Twilio Functions, which would handle the interactions with voice/sms recipients. Another example could be implementing the Slack API/outgoing webhooks to call Twilio Functions, similar to the pre-Twilio Functions Tutorial (https://www.twilio.com/blog/2016/05/build-sms-slack-bot-python.html).

Thanks,
Grayson 

Chris Corcoran

unread,
Nov 27, 2017, 5:07:19 PM11/27/17
to Twilio Functions
Hi Grayson,

Yes, you can use Twilio Functions from outside of Twilio. All you need to do is uncheck the "Check for valid Twilio signature" checkbox on Function instance page.

Removing the Twilio signature validation means that you can make an HTTP request to your function like any other application. You can use either JSON bodies or form-encoded parameters.

Let me know if you have any other questions. 

Thanks,
Chris Corcoran
Product Manager, Twilio Runtime
Reply all
Reply to author
Forward
0 new messages