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

如何在 Azure 的 Web 角色中使用 Twilio 发起电话呼叫How to make a phone call using Twilio in a web role on Azure

本指南演示如何使用 Twilio 从 Azure 中托管的网页发起呼叫。This guide demonstrates how to use Twilio to make a call from a web page hosted in Azure. 生成的应用程序提示用户使用给定的号码和消息进行呼叫,如下面的屏幕截图中所示。The resulting application prompts the user to make a call with the given number and message, as shown in the following screenshot.

使用 Twilio 和 ASP.NET 的 Azure 呼叫窗体

先决条件Prerequisites

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

  1. Twilio 控制台获取 Twilio 帐户和身份验证令牌。Acquire a Twilio account and authentication token from the Twilio Console. 若要开始 Twilio,请在上注册 https://www.twilio.com/try-twilioTo get started with Twilio, sign up at https://www.twilio.com/try-twilio. 你可以在中评估定价 https://www.twilio.com/pricingYou can evaluate pricing at https://www.twilio.com/pricing. 有关 Twilio 提供的 API 的信息,请参阅 https://www.twilio.com/voice/apiFor information about the API provided by Twilio, see https://www.twilio.com/voice/api.
  2. Twilio .NET 库 添加到 Web 角色。Add the Twilio .NET library to your web role. 请参阅本主题后面的 将 Twilio 库添加到 web 角色项目See To add the Twilio libraries to your web role project, later in this topic.

应熟悉如何在 Azure 中创建基本 Web 角色You should be familiar with creating a basic Web Role on Azure.

如何:创建用于发起呼叫的 Web 窗体How to: Create a web form for making a call

向你的 Web 角色项目中添加 Twilio 库:To add the Twilio libraries to your web role project:

  1. 在 Visual Studio 中打开解决方案。Open your solution in Visual Studio.
  2. 右键单击“引用”。Right-click References.
  3. 单击 " 管理 NuGet 包"。Click Manage NuGet Packages.
  4. 单击“联机”。Click Online.
  5. 在联机搜索框中,键入 twilioIn the search online box, type twilio.
  6. 单击 Twilio 程序包对应的“安装”。Click Install on the Twilio package.

以下代码演示了如何创建 Web 窗体来检索用于发起呼叫的用户数据。The following code shows how to create a web form to retrieve user data for making a call. 在此示例中,将创建一个名为 TwilioCloud 的 ASP.NET Web 角色。In this example, an ASP.NET Web Role named TwilioCloud is created.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
    AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="WebRole1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>
        <asp:BulletedList ID="varDisplay" runat="server" BulletStyle="NotSet">
        </asp:BulletedList>
    </div>
    <div>
        <p>Fill in all fields and click <b>Make this call</b>.</p>
        <div>
            To:<br /><asp:TextBox ID="toNumber" runat="server" /><br /><br />
            Message:<br /><asp:TextBox ID="message" runat="server" /><br /><br />
            <asp:Button ID="callpage" runat="server" Text="Make this call"
                onclick="callpage_Click" />
        </div>
    </div>
</asp:Content>

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

在用户完成窗体后将调用以下代码,以创建呼叫消息并生成呼叫。The following code, which is called when the user completes the form, creates the call message and generates the call. 在此示例中,该代码在窗体上按钮的 Onclick 事件处理程序中运行。In this example, the code is run in the onclick event handler of the button on the form. (使用 Twilio 帐户和身份验证令牌,而不是分配给以下代码中的 accountSIDauthToken 的占位符值。)(Use your Twilio account and authentication token instead of the placeholder values assigned to accountSID and authToken in the code below.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Twilio;
using Twilio.Http;
using Twilio.Types;
using Twilio.Rest.Api.V2010;

namespace WebRole1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void callpage_Click(object sender, EventArgs e)
        {
            // Call processing happens here.

            // Use your account SID and authentication token instead of
            // the placeholders shown here.
            var accountSID = "ACNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN";
            var authToken =  "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN";

            // Instantiate an instance of the Twilio client.
            TwilioClient.Init(accountSID, authToken);

            // Retrieve the account, used later to retrieve the
            var account = AccountResource.Fetch(accountSID);

            this.varDisplay.Items.Clear();

            if (this.toNumber.Text == "" || this.message.Text == "")
            {
                this.varDisplay.Items.Add(
                        "You must enter a phone number and a message.");
            }
            else
            {
                // Retrieve the values entered by the user.
                var to = PhoneNumber(this.toNumber.Text);
                var from = new PhoneNumber("+14155992671");
                var myMessage = this.message.Text;

                // Create a URL using the Twilio message and the user-entered
                // text. You must replace spaces in the user's text with '%20'
                // to make the text suitable for a URL.
                var url = $"https://twimlets.com/message?Message%5B0%5D={myMessage.Replace(" ", "%20")}";
                var twimlUri = new Uri(url);

                // Display the endpoint, API version, and the URL for the message.
                this.varDisplay.Items.Add($"Using Twilio endpoint {
                }");
                this.varDisplay.Items.Add($"Twilioclient API Version is {apiVersion}");
                this.varDisplay.Items.Add($"The URL is {url}");

                // Place the call.
                var call = CallResource.create(to, from, url: twimlUri);
                this.varDisplay.Items.Add("Call status: " + call.Status);
            }
        }
    }
}

将发起呼叫,并显示 Twilio 终结点、API 版本和呼叫状态。The call is made, and the Twilio endpoint, API version, and the call status are displayed. 以下屏幕截图显示了示例运行的输出。The following screenshot shows output from a sample run.

使用 Twilio 和 ASP.NET 的 Azure 呼叫响应

有关 TwiML 的详细信息,请参阅 https://www.twilio.com/docs/api/twimlMore information about TwiML can be found at https://www.twilio.com/docs/api/twiml. 有关 < > 其他 Twilio 谓词的详细信息,请参阅 https://www.twilio.com/docs/api/twiml/sayMore information about <Say> and other Twilio verbs can be found at https://www.twilio.com/docs/api/twiml/say.

后续步骤Next steps

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

另请参阅See also