提交不适用于 Azure Bot 发布的 Teams 中的自适应卡

Submit not working for Adaptive Card in Teams posted by Azure Bot

我有一张自适应卡 post 从 Azure 机器人发送到团队,要求用户为显示的字段输入值。当用户点击提交按钮时,没有任何反应。然而在 WebChat 或 Adaptive Card Designer 中,它工作得很好。当我使用 post 从逻辑应用程序到团队的自适应卡时,我什至用来工作。

这是自适应卡片:

{
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "type": "AdaptiveCard",
    "version": "1.0",
    "body": [
        {
            "type": "TextBlock",
            "size": "medium",
            "weight": "bolder",
            "text": "Start VM"
        },
        {
            "type": "TextBlock",
            "text": "Enter the details of the Virtual Machine to restart",
            "wrap": true
        },
        {
            "type": "ColumnSet",
            "columns": [
                {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                        {
                            "type": "TextBlock",
                            "text": "VM Name",
                            "wrap": true
                        }
                    ]
                },
                {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                        {
                            "type": "Input.Text",
                            "placeholder": "e.g. servername",
                            "id": "vmName"
                        }
                    ]
                }
            ]
        },
        {
            "type": "ColumnSet",
            "columns": [
                {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                        {
                            "type": "TextBlock",
                            "text": "VM Resource Group",
                            "wrap": true
                        }
                    ]
                },
                {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                        {
                            "type": "Input.Text",
                            "placeholder": "e.g. resourcegroup",
                            "id": "vmResourceGroup"
                        }
                    ]
                }
            ]
        },
        {
            "type": "ColumnSet",
            "columns": [
                {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                        {
                            "type": "TextBlock",
                            "text": "Username",
                            "wrap": true
                        }
                    ]
                },
                {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                        {
                            "type": "Input.Text",
                            "placeholder": "e.g. joe.bloggs@contoso.com",
                            "id": "username"
                        }
                    ]
                }
            ]
        },
        {
            "type": "ColumnSet",
            "columns": [
                {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                        {
                            "type": "TextBlock",
                            "text": "ServiceNow SysID for Change Record",
                            "wrap": true
                        }
                    ]
                },
                {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                        {
                            "type": "Input.Text",
                            "placeholder": "e.g. 11cef313db4ce0d012d9147a3a961909",
                            "id": "sysId"
                        }
                    ]
                }
            ]
        }
    ],
    "actions": [  
        {  
            "type": "Action.Submit",  
            "title": "Submit"
        }  
    ]
}

Azure Bot 中构造自适应卡的 C# 代码如下:

private Attachment CreateAdaptiveCardAttachment()
{
    var cardResourcePath = "EchoBot.Cards.startVMCard.json";

    using (var stream = GetType().Assembly.GetManifestResourceStream(cardResourcePath))
    {
        using (var reader = new StreamReader(stream))
        {
            var adaptiveCard = reader.ReadToEnd();
            return new Attachment()
            {
                ContentType = "application/vnd.microsoft.card.adaptive",
                Content = JsonConvert.DeserializeObject(adaptiveCard),
            };
        }
    }
}

发送自适应卡片的 Azure Bot 代码如下:

var startVMCard = CreateAdaptiveCardAttachment();
var response = MessageFactory.Attachment(startVMCard);
await turnContext.SendActivityAsync(response, cancellationToken);

提前致谢!

首先感谢您联系我们。

我已经通过并测试了您的自适应卡 json 和您实施的方法。

能够获取“adaptivecards.io/designer”中的值,

此外,我也在 Microsoft Teams 中尝试过,能够获取代码“turnContext”中的值

因此,在“提交”按钮操作中,您必须实现代码以在 Bot 中显示值。

请通读 document,您将获得有关卡片以及如何实施卡片的完整知识。

好的 - 正在尝试解开这个谜团。在 Visual Studio 中,我可以从 Azure 中的 Bot 查看流日志。

通过远程连接到 Bot 的 Bot Framework Emulator 测试 Bot 时,它运行良好。单击日志中的提交按钮后,我看到以下内容:

Application:2020-12-23 06:41:39.456 +00:00 [Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request starting HTTP/1.1 POST https://startvmbot.azurewebsites.net/api/messages application/json 678
Application:2020-12-23 06:41:39.456 +00:00 [Information] Microsoft.AspNetCore.Routing.EndpointMiddleware: Executing endpoint 'StartVMBot.Controllers.BotController.PostAsync (startvmbot)'
Application:2020-12-23 06:41:39.456 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Route matched with {action = "Post", controller = "Bot"}. Executing controller action with signature System.Threading.Tasks.Task PostAsync() on controller StartVMBot.Controllers.BotController (startvmbot).
Application:2020-12-23 06:41:39.456 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Executing action method StartVMBot.Controllers.BotController.PostAsync (startvmbot) - Validation state: Valid
Application:2020-12-23 06:41:39.460 +00:00 [Information] Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter: Received an incoming activity.  ActivityId: e920c600-44e9-11eb-aa2c-f5a359761b0e
Application:2020-12-23 06:41:39.657 +00:00 [Information] Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter: Sending activity.  ReplyToId: e920c600-44e9-11eb-aa2c-f5a359761b0e
Application:2020-12-23 06:41:39.658 +00:00 [Information] Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter: GetTokenAsync: Acquired token using ADAL in 0.
Application:2020-12-23 06:41:40.128 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Executed action method StartVMBot.Controllers.BotController.PostAsync (startvmbot), returned result Microsoft.AspNetCore.Mvc.EmptyResult in 671.7897ms.
Application:2020-12-23 06:41:40.128 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Executed action StartVMBot.Controllers.BotController.PostAsync (startvmbot) in 672.1344ms
Application:2020-12-23 06:41:40.128 +00:00 [Information] Microsoft.AspNetCore.Routing.EndpointMiddleware: Executed endpoint 'StartVMBot.Controllers.BotController.PostAsync (startvmbot)'
Application:2020-12-23 06:41:40.129 +00:00 [Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request finished in 673.416ms 200

这是 Bot Framework Emulator 中的结果

通过 Teams 测试 Bot 时它不起作用。单击日志中的提交按钮后,我看到以下内容:

Application:2020-12-23 06:39:56.705 +00:00 [Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request starting HTTP/1.1 POST https://startvmbot.azurewebsites.net/api/messages application/json; charset=utf-8 1136
Application:2020-12-23 06:39:56.705 +00:00 [Information] Microsoft.AspNetCore.Routing.EndpointMiddleware: Executing endpoint 'StartVMBot.Controllers.BotController.PostAsync (startvmbot)'
Application:2020-12-23 06:39:56.705 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Route matched with {action = "Post", controller = "Bot"}. Executing controller action with signature System.Threading.Tasks.Task PostAsync() on controller StartVMBot.Controllers.BotController (startvmbot).
Application:2020-12-23 06:39:56.705 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Executing action method StartVMBot.Controllers.BotController.PostAsync (startvmbot) - Validation state: Valid
Application:2020-12-23 06:39:56.707 +00:00 [Information] Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter: Received an incoming activity.  ActivityId: f:6238602817490478374
Application:2020-12-23 06:39:56.708 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Executed action method StartVMBot.Controllers.BotController.PostAsync (startvmbot), returned result Microsoft.AspNetCore.Mvc.EmptyResult in 2.2726ms.
Application:2020-12-23 06:39:56.708 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Executed action StartVMBot.Controllers.BotController.PostAsync (startvmbot) in 2.5461ms
Application:2020-12-23 06:39:56.708 +00:00 [Information] Microsoft.AspNetCore.Routing.EndpointMiddleware: Executed endpoint 'StartVMBot.Controllers.BotController.PostAsync (startvmbot)'
Application:2020-12-23 06:39:56.708 +00:00 [Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request finished in 3.6941ms 200 

这是 Teams 中的结果:

我会继续寻找,但如果有人有任何想法,我将不胜感激!

我发现了问题。

似乎 Teams 使用的是 Skype Framework 3.0,并且没有 postBack 方法。自适应卡片提交不包含 ChannelData 中的 postBack。使用 Framework Emulator 提交的自适应卡片在 ChannelData 中包含 postBack

我添加了以下代码来解决这个问题:

var channelData = JObject.Parse(turnContext.Activity.ChannelData.ToString());
JObject activity = JObject.Parse(turnContext.Activity.Value.ToString());

if (channelData.ContainsKey("postBack") || activity.HasValues == true)
{
    ...
}