Twilio: the Ultimate Communication Tool

By  on  

My favorite part of the mobile revolution was the explosion of communication tools.  I grew up chatting on AOL Instant Messenger, MSN Messenger, and other desktop messaging tools, so when mobile phones took on text messaging, I was hyped for a new generation of mobile communication possibilities.  Today we get push notifications and have dedicated communication apps like WhatsApp, Facebook Messenger, etc -- wow, life has changed!  If I wanted to optimize my reach to users, I'd want a service that could accommodate as many communication avenues as possible.  That's why Twilio is such a success: a progressive, flexible, all-encompassing service that allows you to reach your users in a variety of formats!

Quick Hits

  • Twilio provides world class APIs and infrastructure for communicating over SMS, WhatsApp, Facebook messenger, and more
  • Provides an easy XML-like language, TwiML, to respond to received SMS messages
  • Twilio provides an API and helper libraries for numerous languages: PHP, Node.js, and more
  • Twilio provides dozens of easy to understand tutorials
  • Create SMS chat bots and interactive voice responders with their visual builder or with easy to use APIs
  • Use Twilio to manage two factor authentication
  • Create programmable video:  build in-app voice and video with cloud infrastructure and powerful SDKs;  everything you need to build with WebRTC.
  • Used by Twitch, Zendesk, eBay, Trulia, Lyft, and more

Basic Setup

You can sign up for a free account at Twilio so you can get a feel for the service.  Once you sign up, Twilio provides you a console which acts as a starting block for all of the "project" types you can create with Twilio.  In most cases, you'll need a Twilio phone number, which Twilio provides to you readily.  With each project, you'll generate an auth token that you'll use within your API.

Sending SMS Messages with Twilio

Sending a text message to your user or customer, whether it be an appointment reminder, a sales receipt, or authentication, is incredibly useful; what's awesome from a developer perspective is that Twilio makes it so easy to do:

const accountSid = 'XXXXXX';
const authToken = 'XXXXXX';
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
     body: 'David Walsh Blog rules!',
     from: '+16088889069',
     to: '+16088675309'
   })
  .done();

The result is just as you'd hope for:

A clean text message just as you'd directed Twilio to send!

Making a Phone Call with Twilio

If you want to go a step further and have Twilio act as a dialer, the API is just as easy for making a simple phone call:

const accountSid = 'XXXXXX';
const authToken = 'XXXXXX';
const client = require('twilio')(accountSid, authToken);

client.calls
  .create({
    url: 'http://demo.twilio.com/docs/voice.xml',
    to: '+16088675309',
    from: '+16088889069'
  });

Within three seconds my phone was ringing as expected!  Twilio makes the developer experience so easy.

Recording Phone Calls

Using Twilio as an answering machine is another simple task you can code Twilio with:

const express = require('express');
const VoiceResponse = require('twilio').twiml.VoiceResponse;

const app = express();

// Returns TwiML which prompts the caller to record a message
app.post('/record', (request, response) => {
  // Use the Twilio Node.js SDK to build an XML response
  const twiml = new VoiceResponse();
  twiml.say('Hello. Please leave a message after the beep.');

  // Use  to record the caller's message
  twiml.record();

  // End the call with 
  twiml.hangup();

  // Render the response as XML in reply to the webhook request
  response.type('text/xml');
  response.send(twiml.toString());
});

// Create an HTTP server and listen for requests on port 3000
app.listen(3000);

Twilio's Voice API is remarkable and, what's even better, is incredibly easy to use as a developer and the eventual recipient of the message.

Twilio's reputation as an industry leader in communications, from fax to SMS to voice onto video, is well earned.  Their communication capabilities make life easy for the developer, the end user, and the intermediaries managing the communications!

Recent Features

  • By
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

  • By
    Introducing MooTools HeatMap

    It's often interesting to think about where on a given element, whether it be the page, an image, or a static DIV, your users are clicking.  With that curiosity in mind, I've created HeatMap: a MooTools class that allows you to detect, load, save, and...

  • By
    AJAX For Evil:  Spyjax with jQuery

    Last year I wrote a popular post titled AJAX For Evil: Spyjax when I described a technique called "Spyjax": Spyjax, as I know it, is taking information from the user's computer for your own use — specifically their browsing habits. By using CSS and JavaScript, I...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!