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
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

Incredible Demos

  • By
    Using MooTools to Instruct Google Analytics to Track Outbound Links

    Google Analytics provides a wealth of information about who's coming to your website. One of the most important statistics the service provides is the referrer statistic -- you've gotta know who's sending people to your website, right? What about where you send others though?

  • By
    Using Opacity to Show Focus with jQuery

    A few days back I debuted a sweet article that made use of MooTools JavaScript and opacity to show focus on a specified element. Here's how to accomplish that feat using jQuery. The jQuery JavaScript There you have it. Opacity is a very simple but effective...

Discussion

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