Teaching how to integrate with Twilio APIs

In this section we are going to build a simple REST API that calls a “Programmable Voice call and SMS” via using Twilio APIs.

This example is based in one of the samples provided by Twilio (see below).

Original Calling clicktocall-node NodeJS project.

This is the orignal examples/tutorials provided by Twilio. See https://www.twilio.com/docs/tutorials/walkthrough/click-to-call/node/express for more information.

How It Works

This helps demonstrates how to initialize a call using the Twilio REST API and how to create a TwiML call logic using the Say verb. I am assuming to be using a Debian based Linux environment (e.g. Ubuntu 16.04)

Let’s go over the necessary steps to implement click-to-call

  • A website visitor wants to connect to your organization. She submits a web form with her phone number to receive a call connecting her to your sales or support team.
  • Your web application receives the submission form. An HTTP request is initiated from your application to Twilio to initiate an outbound call.
  • Twilio receives the request and initiates a call to the user’s phone number.
  • User receives the call.
  • After the call connects, Twilio asks our application for TwiML instructions. Our TwiML instructs Twilio to connect the user to our sales or support teams.

Extending original Twilio based NodeJS clicktocall example

By now you should understand how the original Twilio sample works, if not get back and try these steps first. Now it is time to extend the original demo to allow the ability to also:

Follow the next steps to build this example:

  • Go to the GitHub repository of this project (https://github.com/barackd222/s2v-iia-nodejs-voicesms-apis) and download the entire project (either clone or download zip file). If you decide to simply download the zip file, unzip it in a known location where you want to run your project.
  • Edit ~/.bashrc and set your account configuration values. Replace the values accordingly.

    vi ~/.bashrc

    export TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxx

    export TWILIO_AUTH_TOKEN=yyyyyyyyyyyyyyyyy

    export TWILIO_NUMBER=+1XXXXX

    export LOCAL_TWILIO_NUMBER=+61XXXXXX

    export ICS_SERVER=integration-xxx.integration.us2.oraclecloud.com

    export ICS_USERNAME=xxxxxxx

    export ICS_PASSWORD=xxxxxxxxx

  • Ensure the variables are set properly:

    echo $TWILIO_ACCOUNT_SID && echo $TWILIO_AUTH_TOKEN && echo $TWILIO_NUMBER

    Make sure you see the values. Otherwise you will have to re-start your shell session.

  • Move within your project folder (make sure you are in the same location as in package.json) and install npm dependencies:

    npm install

    npm install body-parser

    npm install http https

You also need body-parser module to [arse JSON payloads as part of the POST body:

If you need to further use this module to easily parse JSON payload objects, in your code:

  • Add the following after you define the app variable.
    • var bodyParser = require(‘body-parser’);
    • app.use(bodyParser.json()); // support json encoded bodies
    • app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
  • With body-parser configured, we can now create a route. That is, simply grab POST parameters using req.body.variable_name
  • Make sure all dependencies are installed successfully

  • Run the application:

    node app

    The application will show you the port where it is running on.

  • Open a browser window and go to http://[IP]:[Port]

Author: Carlos Rodriguez Iturria

I am extremely passionate about people, technology and the most effective ways to connect the two by sharing my knowledge and experience. Working collaboratively with customers and partners inspires and excites me, especially when the outcome is noticeable valuable to a business and results in true innovation. I enjoy learning and teaching, as I recognise that this is a critical aspect of remaining at the forefront of technology in the modern era. Over the past 10+ years, I have developed and defined solutions that are reliable, secure and scalable, working closely with a diverse range of stakeholders. I enjoy leading engagements and am very active in the technical communities – both internal and external. I have stood out as a noticeable mentor running technology events across major cities in Australia and New Zealand, including various technology areas such as, Enterprise Integrations, API Management, Cloud Integration, IaaS and PaaS adoption, DevOps, Continuous Integration, Continuous Automation among others. In recent years, I have shaped my role and directed my capabilities towards educating and architecting benefits for customers using Oracle and AWS Cloud technologies. I get especially excited when I am able to position both as a way to exceed my customers’ expectations. I hold a bachelor degree in Computer Science and certifications in Oracle and AWS Solutions Architecture.

1 thought on “Teaching how to integrate with Twilio APIs”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s