0
votes

I am migrating from Tropo to Twilio. I have a need to send an SMS to a monitored phone number while parsing a voice call (menu system).

The flow should be:
1. Incoming Voice call
2. Function sends SMS to one or more cell phones //alerts there is a voice call
3. Voice IVR system takes over and processes call

Using the Twilio Runtime functions (twilio's hosted node.js) I seem to be unable to combine both of these needs into a single function as the callback looks for a single twiml object. Further I also seem to be unable to send an SMS from within a voice call.

Is there example code anywhere, or is there solid documentation that might help me achieve my goal?

The following code works, but I have been advised by Twilio tech support not to use the SMS verb as its future viability is not guaranteed.

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

let twiml = new Twilio.twiml.VoiceResponse();

twiml.say("Hello World"); // respond to voice caller
twiml.sms({to:"+19735551212"},"Hello SMS!!"); // send SMS

callback(null, twiml);

}

2
Sounds like something you could do with Twilio Studio (twilio.com/console/studio), perhaps a "Send Message" widget followed by a "Run Function" widget. - Alex Baban
I was able to get it working in Studio similar to your suggestion. I was not however able to figure out how to share variables between widgets, or create a programmatic loop. Larger goal is to run through an array and send several people a notice via SMS. Any pointers on documentation? I couldnt find much. - jjwjj
Twilio's blog is a good place to learn more about Twilio Studio. Here is one blog where you can find more about using/passing parameters (twilio.com/blog/2018/06/…). - Alex Baban

2 Answers

0
votes

Twilio developer evangelist here.

Rather than use the TwiML to do both the call routing and the message sending, you can send messages with the REST API and use the TwiML just for the voice routing.

For example:

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

    const client = context.getTwilioClient();
    client.messages.create({  // Send SMS
        to: "+19735551212",
        from: event.From,
        body: "Hello from SMS"
    }).then(() => {  // When request to send SMS is complete, deal with the caller
        let twiml = new Twilio.twiml.VoiceResponse();
        twiml.say("Hello World");   // respond to voice caller
        callback(null, twiml);
    })    
}
0
votes

I got it working with a single iteration. The example @philnash posted above was correct with one semantic exception. The "from" parameter should be using "event.To" or "event.Called" as Twilio requires a valid Twilio number. "event.From" in my case was my cell phone and not a vaild Twilio number.

exports.handler = function(context, event, callback) {
    const client = context.getTwilioClient();
    client.messages.create({  // Send SMS
        to: "+19735551212",
        from: event.Called,  // **must be a valid Twilio number **
        body: "Hello from SMS"
    }).then(function(message) {
        console.log(event.Called);
        console.log(message.sid);
        let twiml = new Twilio.twiml.VoiceResponse();
        twiml.say("How now brown cow ");
        callback(null, twiml);
    });  
}