你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用于 JavaScript 的 Azure CommunicationMessages REST 客户端库 - 版本 2.0.0
此包包含适用于 Azure 通信消息服务的 JavaScript SDK。
关键链接:
开始
当前支持的环境
先决条件
- 必须具有 Azure 订阅 才能使用此包。
- 现有的通信服务资源。 如果需要创建资源,可以使用 Azure 门户、Azure PowerShell或 Azure CLI。
- 请参阅 如何注册 whatsapp 业务帐户 & 创建频道,以便将 whatsapp 通道注册到通信服务资源。
安装 @azure-rest/communication-messages
包
使用 npm
安装适用于 JavaScript 的 Azure CommunicationMessages REST 客户端 REST 客户端库:
npm install @azure-rest/communication-messages
认证
可以从 Azure 门户中的通信服务资源获取密钥和/或连接字符串。 获得密钥后,可以使用以下任一方法进行身份验证:
使用连接字符串
import MessageClient, { MessagesServiceClient } from "@azure-rest/communication-messages";
const connectionString = `endpoint=https://<resource-name>.communication.azure.com/;accessKey=<Base64-Encoded-Key>`;
const client:MessagesServiceClient = MessageClient(connectionString);
使用 AzureKeyCredential
import { AzureKeyCredential } from "@azure/core-auth";
import MessageClient, { MessagesServiceClient } from "@azure-rest/communication-messages";
const endpoint = "https://<resource-name>.communication.azure.com";
const credential = new AzureKeyCredential("<Base64-Encoded-Key>");
const client:MessagesServiceClient = MessageClient(endpoint, credential);
使用 Azure Active Directory 托管标识
大多数示例中都使用客户端 API 密钥身份验证,但也可以使用 Azure 标识库通过 Azure Active Directory 进行身份验证。 若要使用如下所示的 DefaultAzureCredential 提供程序,或 Azure SDK 提供的其他凭据提供程序,请安装 @azure/identity
包:
npm install @azure/identity
@azure/identity
包提供了应用程序可用于执行此操作的各种凭据类型。
@azure/identity 自述文件提供了更多详细信息和示例来帮助你入门。
创建 DefaultAzureCredential 对象需要AZURE_CLIENT_SECRET、AZURE_CLIENT_ID和AZURE_TENANT_ID环境变量。
import { DefaultAzureCredential } from "@azure/identity";
import MessageClient, { MessagesServiceClient } from "@azure-rest/communication-messages";
const endpoint = "https://<resource-name>.communication.azure.com";
const credential = new DefaultAzureCredential();
const client:MessagesServiceClient = MessageClient(endpoint, credential);
使用 WhatsApp 通道发送模板消息
Note: Business always starts the conversation with a template message.
若要发送模板消息,需要将模板添加到 WhatsApp Bussiness 帐户。 有关 WhatsApp 模板的更多详细信息,请参阅 创建和管理模板。 在下面的示例中,我们使用
Template Name: sample_issue_resolution
Template Language: en_US
Template Body: "Hi {{1}}, were we able to solve the issue that you were facing?"
With Quick Action Button (Yes, No)
const nameValue:MessageTemplateValue = {
kind: "text",
name: "name",
text: "Arif"
};
const yesAction: MessageTemplateValue = {
kind: "quickAction",
name: "Yes",
payload: "Yes"
};
const noAction: MessageTemplateValue = {
kind: "quickAction",
name: "No",
payload: "No"
};
const templateBindings:MessageTemplateBindings = {
kind: "whatsApp",
body: [
{
refValue: "name"
}
],
buttons: [
{
subType: "quickReply",
refValue: "Yes"
},
{
subType: "quickReply",
refValue: "No"
}
]
};
const template:MessageTemplate = {
name: "sample_issue_resolution",
language: "en_US",
bindings: templateBindings,
values: [nameValue, yesAction, noAction]
};
const result = await client.path("/messages/notifications:send").post({
contentType: "application/json",
body: {
channelRegistrationId: "<Channel_Registration_Id>",
to: ["<to-phone-number-1>"],
kind: "template",
template: template
}
});
if (result.status === "202") {
const response:Send202Response = result as Send202Response;
response.body.receipts.forEach((receipt) => {
console.log("Message sent to:"+receipt.to+" with message id:"+receipt.messageId);
});
} else {
throw new Error("Failed to send message");
}
使用 WhatsApp 通道发送短信
Note: Business can't start a conversation with a text message. It needs to be user initiated.
const result = await client.path("/messages/notifications:send").post({
contentType: "application/json",
body: {
channelRegistrationId: "<Channel_Registration_Id>",
to: ["<to-phone-number-1>"],
kind: "text",
content: "Hello World!!"
}
});
if (result.status === "202") {
const response:Send202Response = result as Send202Response;
response.body.receipts.forEach((receipt) => {
console.log("Message sent to:"+receipt.to+" with message id:"+receipt.messageId);
});
} else {
throw new Error("Failed to send message");
}
使用 WhatsApp 通道发送媒体消息
Note: Business can't start a conversation with a media message. It needs to be user initiated.
const result = await client.path("/messages/notifications:send").post({
contentType: "application/json",
body: {
channelRegistrationId: "<Channel_Registration_Id>",
to: ["<to-phone-number-1>"],
kind: "image",
mediaUri: "https://<your-media-image-file>"
}
});
if (result.status === "202") {
const response:Send202Response = result as Send202Response;
response.body.receipts.forEach((receipt) => {
console.log("Message sent to:"+receipt.to+" with message id:"+receipt.messageId);
});
} else {
throw new Error("Failed to send message");
}
故障 排除
伐木
启用日志记录可能有助于发现有关故障的有用信息。 若要查看 HTTP 请求和响应的日志,请将 AZURE_LOG_LEVEL
环境变量设置为 info
。 或者,可以通过在 @azure/logger
中调用 setLogLevel
在运行时启用日志记录:
const { setLogLevel } = require("@azure/logger");
setLogLevel("info");
有关如何启用日志的更详细说明,可以查看 @azure/记录器包文档。
后续步骤
有关如何使用此库的详细示例,请查看 示例 目录。
贡献
若要参与此库,请阅读 贡献指南 了解有关如何生成和测试代码的详细信息。
相关项目
- 用于 Javascript 的 Azure SDK Microsoft