你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Bot Connector API 实现特定于通道的功能

某些频道提供的功能无法仅使用 邮件文本和附件来实现。 若要实现特定于通道的功能,可以将本机元数据传递到 Activity 对象的 channelData 属性中的通道。 例如,机器人可使用 channelData 属性来指示 Telegram 发送贴纸或指示 Office365 发送电子邮件。

本文介绍如何使用消息活动的 channelData 属性来实现此通道特定的功能:

Channel 功能
电子邮件 发送和接收包含正文、主题和重要性元数据的电子邮件
Slack 发送全保真的 Slack 消息
Facebook 本机发送 Facebook 通知
Telegram 执行 Telegram 特定操作,例如共享语音备忘录或贴纸

注意

Activity 对象的 channelData 属性的值是一个 JSON 对象。 JSON 对象的结构将根据所执行的通道和功能而变化,如下所述。

创建自定义电子邮件

要创建电子邮件,请将 Activity 对象的 channelData 属性设置为包含以下属性的 JSON 对象:

{
    "type": "ActivityTypes.Message",
    "locale": "en-Us",
    "channelID": "email",
    "fromName": { "id": "mybot@mydomain.com", "name": "My bot"},
    "recipientName": { "id": "joe@otherdomain.com", "name": "Joe Doe"},
    "conversation": { "id": "123123123123", "topic": "awesome chat" },
    "channelData":
    {
        "htmlBody": "<html><body style = \"font-family: Calibri; font-size: 11pt;\" >This is more than awesome.</body></html>",
        "importance": "high",
        "ccRecipients": "Yasemin@adatum.com;Temel@adventure-works.com",
    }
}

此代码片段显示了自定义“电子邮件”消息的 channelData 属性示例。

"channelData":
{
    "htmlBody": "<html><body style = \"font-family: Calibri; font-size: 11pt;\" >This is more than awesome.</body></html>",
    "importance": "high",
    "ccRecipients": "Yasemin@adatum.com;Temel@adventure-works.com"
}

创建全保真的 Slack 消息

要创建全保真的 Slack 消息,请将 Activity 对象的 channelData 属性设置为 JSON 对象,该对象指定 Slack 消息Slack 附件 和/或 Slack 按钮

注意

要使用户能在 Slack 消息中使用按钮,必须在将机器人连接到 Slack 通道时启用“交互式消息” 。

此代码片段显示了自定义 Slack 消息的 channelData 属性示例。

"channelData": {
   "text": "Now back in stock! :tada:",
   "attachments": [
        {
            "title": "The Further Adventures of Slackbot",
            "author_name": "Stanford S. Strickland",
            "author_icon": "https://api.slack.com/img/api/homepage_custom_integrations-2x.png",
            "image_url": "http://i.imgur.com/OJkaVOI.jpg?1"
        },
        {
            "fields": [
                {
                    "title": "Volume",
                    "value": "1",
                    "short": true
                },
                {
                    "title": "Issue",
                    "value": "3",
                    "short": true
                }
            ]
        },
        {
            "title": "Synopsis",
            "text": "After @episod pushed exciting changes to a devious new branch back in Issue 1, Slackbot notifies @don about an unexpected deploy..."
        },
        {
            "fallback": "Would you recommend it to customers?",
            "title": "Would you recommend it to customers?",
            "callback_id": "comic_1234_xyz",
            "color": "#3AA3E3",
            "attachment_type": "default",
            "actions": [
                {
                    "name": "recommend",
                    "text": "Recommend",
                    "type": "button",
                    "value": "recommend"
                },
                {
                    "name": "no",
                    "text": "No",
                    "type": "button",
                    "value": "bad"
                }
            ]
        }
    ]
}

当用户单击 Slack 消息中的按钮时,机器人将收到一条答复消息,其中 channelData 属性中填充了 payload JSON 对象。 payload 对象指定原始消息的内容,标识单击的按钮,并标识单击该按钮的用户。

此代码片段显示了当用户单击 Slack 消息中的按钮时,机器人收到的消息中 channelData 属性的示例。

"channelData": {
    "payload": {
        "actions": [
            {
                "name": "recommend",
                "value": "yes"
            }
        ],
        //...
        "original_message": "{...}",
        "response_url": "https://hooks.slack.com/actions/..."
    }
}

机器人可以按常规方式回复此消息,也可以将其答复直接发布到由 payload 对象的 response_url 属性指定的终结点。 要了解何时以及如何将答复发布到 response_url,请参阅 Slack 按钮

创建 Facebook 通知

要创建 Facebook 通知,请将 Activity 对象的 channelData 属性设置为指定以下属性的 JSON 对象:

properties 说明
notification_type 通知 (的类型,例如 ,常规SILENT_PUSHNO_PUSH) 。
attachment 附件(用于指定图像、视频或其他多媒体类型)或模板化附件(如收据)。

注意

要详细了解 notification_type 属性和attachment 属性的格式和内容,请参阅 Facebook API 文档

此代码片段显示了Facebook 收据附件的 channelData 属性示例。

"channelData": {
    "notification_type": "NO_PUSH",
    "attachment": {
        "type": "template",
        "payload": {
            "template_type": "receipt",
            //...
        }
    }
}

创建 Telegram 消息

要创建实现 Telegram 特定的操作(例如共享语音备注或贴纸)的消息,请将 Activity 对象的 channelData 属性设置为指定以下属性的 JSON 对象:

properties 说明
method 要调用的 Telegram 机器人 API 方法。
parameters 已指定的方法的参数。

支持以下 Telegram 方法:

  • answerInlineQuery
  • editMessageCaption
  • editMessageReplyMarkup
  • editMessageText
  • forwardMessage
  • banChatMember
  • sendAudio
  • sendChatAction
  • sendContact
  • sendDocument
  • sendLocation
  • sendMessage
  • sendPhoto
  • sendSticker
  • sendVenue
  • sendVideo
  • sendVoice
  • unbanChatMember

要详细了解这些 Telegram 方法及其参数,请参阅 Telegram 机器人 API 文档

注意

  • chat_id 参数可通用于所有 Telegram 方法。 如果未指定 chat_id 为参数,框架将为你提供 ID。
  • 不要内联传递文件内容,而是使用 URL 和媒体类型指定文件,如下面的示例所示。
  • 在机器人从 Telegram 通道接收的每条消息中,channelData 属性都将包含机器人之前发送的消息。

此代码片段显示了一个指定单个 Telegram 方法的 channelData 属性的示例。

"channelData": {
    "method": "sendSticker",
    "parameters": {
        "sticker": {
            "url": "https://domain.com/path/gif",
            "mediaType": "image/gif",
        }
    }
}

此代码片段显示了一个指定一组 Telegram 方法的 channelData 属性的示例。

"channelData": [
    {
        "method": "sendSticker",
        "parameters": {
            "sticker": {
                "url": "https://domain.com/path/gif",
                "mediaType": "image/gif",
            }
        }
    },
    {
        "method": "sendMessage",
        "parameters": {
            "text": "<b>This message is HTML formatted.</b>",
            "parse_mode": "HTML"
        }
    }
]

其他资源