您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

如何在 Azure 的 Java 应用程序中使用 Twilio 发起电话呼叫How to Make a Phone Call Using Twilio in a Java Application on Azure

以下示例演示了如何使用 Twilio 从 Azure 中托管的网页发起呼叫。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 screenshot.

使用 Twilio 和 Java 的 Azure 呼叫窗体

需要执行以下操作来使用本主题中的代码:You'll need to do the following to use the code in this topic:

  1. 获取 Twilio 帐户和身份验证令牌。Acquire a Twilio account and authentication token. 若要开始 Twilio,请评估价格 https://www.twilio.com/pricingTo get started with Twilio, evaluate pricing at https://www.twilio.com/pricing. 你可以注册 https://www.twilio.com/try-twilioYou can sign up at https://www.twilio.com/try-twilio. 有关 Twilio 提供的 API 的信息,请参阅 https://www.twilio.com/apiFor information about the API provided by Twilio, see https://www.twilio.com/api.
  2. 获取 Twilio JAR。Obtain the Twilio JAR. https://github.com/twilio/twilio-java 中,你可以下载 GitHub 源并创建自己的 jar,或下载具有或不具有依赖关系) 的预建 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). 本主题中的代码使用预建的 TwilioJava-3.3.8-with-dependencies JAR 编写。The code in this topic was written using the pre-built TwilioJava-3.3.8-with-dependencies JAR.
  3. 将 JAR 添加到 Java 生成路径。Add the JAR to your Java build path.
  4. 如果使用 Eclipse 创建此 Java 应用程序,请使用 Eclipse 的部署程序集功能将 Twilio JAR 包含在应用程序部署文件 (WAR) 中。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. 如果不使用 Eclipse 创建此 Java 应用程序,请确保将 Twilio JAR 包含在与 Java 应用程序相同的 Azure 角色中,并将其添加到应用程序的类路径下。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.
  5. 确保 cacerts 密钥库包含带有 MD5 指纹 67:CB:9D:C0:13:24:8A:82:9B:B2:17:1E:D1:1B:EC:D4(序列号为 35:DE:F4:CF,SHA1 指纹为 D2:32:09:AD:23:D3:14:23:21:74:E4:0D:7F:9D:62:13:97:86:63:3A)的 Equifax 安全证书颁发机构证书。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). 这是服务 (CA) 证书的证书颁发机构 https://api.twilio.com ,在使用 Twilio api 时将调用该证书颁发机构。This is the certificate authority (CA) certificate for the https://api.twilio.com service, which is called when you use Twilio APIs.

此外,如果不使用 Eclipse,我们强烈建议先熟悉使用用于 Eclipse 的 Azure 工具包创建 Hello World 应用程序中的信息或熟悉用于在 Azure 中托管 Java 应用程序的其他方法。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.

创建用于发起呼叫的 Web 窗体Create a web form for making a call

以下代码演示了如何创建 Web 窗体来检索用于发起呼叫的用户数据。The following code shows how to create a web form to retrieve user data for making a call. 在本示例中,将创建名为 TwilioCloud 的新的动态 Web 项目,并添加 callform.jsp 作为 JSP 文件。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" "https://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>

创建用于发起呼叫的代码Create the code to make the call

以下代码在用户填写 callform.jsp 显示的窗体后调用,用于创建呼叫消息并生成呼叫。The following code, which is called when the user completes the form displayed by callform.jsp, creates the call message and generates the call. 在本示例中,JSP 文件被命名为 makecall.jsp 并添加到 TwilioCloud 项目。For purposes of this example, the JSP file is named makecall.jsp and was added to the TwilioCloud project. (使用 Twilio 帐户和身份验证令牌,而不是分配给以下代码中的 accountSIDauthToken 的占位符值。)(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" "https://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="https://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>

除了发起呼叫外,makecall.jsp 还可显示 Twilio 终结点、API 版本和呼叫状态。In addition to making the call, makecall.jsp displays the Twilio endpoint, API version, and the call status. 下面的屏幕截图是一个示例:An example is the following screenshot:

使用 Twilio 和 Java 的 Azure 呼叫响应

运行应用程序Run the application

下面是运行应用程序的概要步骤;这些步骤的详细信息可在使用用于 Eclipse 的 Azure 工具包创建 Hello World 应用程序中找到。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.

  1. 将 TwilioCloud WAR 导出到 Azure approot 文件夹。Export your TwilioCloud WAR to the Azure approot folder.
  2. 修改 startup.cmd 以解压缩 TwilioCloud WAR。Modify startup.cmd to unzip your TwilioCloud WAR.
  3. 针对计算模拟器编译应用程序。Compile your application for the compute emulator.
  4. 在计算模拟器中启动部署。Start your deployment in the compute emulator.
  5. 打开浏览器,并运行 http://localhost:8080/TwilioCloud/callform.jspOpen a browser, and run http://localhost:8080/TwilioCloud/callform.jsp.
  6. 在窗体中输入值,单击“发起此呼叫”,并查看 makecall.jsp 中的结果。Enter values in the form, click Make this call, and then see the results in makecall.jsp.

准备好部署到 Azure 之后,请针对云部署重新进行编译,部署到 Azure,然后在浏览器中运行 http://your_hosted_name.cloudapp.net/TwilioCloud/callform.jsp(将 your_hosted_name 替换为自己的值)。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).

后续步骤Next steps

提供此代码是为了演示在 Azure 上通过 Java 使用 Twilio 的基本功能。This code was provided to show you basic functionality using Twilio in Java on Azure. 在生产中部署到 Azure 之前,可能希望添加更多错误处理或其他功能。Before deploying to Azure in production, you may want to add more error handling or other features. 例如:For example:

有关 Twilio 的其他信息,请参阅 https://www.twilio.com/docsFor additional information about Twilio, see https://www.twilio.com/docs.

另请参阅See Also