Using Twilio with Codenvy

Below are various different projects talking to Twilio API that you can create with Codenvy Factories:


This document will explain the details of integrating the Twilio API into Codenvy. Twilio lets you to build voice and sms applications using its API that will enable you to receive incoming calls/smses and send outgoing calls/smses via Twilio API. The explanation has been done with 3 very basic Java servlet projects that makes use of the Twilio API through the Twilio Java client library to explain some basic features provided by the API.

Before proceeding to the creation of the Java projects, please register at https://www.twilio.com/try-twilio with your email id and then enter a phone number in the next screen to get a verification code from Twilio. This phone number will be registered in your Twilio account:

twilio

In the next screen, you will need to enter the verification code and you will then be taken to your account dashboard:

account

Under the Getting Started section, you will get your Twilio number that can received incoming calls and send outgoing calls on your application’s behalf. After getting the number, you can configure the trial number to invoke your application’s URL using the desired HTTP method:

trial_number

We will get to know more about this in our sample source codes.

On your account dashboard, you can also view your Account SID and Auth Token which would be used in your applications and these are demonstrated in our sample source codes.

The Twilio API has various methods to integrate with your application.

You can get more information about the API methods at https://www.twilio.com/docs. This section has various sub sections explaining the usage of various API methods. In a nutshell, the process of incoming calls is achieved using Twilio Markup Language (TwiML) and the process of outgoing calls is achieved using REST API which then makes use of TwiML. We will explain them in our sample source codes.

The sample source code is to explain the process of receiving a simple incoming call from any phone number to your Twilio number and the your application responding to the call with a simple message.

Create a Project

Login to your Codenvy workspace and create a WAR project named as ‘TwilioSample’. This will have the following structure:

project

Create pom.xml File

To make use of the Twilio API inside a servlet using the Twilio Java helper library, please update the pom.xml to include the HttpClient and Java EE Web API jars and the servletapi jar and the twilio-java-sdk jar into your project as Maven dependencies:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
<groupId>com.codenvy</groupId>
<artifactId>TwilioSample</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>TwilioSample</name>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.2.5</version>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-web-api</artifactId>
      <version>6.0</version>
    </dependency>
    <dependency>
      <groupId>com.twilio.sdk</groupId>
      <artifactId>twilio-java-sdk</artifactId>
      <version>3.3.15</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  <repositories>
     <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
     </repository>
  </repositories>
  <build>
    <finalName>TwilioSample</finalName>
  </build>
</project>

Creater Java Class

Now create a Java servlet class named ‘TwilioIncomingServlet.java’ that will send a TwiML response back to Twilio when the application is invoked via the incoming call. The TwiML response is being sent with the help of the Twilio API provided by the Twilio Java library. All this is done inside the method doGet() in the sample source Java class.

package com.codenvy;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.twilio.sdk.verbs.TwiMLResponse;
import com.twilio.sdk.verbs.TwiMLException;
import com.twilio.sdk.verbs.Say;

public class TwilioIncomingServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
      // Create a TwiML response and add our friendly message.
        TwiMLResponse twiml = new TwiMLResponse();
        Say say = new Say("Hello World, How are you doing today");
        try {
            twiml.append(say);
        } catch (TwiMLException e) {
            e.printStackTrace();
        }
        response.setContentType("application/xml");
        response.getWriter().print(twiml.toXML());
    }
}

Create index.jsp File

Your Codenvy project will have an index.jsp which will invoke the Servlet class -

<%
response.sendRedirect("/hello");
%>

Create web.xml File

The servlet mapping ‘/hello’ is defined in the web.xml as follows –

<web-app>
<display-name>Twilio Incoming Servlet App</display-name>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.codenvy.TwilioIncomingServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>

Build and Run Your Project

Now you build and run your Codenvy project and the TwiML response will be displayed in the browser:
app
This browser URL will now need to be configured in the Voice URL section of your Twilio number in your Twilio account and please also select the HTTP method type:
app1

Now using any phone make a call to your Twilio number. On receiving the call, the Twilio will invoke your application URL configured and the application will then send the TwiML response displayed above to the Twilio and Twilio will then send out the greeting message to the caller. This way, you can make your application respond to an incoming call using the Twilio API.