Twilio function used in autopilot task should send SMS but returns "an application error has occured"

404 views Asked by At

(2nd updated version after input - there is progress - thank you) I want to use a Twilio function to send a SMS during a call (to avoid to make a full external application). The goal is to send a confirmation SMS at the end of an automated call (with Twilio autopilot). If the user says the correct sentence ("I want a confirmation by SMS please"), autopilot launches the following tasks.

{
    "actions": [
        {
            "redirect": "https://my_domain_here.twil.io/my_url_here"
        }
    ]
}

Then my function has the following code :

    exports.handler = function(context, event, callback) {
    var client = context.getTwilioClient();
/**to have some view on incoming request    */
    console.log("context : "+JSON.stringify(context));
    console.log("event : "+JSON.stringify(event));
//send the answer SMS
    console.log("sending SMS");
client.messages
  .create({
    body: 'test sms',
    from: '+32x_my_nbr_xx',
    to: '+32x_my_other_nbr_xx'//is hardcoded - just to test
  })
.then(message => console.log(message.sid))
.done();
//now create the Twiml to send back to autopilot the text to be spoken
    console.log("SMS sent, returning to autopilot");
    var action = {"actions": [{"say": "I understand that you want a confirmation. I will send you a summary by SMS. Goodbye" }]};
    callback(null, action);
}

But when I call and I say "I want a confirmation by SMS", then I hear 'I understand that you want a confirmation. I will send you a summary by SMS. Goodbye". But no SMS is sent. When I look in the logs of autopilot, the correct intent was triggered. The log of the function contains nothing (just regular logs but not the Msgid) Anybody an idea ?

I know it would work but is there really no way to avoid writing and maintaining a complete backend just to send this SMS ? Thx in advance.

2

There are 2 answers

0
philnash On BEST ANSWER

Another Twilio developer evangelist here.

The update that used the REST API should help, but I think the problem now is that you are returning from the function before the API request is complete. Rather than calling the callback after the promise resolution, you should call it within the promise then function.

Something like this:

exports.handler = function(context, event, callback) {
  var client = context.getTwilioClient();
  /**to have some view on incoming request    */
  console.log('context : ' + JSON.stringify(context));
  console.log('event : ' + JSON.stringify(event));
  //send the answer SMS
  console.log('sending SMS');
  client.messages
    .create({
      body: 'test sms',
      from: '+32x_my_nbr_xx',
      to: '+32x_my_other_nbr_xx' //is hardcoded - just to test
    })
    .then(message => {
      console.log('SMS sent, returning to autopilot');
      var action = {
        actions: [
          {
            say:
              'I understand that you want a confirmation. I will send you a summary by SMS. Goodbye'
          }
        ]
      };
      callback(null, action);
    })
};
1
Devin Rader On

Twilio developer evangelist here.

I suspect that the request being made by Autopilot is a Voice request which means you won't be able to return a MessageResponse since that only work for requests coming from an incoming SMS message.

To send the text in that Function you'll need to use the Node helper library to make a call to the REST API:

client.messages
  .create({
    body: 'This is the ship that made the Kessel Run in fourteen parsecs?',
    from: '+15017122661',
    to: '+15558675310'
  })
.then(message => console.log(message.sid))
.done();

Hope that helps.