title | description | services | documentationcenter | author | manager | editor | ms.assetid | ms.service | ms.workload | ms.tgt_pltfrm | ms.devlang | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to Make a phone call from Twilio (Java) | Microsoft Docs |
Learn how to make a phone call from a web page using Twilio in a Java application on Azure. |
java |
devinrader |
twilio |
mollybos |
0381789e-e775-41a0-a784-294275192b1d |
multiple |
na |
na |
Java |
article |
11/25/2014 |
The following example shows you how you can use Twilio to make a call from a web page hosted in Azure. The resulting application will prompt the user for phone call values, as shown in the following screen shot.
You'll need to do the following to use the code in this topic:
- Acquire a Twilio account and authentication token. To get started with Twilio, evaluate pricing at http://www.twilio.com/pricing. You can sign up at https://www.twilio.com/try-twilio. For information about the API provided by Twilio, see http://www.twilio.com/api.
- Obtain the Twilio JAR. At https://github.com/twilio/twilio-java, you can download the GitHub sources and create your own JAR, or download a pre-built JAR (with or without dependencies). The code in this topic was written using the pre-built TwilioJava-3.3.8-with-dependencies JAR.
- Add the JAR to your Java build path.
- If you are using Eclipse to create this Java application, include the Twilio JAR in your application deployment file (WAR) using Eclipse's deployment assembly feature. If you are not using Eclipse to create this Java application, ensure the Twilio JAR is included within the same Azure role as your Java application, and added to the class path of your application.
- Ensure your cacerts keystore contains the Equifax Secure Certificate Authority certificate with MD5 fingerprint 67:CB:9D:C0:13:24:8A:82:9B:B2:17:1E:D1:1B:EC:D4 (the serial number is 35:DE:F4:CF and the SHA1 fingerprint is D2:32:09:AD:23:D3:14:23:21:74:E4:0D:7F:9D:62:13:97:86:63:3A). This is the certificate authority (CA) certificate for the https://api.twilio.com service, which is called when you use Twilio APIs. For information about adding this CA certificate to your JDK's cacert store, see Adding a Certificate to the Java CA Certificate Store.
Additionally, familiarity with the information at Creating a Hello World Application Using the Azure Toolkit for Eclipse, or with other techniques for hosting Java applications in Azure if you are not using Eclipse, is highly recommended.
The following code shows how to create a web form to retrieve user data for making a call. For purposes of this example, a new dynamic web project, named TwilioCloud, was created, and callform.jsp was added as a JSP file.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Automated call form</title>
</head>
<body>
<p>Fill in all fields and click <b>Make this call</b>.</p>
<br/>
<form action="makecall.jsp" method="post">
<table>
<tr>
<td>To:</td>
<td><input type="text" size=50 name="callTo" value="" />
</td>
</tr>
<tr>
<td>From:</td>
<td><input type="text" size=50 name="callFrom" value="" />
</td>
</tr>
<tr>
<td>Call message:</td>
<td><input type="text" size=400 name="callText" value="Hello. This is the call text. Good bye." />
</td>
</tr>
<tr>
<td colspan=2><input type="submit" value="Make this call" />
</td>
</tr>
</table>
</form>
<br/>
</body>
</html>
The following code, which is called when the user completes the form displayed by callform.jsp, creates the call message and generates the call. For purposes of this example, the JSP file is named makecall.jsp and was added to the TwilioCloud project. (Use your Twilio account and authentication token instead of the placeholder values assigned to accountSID and authToken in the code below.)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
import="java.util.*"
import="com.twilio.*"
import="com.twilio.sdk.*"
import="com.twilio.sdk.resource.factory.*"
import="com.twilio.sdk.resource.instance.*"
pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Call processing happens here</title>
</head>
<body>
<b>This is my make call page.</b><p/>
<%
try
{
// Use your account SID and authentication token instead
// of the placeholders shown here.
String accountSID = "your_twilio_account";
String authToken = "your_twilio_authentication_token";
// Instantiate an instance of the Twilio client.
TwilioRestClient client;
client = new TwilioRestClient(accountSID, authToken);
// Retrieve the account, used later to retrieve the CallFactory.
Account account = client.getAccount();
// Display the client endpoint.
out.println("<p>Using Twilio endpoint " + client.getEndpoint() + ".</p>");
// Display the API version.
String APIVERSION = TwilioRestClient.DEFAULT_VERSION;
out.println("<p>Twilio client API version is " + APIVERSION + ".</p>");
// Retrieve the values entered by the user.
String callTo = request.getParameter("callTo");
// The Outgoing Caller ID, used for the From parameter,
// must have previously been verified with Twilio.
String callFrom = request.getParameter("callFrom");
String userText = request.getParameter("callText");
// Replace spaces in the user's text with '%20',
// to make the text suitable for a URL.
userText = userText.replace(" ", "%20");
// Create a URL using the Twilio message and the user-entered text.
String Url="http://twimlets.com/message";
Url = Url + "?Message%5B0%5D=" + userText;
// Display the message URL.
out.println("<p>");
out.println("The URL is " + Url);
out.println("</p>");
// Place the call From, To and URL values into a hash map.
HashMap<String, String> params = new HashMap<String, String>();
params.put("From", callFrom);
params.put("To", callTo);
params.put("Url", Url);
CallFactory callFactory = account.getCallFactory();
Call call = callFactory.create(params);
out.println("<p>Call status: " + call.getStatus() + "</p>");
}
catch (TwilioRestException e)
{
out.println("<p>TwilioRestException encountered: " + e.getMessage() + "</p>");
out.println("<p>StackTrace: " + e.getStackTrace().toString() + "</p>");
}
catch (Exception e)
{
out.println("<p>Exception encountered: " + e.getMessage() + "");
out.println("<p>StackTrace: " + e.getStackTrace().toString() + "</p>");
}
%>
</body>
</html>
In addition to making the call, makecall.jsp displays the Twilio endpoint, API version, and the call status. An example is the following screen shot:
Following are the high-level steps to run your application; details for these steps can be found at Creating a Hello World Application Using the Azure Toolkit for Eclipse.
- Export your TwilioCloud WAR to the Azure approot folder.
- Modify startup.cmd to unzip your TwilioCloud WAR.
- Compile your application for the compute emulator.
- Start your deployment in the compute emulator.
- Open a browser, and run http://localhost:8080/TwilioCloud/callform.jsp.
- Enter values in the form, click Make this call, and then see the results in makecall.jsp.
When you are ready to deploy to Azure, recompile for deployment to the cloud, deploy to Azure, and run http://your_hosted_name.cloudapp.net/TwilioCloud/callform.jsp in the browser (substitute your value for your_hosted_name).
This code was provided to show you basic functionality using Twilio in Java on Azure. Before deploying to Azure in production, you may want to add more error handling or other features. For example:
- Instead of using a web form, you could use Azure storage blobs or SQL Database to store phone numbers and call text. For information about using Azure storage blobs in Java, see How to Use the Blob Storage Service from Java. For information about using SQL Database in Java, see Using SQL Database in Java.
- You could use RoleEnvironment.getConfigurationSettings to retrieve the Twilio account ID and authentication token from your deployment's configuration settings, instead of hard-coding the values in makecall.jsp. For information about the RoleEnvironment class, see Using the Azure Service Runtime Library in JSP and the Azure Service Runtime package documentation at http://dl.windowsazure.com/javadoc.
- The makecall.jsp code assigns a Twilio-provided URL, http://twimlets.com/message, to the Url variable. This URL provides a Twilio Markup Language (TwiML) response that informs Twilio how to proceed with the call. For example, the TwiML that is returned can contain a <Say> verb that results in text being spoken to the call recipient. Instead of using the Twilio-provided URL, you could build your own service to respond to Twilio's request; for more information, see How to Use Twilio for Voice and SMS Capabilities in Java. More information about TwiML can be found at http://www.twilio.com/docs/api/twiml, and more information about <Say> and other Twilio verbs can be found at http://www.twilio.com/docs/api/twiml/say.
- Read the Twilio security guidelines at https://www.twilio.com/docs/security.
For additional information about Twilio, see https://www.twilio.com/docs.