0

(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.

  • Can you add a catch block to the create() function to see if for some reason the call to send a message via the REST API is returning an error. – Devin Rader Nov 12 '18 at 14:58

2 Answers2

0

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.

Devin Rader
  • 10,260
  • 1
  • 20
  • 32
  • Hello, indeed this is a voice request.I used your code but no SMS is sent. I hear also "an applicaiton error has occured. I adapt the post with the new code, which is already much better. Nb : the accounts are already used in production to send SMS through the same number so it should not be the problem. I also logged the value of the "client" before the operation and it contains the correct Account/token. – Laurent Berti Nov 02 '18 at 18:41
0

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);
    })
};
philnash
  • 70,667
  • 10
  • 60
  • 88