2

I want Twilio to initiate an outbound call to my mobile phone when anyone sends a text message to my Twilio number.

I would prefer to do this with a TwiML Bin or Twilio Function or something hosted by Twilio so I do not have to run my own web server.

How can I do this?

Zack Peterson
  • 56,055
  • 78
  • 209
  • 280

2 Answers2

2

I found some Functions help documentation that got me moving in the right direction.

I've got a TwiML Bin "SMS to Voice TwiML Bin":

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say loop="3">{{Body}}</Say>
</Response>

And a Function "SMS to Voice Function" that uses that TwiML:

exports.handler = function(context, event, callback) {
  const client = context.getTwilioClient()
  client.calls.create({
    to:   '+...', 
    from: '+...', 
    url:  'https://handler.twilio.com/twiml/...?Body=' + encodeURIComponent(event.Body) }, // SMS to Voice TwiML Bin
    function(err, res) {
      callback(err, "OK")
    })
};

And in the Twilio console, for my Twilio number, under Messaging, I have my A MESSAGE COMES IN set to "Function" and "SMS to Voice Function".

This works. If the Twilio number receives a text, then Twilio calls my mobile phone number and speaks the original text message.

Zack Peterson
  • 56,055
  • 78
  • 209
  • 280
  • Sounds like you've nailed it. Let me know (or ask another question tagged Twilio) if you need any more guidance on this. – philnash Oct 24 '17 at 00:13
1

If I understand what you are doing, this is a response that is coming from an inbound SMS message.

The problem is that you need to initiate an actual phone call. The SMS message is not in a call so it cannot dial anything.

Check out the documentation for Making Calls for how you can start a call. You will likely need the SMS to hit a function (Twilio function or your own code) that can then make the Twilio REST call to start a new call.

Rob Di Marco
  • 43,054
  • 9
  • 66
  • 56