如何检测/侦听来自自适应卡的动作

时间:2018-11-16 22:57:59

标签: c# azure botframework bots luis

我正在使用botframework(企业bot模板)和LUIS.ai:

我的问题是,当我填写一张自定义自适应卡(它具有三个文本输入字段)并单击“提交”时,我收到以下消息:“对不起,我无法提供帮助。”我可以在模拟器中看到``提交''按钮回发输入的值,但是我不确定如何监听按钮动作,我的想法是当调用动作ID时我必须监听我不太确定该怎么做。

下面是调用对话框的代码:

    public static IMessageActivity SendTicketFormCard(ITurnContext turnContext, dynamic data)
    {
        var response = turnContext.Activity.CreateReply();
        var introcard = File.ReadAllText(@".\dialogs\main\resources\Ticket_Fields.json");

        response.Attachments = new List<Attachment>();
        response.Attachments.Add(new Attachment()
        {
            ContentType = "application/vnd.microsoft.card.adaptive",
            Content = JsonConvert.DeserializeObject(introcard),
        });

        return response;
    }

JSON对话框如下所示:

{
  "type": "AdaptiveCard",
  "body": [
    {
      "type": "TextBlock",
      "horizontalAlignment": "Center",
      "size": "Medium",
      "weight": "Bolder",
      "color": "Dark",
      "text": "Search Ticket"
    },
    {
      "type": "TextBlock",
      "id": "94358428-5ef2-43a5-9056-d3cac1abfabd",
      "text": "Ticket ID:",
      "maxLines": 1
    },
    {
      "type": "Input.Text",
      "id": "68e1e180-4cdc-4ad6-bb8f-743554f1f58b",
      "placeholder": "Ticket ID (required)",
      "maxLength": 10
    },
    {
      "type": "TextBlock",
      "id": "2da1df9d-7f61-4e5c-9ff9-7aba2c5b306b",
      "text": "Summary:",
      "maxLines": 1
    },
    {
      "type": "Input.Text",
      "id": "403979a3-ccba-4baa-a885-2abca754cc69",
      "placeholder": "Summary (optional)",
      "maxLength": 250,
      "isMultiline": true
    },
    {
      "type": "TextBlock",
      "id": "a25464c7-07ea-4270-995f-5e57b783b52d",
      "text": "Status:",
      "maxLines": 1
    },
    {
      "type": "Input.Text",
      "id": "7794d725-feb5-4516-9786-d18684892106",
      "placeholder": "Status (optional)",
      "maxLength": 30
    }
  ],
  "actions": [
    {
      "type": "Action.Submit",
      "id": "783fe2e4-4056-449e-8cc6-5dc9c406222a",
      "title": "Search"
    }
  ],
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "version": "1.0"
}

2 个答案:

答案 0 :(得分:1)

无论何时单击自适应卡中的Submit(提交)按钮,通道都会在通道数据对象中发送回“ postback”:“ true”令牌。与此相关的是,在“提交”按钮上配置的数据将在json的Value对象中发送。您需要解析该值以获取需要采取进一步措施的详细信息。我已经发表了关于这个的帖子。请在以下位置查看 BotFramework Reactive Adaptive Cards: How to Read Data on Action Buttons in Adaptive Cards

答案 1 :(得分:0)

消息“很抱歉,我无法为此提供帮助。”是对企业模板的MainDialog类中“无” LUIS意图的响应。您可以在MainStrings.resx中找到该消息。

自适应卡的提交动作将没有文本的消息发送给机器人,然后LUIS尝试使用消息的文本解释该消息的意图,但没有文本。提交的数据将包含在活动的Value属性中。要从Value属性读取数据,您需要使用卡的输入字段的ID作为键。

private const string TICKETFIELD = "68e1e180-4cdc-4ad6-bb8f-743554f1f58b";
private const string SUMMARYFIELD = "403979a3-ccba-4baa-a885-2abca754cc69";
private const string STATUSFIELD = "7794d725-feb5-4516-9786-d18684892106";

您将要检查传入的消息以查看它们是否没有文本,然后像执行可能的提交操作一样进行响应。一旦LUIS已经确定意图为“无”,就可以执行此操作,但是您也可以在将消息发送给LUIS之前进行检查。转到MainDialog的RouteAsync方法并将所有现有代码包装在else块中,然后可以将响应提交操作的代码放在if块中:

protected override async Task RouteAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
{
    if (string.IsNullOrEmpty(activity.Text))
    {
        dynamic value = dc.Context.Activity.Value;

        await turnContext.SendActivityAsync($"Ticket = {value[TICKETFIELD]}, Summary = {value[SUMMARYFIELD]}, Status = {value[STATUSFIELD]}");
    }
    else
    {
        // All the code that was already there
    }
}