(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.
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 promisethen
function.Something like this: