Haa*_*ked 5 c# botframework microsoft-teams azure-bot-service
我使用 4.12.2 构建了一个机器人,Microsoft.Bot.Builder.Azure通过 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个实际的例子。
这是活动的顺序。
我想要做的是让机器人忽略我输入“cup1”的情况,因为这不是单击按钮。当我检查IMessageActivity机器人在按钮单击时收到的信息时,似乎没有任何迹象表明这是按钮单击。任何帮助表示赞赏!
该ImBack操作旨在模拟一条消息,就好像用户通过文本将其发送给您一样。它们旨在用作用户键入消息的替代方法,因此上述行为是预期的规范。
话虽这么说,您有几种选择来实现您的目标。第一种是使用messageBack按钮的操作类型。这将为您提供更多控制权,并更轻松地确定按钮单击 v 的文本消息。
第二种选择是使用自适应卡及其动作(在这种情况下或者action.submit取决于action.execute您想要的行为),而不是英雄卡。这可能是我为团队建议的解决方案,因为自适应卡比英雄卡为您提供更大的灵活性。
可以在此处找到 Teams 中卡片操作的完整文档:https://learn.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\" }"
}
]
}
Run Code Online (Sandbox Code Playgroud)