您如何处理 Bot Framework .NET for Teams 中的 CardAction 按钮点击?
How do you handle CardAction Button Clicks in Bot Framework .NET for Teams?
我使用 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);
}
}
}
这是一个实际应用的例子。
这是活动的顺序。
- 我向机器人发送消息“测试”。
- 机器人用包含两个按钮的消息进行响应。
- 我点击第一个按钮。
- 机器人回复说我的选择很明智。
- 我输入“cup1”
- 机器人回复说我的选择很明智。
我想要做的是让机器人在我输入“cup1”时忽略,因为这不是按钮点击。当我检查机器人在单击按钮时收到的 IMessageActivity
时,似乎没有任何迹象表明它是按钮单击。感谢您的帮助!
ImBack
操作旨在模拟消息,就好像用户通过文本将消息发送给您一样。它们旨在用作您的用户键入消息的替代方法,因此上述行为是一种预期的规范。
话虽这么说,但您有几种选择可以实现您的目标。第一种是为您的按钮使用 messageBack
操作类型。这会给你更多的控制权,更容易确定按钮点击 v 的文本消息。
第二种选择是使用自适应卡片及其动作(在本例中为 action.submit
或 action.execute
,具体取决于您想要的行为),而不是英雄卡片。这可能是我建议的 Teams 解决方案,因为 Adaptive Cards 比 Hero Cards 给你的灵活性要大得多。
可以在此处找到 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\" }"
}
]
}
如果你想区分按钮点击,那么你可以在 ms teams 中使用 “Adaptive Card”。因为在 ms 团队频道中单击自适应卡按钮时,"Activity" 文本应该为空,值 属性 将包含“msteams”属性 值。
MS Teams 自适应卡需要特殊的 属性 名称 msteams 到对象中的对象提交操作的数据 属性 才能访问此功能。
{
"type": "Action.Submit",
"title": "Click me for messageBack",
"data": {
"msteams": {
"type": "messageBack",
"displayText": "I clicked this button",
"text": "text to bots",
"value": "{\"bfKey\": \"bfVal\", \"conflictKey\": \"from value\"}"
},
"extraData": {}
}
}
类型属性是“messageBack”提交动作将表现得像一个messageBack卡片动作,就像是imBack和postBack的组合。
我使用 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);
}
}
}
这是一个实际应用的例子。
这是活动的顺序。
- 我向机器人发送消息“测试”。
- 机器人用包含两个按钮的消息进行响应。
- 我点击第一个按钮。
- 机器人回复说我的选择很明智。
- 我输入“cup1”
- 机器人回复说我的选择很明智。
我想要做的是让机器人在我输入“cup1”时忽略,因为这不是按钮点击。当我检查机器人在单击按钮时收到的 IMessageActivity
时,似乎没有任何迹象表明它是按钮单击。感谢您的帮助!
ImBack
操作旨在模拟消息,就好像用户通过文本将消息发送给您一样。它们旨在用作您的用户键入消息的替代方法,因此上述行为是一种预期的规范。
话虽这么说,但您有几种选择可以实现您的目标。第一种是为您的按钮使用 messageBack
操作类型。这会给你更多的控制权,更容易确定按钮点击 v 的文本消息。
第二种选择是使用自适应卡片及其动作(在本例中为 action.submit
或 action.execute
,具体取决于您想要的行为),而不是英雄卡片。这可能是我建议的 Teams 解决方案,因为 Adaptive Cards 比 Hero Cards 给你的灵活性要大得多。
可以在此处找到 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\" }"
}
]
}
如果你想区分按钮点击,那么你可以在 ms teams 中使用 “Adaptive Card”。因为在 ms 团队频道中单击自适应卡按钮时,"Activity" 文本应该为空,值 属性 将包含“msteams”属性 值。
MS Teams 自适应卡需要特殊的 属性 名称 msteams 到对象中的对象提交操作的数据 属性 才能访问此功能。
{
"type": "Action.Submit",
"title": "Click me for messageBack",
"data": {
"msteams": {
"type": "messageBack",
"displayText": "I clicked this button",
"text": "text to bots",
"value": "{\"bfKey\": \"bfVal\", \"conflictKey\": \"from value\"}"
},
"extraData": {}
}
}
类型属性是“messageBack”提交动作将表现得像一个messageBack卡片动作,就像是imBack和postBack的组合。