Make Calls with Twilio using the ThingHTTP App

Twilio is a cloud communications platform for phone calls and SMS messages. They have an API that you can send HTTP requests to and place phone calls. The API requires HTTPS and Basic Authentication which is perfect for high-level devices like phones and computers, but nearly impossible for an embedded device like the Arduino, Netduino, and mbed. The ThingHTTP App from ThingSpeak allows a device to make a simple API call over HTTP that gets relayed to any web service or API. The App supports HTTPS and is perfect to interface with the Twilio API with your device. Now, your thing can really speak…

Setup Twilio

Twilio is a paid service. At the time of this tutorial, your account will start with some funds to play in the sandbox. You will also have access to a phone number to use. You can take the service further by adding more funds and buying a dedicated phone number.

  • Sign up for Twilio
  • Find your API Credentials [AccountSid and Auth Token] under the Developer Tools section of the Twilio Dashboard

Setup ThingHTTP Request

  • Sign into ThingSpeak
  • Click Apps
  • Select  ThingHTTP
  • Click Create New Request
  • Enter a Name for your new request: Twilio Test
  • Enter the Making Calls API URL: https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Calls.json
  • Enter your AccountSid under HTTP Auth Username
  • Enter your Auth Token under HTTP Auth Password
  • Select POST method
  • Enter Content Type: application/x-www-form-urlencoded
  • Under Body enter the required parameters of From, To, and Url: From={sandbox_number}&To=%%number%%&{link_to_Twilio_formated_document}
  • Click Save ThingHTTP

Example Body:
This example body will speak the last light level on the “my_house” public feed.

From={sandbox_number}&To=%%number%%&Url=http://api.thingspeak.com/channels/9/field/1/last.txt&Method=GET

Send Request to ThingHTTP

POST /apps/thinghttp/send_request HTTP/1.1
Host: api.thingspeak.com
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: (number of characters in message)
api_key=(thinghttp_api_key)&number={phone_number_to_call}

Notes

  • The formats for the phone number used by Twilio follows the E.164 format, such as +13215551212
  • The Twilio formatted document can be any Url that outputs TwiML format (based on XML) – whatever this URL contains will be read back by Twilio
  • “&” must be encoded in the Twilio return Url with “%26”
  • Using the %%number%% in the ThingHTTP request allows you to dynamically change what number is called by the Twilio  request by passing the number in the request

15 comments

  1. I’m trying to send a SMS message using this technique, but can not seem to get Twilio to acknowledge the thingspeak URL. I have no problem sending put text, but can’t seem to get a response from thingspeak to be integrated into the page. I am using the proper Twilio app, as plain text messages get through just fine.

  2. Another thing to note… if you’re using this example to send texts from data in your channel it doesn’t seem to work because channels only send information in .txt .json and .xml and twilio won’t accept any of those. Or at least as I’ve seen.
    Also also, this tutorial needs updating! 😀

  3. I’m trying to send a SMS. First a test twilio’s account with same parameters on API Explorer, its work great.
    Then a create ThingHTTP Request
    Name
    Twilio send SMS
    API Key **********
    URL
    api.twilio.com/2010-04-01/Accounts/AC96*********/SMS/Messages
    HTTP Auth Username: AC96cce*********
    HTTP Auth Password: 344de2f**********
    Method:POST
    Content Type: application/x-www-form-urlencoded
    HTTP Version 1.1
    Body:
    To=+549387******&From=+164657*****&Body=hola
    but only get this:
    21211The ‘To’ number 549387******* is not a valid phone number.
    cheers

  4. I am getting the same error as Eduardo above.
    Using the API explorer to make the request works and so does running the CURL command from my machine locally.
    It seems the plus sign is getting stripped from the response.
    Trying to escape the + symbol doesnt work either.
    Looks like there is a bug in the ThingHTTP API.

    1. Hi – I just noticed that there were a few issues reported. Can you tell me what is happening? I just tested my Twilio call example and it is working with the + in front of the numbers. The to number has to be a phone number that Twilio supports. They do not support every country code.

      1. What you have written in ThingHTTP Body, Will you please tell me. I am facing problem to call operation using twilio

        1. Are you stuck at Twilio or ThingSpeak?

          Here’s the body on the Twilio side:

          From={sandbox_number}&To=%%number%%&Url=http://api.thingspeak.com/channels/9/field/1/last.txt&Method=GET

          1. How can i get the call with voice say ‘Temperature is 100 degree farenhite’ where 100 is the thingspeak channel data. What i need to write in body of ThingHTTP for it. Please help

          2. You can use “prepend” on the ThingSpeak side:

            From={sandbox_number}&To=%%number%%&Url=http://api.thingspeak.com/channels/9/field/1/last.txt?prepend=Temperature%2520is%2520&Method=GET

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.