链接展开Link unfurling

重要

本节中的代码示例基于 Bot 框架 SDK 的4.6 和更高版本。The code samples in this section are based on 4.6 and later versions of the Bot Framework SDK. 如果要查找早期版本的文档,请参阅文档的资源文件夹中的 "消息扩展-V3 SDK " 一节。If you're looking for documentation for earlier versions, see the Messaging Extensions - v3 SDK section in the Resources folder of the documentation.

备注

目前,移动客户端上不支持链接 unfurling。Currently, Link unfurling is not supported on Mobile clients.

通过链接 unfurling invoke 如果将特定域的 url 粘贴到撰写邮件区域中,应用可以注册接收活动。With link unfurling your app can register to receive an invoke activity when URLs with a particular domain are pasted into the compose message area. invoke将包含粘贴到撰写邮件区域中的完整 URL,您可以使用用户可以unfurl的卡片进行响应,并提供其他信息或操作。The invoke will contain the full URL that was pasted into the compose message area, and you can respond with a card the user can unfurl, providing additional information or actions. 此操作的工作方式与搜索命令非常相似,URL 充当搜索词。This works very similarly to a search command, with the URL serving as the search term.

Azure DevOps 消息扩展使用 link unfurling 查找粘贴到指向工作项的撰写邮件区域中的 Url。The Azure DevOps messaging extension uses link unfurling to look for URLs pasted into the compose message area pointing to a work item. 在下面的屏幕截图中,用户已将 Azure DevOps 中的工作项的 URL 粘贴到邮件扩展已解析为卡片。In the screenshot below, a user has pasted in a URL for a work item in Azure DevOps which the messaging extension has resolved into a card.

Link unfurling 的示例

为此,您需要将新的 messageHandlers 数组添加到 composeExtensions 应用程序清单 JSON 的部分。To do this you'll add a new messageHandlers array to the composeExtensions section of your app manifest JSON. 您可以使用应用程序 Studio 的帮助或手动执行此操作。You can either do so with the help of App Studio, or manually. 例如,域列表可以包含通配符 *.example.comDomain listings can include wildcards, for example *.example.com. 这与域的一段完全匹配;如果需要匹配, a.b.example.com 请使用 *.*.example.comThis matches exactly one segment of the domain; if you need to match a.b.example.com then use *.*.example.com.

使用 App StudioUsing App Studio

  1. 在应用程序 Studio 中,在 "清单编辑器" 选项卡上,加载您的应用程序清单。In App Studio, on the Manifest editor tab, load your app manifest.
  2. 在 "邮件扩展" 页上,在 "邮件处理程序" 部分添加要查找的域,如下面的屏幕截图所示。On the Messaging Extension page, add the domain you want to look for in the Message handlers section as in the screenshot below.

应用程序 Studio 中的 "邮件处理程序" 部分

手动Manually

若要使您的邮件扩展能够与链接进行交互,这种方法首先需要将该 messageHandlers 数组添加到应用程序清单中,如以下示例中所示。To enable your messaging extension to interact with links this way you'll first need to add the messageHandlers array to your app manifest as in the example below. 本示例不是完整的清单,请参阅清单参考,了解完整的清单示例。This example is not the complete manifest, see manifest reference for a complete manifest example.

...
"composeExtensions": [
  {
    "botId": "abc123456-ab12-ab12-ab12-abcdef123456",
    "messageHandlers": [
      {
        "type": "link",
        "value": {
          "domains": [
            "*.trackeddomain.com"
          ]
        }
      }
    ]
  }
],
...

在添加了要侦听应用程序清单的域之后,您需要更新 web 服务代码以处理调用请求。Once you've added the domain to listen on to the app manifest, you'll need to update your web service code to handle the invoke request. 使用您收到的 URL 搜索您的服务并创建卡片响应。Use the URL you receive to search your service and create a card response. 如果使用多个卡进行响应,将仅使用第一个。If you respond with more than one card, only the first will be used.

我们支持以下卡类型:We support the following card types:

有关概述,请参阅什么是卡片See What are cards for an overview.

protected override async Task<MessagingExtensionResponse> OnTeamsAppBasedLinkQueryAsync(ITurnContext<IInvokeActivity> turnContext, AppBasedLinkQuery query, CancellationToken cancellationToken)
{
    //You'll use the query.link value to search your service and create a card response
    var card = new HeroCard
    {
        Title = "Hero Card",
        Text = query.Url,
        Images = new List<CardImage> { new CardImage("https://raw.githubusercontent.com/microsoft/botframework-sdk/master/icon.png") },
    };

    var attachments = new MessagingExtensionAttachment(HeroCard.ContentType, null, card);
    var result = new MessagingExtensionResult(AttachmentLayoutTypes.List, "result", new[] { attachments }, null, "test unfurl");

    return new MessagingExtensionResponse(result);
}