Enable user to user calls, texts and phone number verificator with Twilio

By | December 17, 2015

One of our projects needed to set up voice and short message text communication between users through mobile phones and/or tablets. There are dozens solutions (some of them are given in List of some communication tools and phone number verification), we analyzed them and choose Twilio tool. We used it for few projects and knew that it works fine. Main features of this tool that we liked from the start are:

  1. single platform that provides one API for all types of communication (voice, video, SMS, MMS, and real-time IP communications);
  2. the tool has worldwide coverage, so it doesn’t matter where are the users who use the DevelopEx’s software;
  3. scaling – due to principle of Cloud communication Twilio doesn’t depend on peak usage estimates, thus is ready to extra capacity;
  4. the tool supports anonymous connection in case of privacy.

List of some communication tools and phone number verification

  1. Twilio. Has short codes. Pricing depends on messages count.
  2. Plivo. Has short codes. Pricing ~ $3,000 per month (good article about Plivo usage)
  3. Sinch. Has short codes. Pricing depends on messages count and country. Has standalone option as SMS/call number verification.
  4. Checkmobi. Has check number API.
  5. Ringcaptcha. Number verification service.
  6. Dial2verify. Verification via missed calls.
  7. Nexmo. Has number verification service.

How we used Twilio

Actually, Twilio web-site has very good documentation, for instance, for Android SDK implementation process. So we just repeat main steps how to set up voice, text and video connection between users easily. We assume that the reader has already used Android Studio with all needed tools common for any developer. Then:

  1. Sign Up to Twilio (trial account could be registered for free), get a Twilio Account SID and Auth Token to start calls from Android device.
  2. Download Twilio Client Android SDK, unzip and copy downloaded files to root directory project.
  3. Integrate Twilio API to the project according to aimed task. There are a wide range of cases how to apply Twilio.

Technical integration

Step 1. Open AndroidManifest.xml of our project and declare permissions.

 <!-- needed to do anything on the network -->
 <uses-permission android:name="android.permission.INTERNET" />
 <!-- needed to receive audio from the microphone when in a call -->
 <uses-permission android:name="android.permission.RECORD_AUDIO" />
 <!-- needed to e.g. enable/disable the speakerphone on request -->
 <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
 <!-- needed to monitor for when the network is available/unavailable in order to refresh background connections -->
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Step 2. Use the proper Twilio Client Android SDK classes to establish voice connection. Twilio’s API suggests such classes: Twilio, Device, Connection, DeviceListener, ConnectionListener and PresenceEvent. The class Twilio is used to initialize the object of Device class that coördinates service authorization with Twilio, listens for incoming connections, and establishes outgoing connections. The create Connection for Device instance. A Connection object represents the connection between a Device and Twilio’s services. A Connection is either incoming or outgoing. Actually, we applied just outcoming calls. Corresponding incoming calls are provided by Twilio server.

To control the Connection’s state we used class ConnectionListener. The instance of this class reports us if something wrong. When the connection happened or when the connection was interrupted. Thus, ConnectionListener object gives us ability to treat unpredictable situations.

Step 3. Deploy a server to perform two functions:

a) provide capability tokens to our Android client application;
b) serve up TwiML application and/or make REST API calls to orchestrate Twilio’s telephony services.

The Twilio docs suggest to use Heroku (Cloud platform with integrated data services) or ngrok (tool that tunnel local ports to public URLs and inspect traffic). But we applied own code in case of its simplicity and needing to work not only with Twilio.

This is a simple TwiML (“Twilio Markup Language“) application that will play an announcement whenever you make a call from your Android application.

Step 4. SMS connection is provided by sending message to server part. For security reason we use own server. It encrypts data of outgoing signal, send it to Twilio server that, in turn, send message to target user. Also, SMS service is used for verification telephone numbers of new users.

Final Twilio using

Now everything is ready to use Twilio to connect users with each other (Diagram 1).

Enable user to user calls, texts and phone number verificator with Twilio
Diagram 1. Voice and SMS interaction between two mobile devices using DevelopEx’ applications, Twilio and DevelopEx server (picture created with draw.io).

Call from device emulator
Screenshot 1. Call from device emulator

Capture from Android SDK
Screenshot 2. Capture from Android SDK that represents a part of TwilioPhone class and requests to Twilio server.

User_1 calls to User_2 (Screenshot 1). And what happens in reality. The User_1 application, actually, the Device instance from Twilio Client Android SDK sends request to Twilio server (Screenshot 2). Interaction between devices and server uses TwiML code format. When the User start the phone call, the application generate TwiML code finally that contains request to Twilio server (Listing 2).

<?xml version="1.0" encoding="UTF-8"?>
	<Dial callerId="+16024039126">

Listing 2. Using TwiML (“Twilio Markup Language“) to exchange data between client and server.

Twilio server gets the input data, uses token that’d given during registration, uses the Twilio Phone Number API to instantly search and provision a number to match the local area and country code. As a conversation happens, the app assigns an available phone number to use. Calls and texts are forwarded to masked phone numbers automatically. Finally, the app pulls call data for internal reporting and dashboarding. There are no limits to calls for one time. After a short time (less than 1 second as Twilio docs say) and without reference to distance between two users Twilio software establish connection with User_2.


The Twilio communication tool is based on Cloud communication platform that allow developers to embed voice, messaging and video directly into software applications. It uses one API for all platforms and for all kinds of connections (voice, video, SMS, MMS, and real-time IP communications). Also, Twilio has useful feature for marketing: simplified communication plan (no capacity planning, carrier contracts and telecom hardware) that helps to promote this software itself.

As always, feel free to contact us for a consultation!

One thought on “Enable user to user calls, texts and phone number verificator with Twilio

  1. Pingback: Move to Simplicity: Authentication Using “Digits” | DevelopEx blog

Leave a Reply

Your email address will not be published. Required fields are marked *