Azure Bot框架:如何使自适应卡在团队中工作?

时间:2020-09-24 20:29:47

标签: botframework azure-bot-service adaptive-cards microsoft-bot-framework

我正在尝试使用Microsoft Bot Framework创建一个Azure托管的机器人。我在Bot仿真器中一切正常,但是当我将机器人部署到Teams并尝试与之通信时,我遇到了麻烦。我能够向机器人发送消息并让它响应我,但是这是我所能做到的。 本质上,我只希望为用户提供一个问题和四个选项,并使他们能够单击一个选项。例如,如果向用户显示“进行选择”,则他们应该能够单击A卡,B卡,C卡或D卡。每张卡片都应将用户带到另一个问题,在该问题上会类似地显示新的单击选项-基本对话框树。

问题在于,无论我如何构造卡的JSON,单击选项时都无法使它们实际执行任何操作。我的初始对话框代码如下:

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Threading;
using System.Threading.Tasks;
using AdaptiveCards;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Choices;
using Microsoft.Bot.Schema;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace evos.ChatBot
{
    public class InitialDialog : ComponentDialog
    {

        public InitialDialog()
            : base(nameof(InitialDialog))
        {
            AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));

            AddDialog(new ADialog());
            AddDialog(new BDialog());
            AddDialog(new CDialog());
            AddDialog(new DDialog());

            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
            {
                InitialCardChoiceStepAsync,
                StartSelectionStepAsync,
            }));

            InitialDialogId = nameof(WaterfallDialog);
        }

        private async Task<DialogTurnResult> InitialCardChoiceStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var filePath = Path.Combine(".", "Cards", "InitialCard.json");
            var adaptiveCardJson = File.ReadAllText(filePath);

            return await stepContext.PromptAsync(
                nameof(ChoicePrompt),
                new PromptOptions()
                {
                    Prompt = (Activity)MessageFactory.Attachment(new Attachment
                    {
                        ContentType = AdaptiveCard.ContentType,
                        Content = JsonConvert.DeserializeObject(adaptiveCardJson),
                    }),
                },
                cancellationToken);
        }

        private async Task<DialogTurnResult> StartSelectionStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            switch (((FoundChoice)stepContext.Result).Value)
            {
                case "A":
                    return await stepContext.BeginDialogAsync(nameof(ADialog), null, cancellationToken);
                case "B":
                    return await stepContext.BeginDialogAsync(nameof(BDialog), null, cancellationToken);
                case "C":
                    return await stepContext.BeginDialogAsync(nameof(CDialog), null, cancellationToken);
                case "D":
                    return await stepContext.BeginDialogAsync(nameof(DDialog), null, cancellationToken);
                default:
                    return await stepContext.EndDialogAsync(null, cancellationToken);
            }
        }
    }
}

现在,InitialCode.json看起来像这样:

{
  "type": "AdaptiveCard",
  "body": [
    {
      "type": "TextBlock",
      "size": "smaller",
      "weight": "bolder",
      "text": "Please make a selection:"
    },
    {
      "type": "ActionSet",
      "actions": [
        {
          "type": "Action.Submit",
          "title": "A",
          "data": {
            "msteams": {
              "type": "messageBack",
              "displayText": "A!",
              "text": "A"
            }
          }
        },
        {
          "type": "Action.Submit",
          "title": "B",
          "data": {
            "msteams": {
              "type": "messageBack",
              "displayText": "B!",
              "text": "B"
            }
          }
        },
        {
          "type": "Action.Submit",
          "title": "C",
          "data": {
            "msteams": {
              "type": "messageBack",
              "displayText": "C!",
              "text": "C"
            }
          }
        },
        {
          "type": "Action.Submit",
          "title": "D",
          "data": {
            "msteams": {
              "type": "messageBack",
              "displayText": "D!",
              "text": "D"
            }
          }
        }
      ]
    }
  ],
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "version": "1.2"
}

此代码在仿真器中不再起作用,但是我很困惑。我觉得我在这里确实遗漏了一些明显的东西-有人能说明我做错了什么吗?

0 个答案:

没有答案