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
    From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

Incredible Demos

  • By
    WebSocket and Socket.IO

    My favorite web technology is quickly becoming the WebSocket API. WebSocket provides a welcomed alternative to the AJAX technologies we've been making use of over the past few years. This new API provides a method to push messages from client to server efficiently...

  • By
    jQuery Link Nudging

    A few weeks back I wrote an article about MooTools Link Nudging, which is essentially a classy, subtle link animation achieved by adding left padding on mouseover and removing it on mouseout. Here's how to do it using jQuery: The jQuery JavaScript It's important to keep...

Discussion

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