Search code examples
twiliotwilio-twimltwilio-functions

Twilio Inbound Text Message to Outbound Voice Call


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?


Solution

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