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
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    9 More Mind-Blowing WebGL Demos

    With Firefox OS, asm.js, and the push for browser performance improvements, canvas and WebGL technologies are opening a world of possibilities.  I featured 9 Mind-Blowing Canvas Demos and then took it up a level with 9 Mind-Blowing WebGL Demos, but I want to outdo...

Incredible Demos

  • By
    Dynamic Waveform Visualizations with wavesurfer.js

    Waveform images are an awesome addition to boring audio widgets.  They can be functional as well as aesthetically pleasing, allowing users to navigate audio visually.  I recently found wavesurfer.js, an amazing waveform image utility that uses to Web Audio API to create super customizable...

  • By
    MooTools-Like Element Creation in jQuery

    I really dislike jQuery's element creation syntax. It's basically the same as typing out HTML but within a JavaScript string...ugly! Luckily Basil Goldman has created a jQuery plugin that allows you to create elements using MooTools-like syntax. Standard jQuery Element Creation Looks exactly like writing out...

Discussion

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