c# - 您如何处理 Bot Framework .NET for Teams 中的 CardAction 按钮点击?

标签 c# botframework microsoft-teams azure-bot-service

我使用 Microsoft.Bot.Builder.Azure 构建了一个机器人4.12.2 通过 Azure 机器人服务连接到 MS Teams。我有一个带有英雄卡附件的消息,其中包含一组按钮。当用户单击按钮时,卡片的值会作为消息发送回机器人,但似乎没有附加任何其他信息来识别消息是按钮单击而不是消息。我想了解如何正确处理按钮点击。
我将展示我的代码来演示...

public class MyBot : ActivityHandler
{ 
    protected override async Task OnMessageActivityAsync(
        ITurnContext<IMessageActivity> turnContext,
        CancellationToken cancellationToken)
    {
        var activity = turnContext.Activity;
        if (activity.Text is "test")
        {
            var heroCard = new HeroCard
            {
                Buttons = new List<CardAction>
                {
                    new(ActionTypes.ImBack)
                    {
                       Title = "Cup 1",
                       DisplayText = "You chose Cup1",
                       Value = "cup1",
                       ChannelData = new { id = 1, status = "wise" }
                    },
                    new(ActionTypes.ImBack)
                    {
                        Title = "Cup 2",
                        DisplayText = "You chose Cup2",
                        Value = "cup2",
                        ChannelData = new { id = 1, status = "unwise" }
                    }
                }
            };
            var response = MessageFactory.Text("Choose wisely");
            response.Attachments.Add(heroCard.ToAttachment());
            await turnContext.SendActivityAsync(response, cancellationToken);
            return;
        }

        // How can I tell that they actually clicked the Cup 1 button and didn't just type "cup1"?
        if (activity.Text is "cup1")
        {
            await turnContext.SendActivityAsync(MessageFactory.Text("you chose wisely."), cancellationToken);
        }
        else
        {
            await turnContext.SendActivityAsync(MessageFactory.Text("you chose unwisely."), cancellationToken);
        }
    }
}

这是它在行动中的一个例子。
Teams session
这是事件的顺序。
  • 我向机器人发送了一条消息“测试”。
  • Bot 回复一条包含两个按钮的消息。
  • 我点击第一个按钮。
  • Bot 回应说我的选择是明智的。
  • 我输入“cup1”
  • Bot 回应说我的选择是明智的。

  • 如果我键入“cup1”,我希望能够做的是让机器人忽略,因为这不是单击按钮。当我检查 IMessageActivity机器人在按钮点击时收到,似乎没有任何东西表明它是按钮点击。任何帮助表示赞赏!

    最佳答案

    ImBack action 旨在模拟一条消息,就好像用户通过文本将其发送给您一样。它们旨在用作用户键入消息的替代方法,因此上述行为是一种预期的规范。
    话虽如此,您有几种选择来实现您的目标。第一个是使用 messageBack按钮的操作类型。这会给你更多的控制,更容易确定按钮点击 v 的文本消息。
    第二种选择是使用自适应卡片及其 Action (在本例中为 action.submitaction.execute,取决于您想要的行为),而不是英雄卡片。这可能是我建议的 Teams 解决方案,因为自适应卡片比英雄卡片为您提供了更大的灵活性。
    可以在此处找到 Teams 中卡片操作的完整文档:https://docs.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-actions ,但我也举了一个例子 messageBack下面行动。

    {
      "buttons": [
        {
        "type": "messageBack",
        "title": "My MessageBack button",
        "displayText": "I clicked this button",
        "text": "User just clicked the MessageBack button",
        "value": "{\"property\": \"propertyValue\" }"
        }
      ]
    }
    

    关于c# - 您如何处理 Bot Framework .NET for Teams 中的 CardAction 按钮点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67537580/

    相关文章:

    c# - C# 中的日期时间分析器错误

    c# - 比较日期时发生 StackOverflowException

    c# - Bot Framework 搞乱了对话框状态

    botframework - 如何在 bot framework v4 中形成英雄卡片轮播

    botframework - Bot Framework v4 - Bot 发起对话

    node.js - 我想在 Microsoft Teams 中垂直显示按钮

    azure - 无法创建 MS Team 免费组织帐户

    c# - 如何使用c#实现非二叉树结构

    c# - 在 Entity Framework 4.1 中将鉴别器列更改为 int

    c# - 显示 OAuth 卡时任务被取消