c# - Display Text doesn't echo back
问题描述
I am creating a Messageback Button with Title, DisplayText, text, and value (v3 SDK). The title is set correctly when running the bot, the display text did not appear after clicking the button.
I have set up two card action.
CardAction yesBtn = new CardAction()
{
Type = ActionTypes.MessageBack,
Title = "Yes",
DisplayText = "OK",
Text = "Yes",
};
CardAction noBtn = new CardAction()
{
Type = ActionTypes.MessageBack,
Title = "No",
DisplayText = "No",
Text = "No",
};
I cannot find any solution to this problem. The most similar one is : CardAction DisplayText doesn't seem to work but there is no answer.
The document of Microsoft bot framework said
displayText
Optional. Echoed by the user into the chat stream when the action is performed. This text is not sent to your bot.
but nothing happens after clicking the button.
I also tried the imBack ActionType, and the result is the same.
I test it on the bot emulator and azure portal, both don't work.
解决方案
卡行为本质上是特定于通道的。
无论卡牌类型、行动类型或渠道如何,这都是全面的。虽然某些准则适用于卡片操作的属性,但您确实不能依赖于像displayText
您期望的那样表现的属性。您需要自己测试该卡。这里有一些代码可以帮助您测试各种卡片操作类型和属性:
var actionTypes = new List<string>
{
ActionTypes.ImBack,
ActionTypes.PostBack,
ActionTypes.MessageBack,
};
var cardActions = actionTypes.Select(actionType => new CardAction(
actionType,
$"{actionType} title",
null,
$"{actionType} value",
$"{actionType} text",
$"{actionType} displayText"
)).ToList();
var reply = activity.CreateReply("Reply:");
reply.Attachments = new List<Attachment> { new Attachment(HeroCard.ContentType, content: new HeroCard("Hero title", "Hero subtitle", "Hero text", buttons: cardActions)) };
reply.SuggestedActions = new SuggestedActions(new List<string> { activity.From.Id }, cardActions);
await Connector.Conversations.ReplyToActivityAsync(reply);
这三种操作类型(ImBack、PostBack 和 MessageBack)具有一些与之相关的预期行为:
- ImBack 旨在在对话历史记录中显示一条消息,就像用户键入它一样
- PostBack 旨在向机器人发送隐藏元数据的不可见消息
- MessageBack 旨在向机器人发送一条消息,该消息显示在对话历史记录中并包含隐藏的元数据,结合了其他两种类型
同样,您不能指望这种行为可以在不同的渠道中一致地实施。在这三个中,事实证明 Facebook Messenger 平台只有PostBack类型,但实际上它的行为类似于 MessageBack,因为它向用户显示文本以及向机器人发送替代文本。在英雄卡片中,CardAction.Title
将用于按钮的标签和对话历史记录中显示的文本,CardAction.Value
并将用作分配给传入活动Text
和Value
属性的隐藏数据。CardAction.Text
并将CardAction.DisplayText
被 Facebook Messenger 连接器忽略。
由于您只想在对话历史记录中显示文本,因此您很幸运。实际上,您使用哪种操作类型并不重要。Facebook 连接器会自动将具有这三种操作类型中的任何一种的英雄卡片转换为具有 PostBack 按钮的通用模板。建议的操作将转换为快速回复,其行为略有不同,因为发送到您的机器人的数据将采用不同的格式,但您可以从中提取相同的信息。
如果你想直接向 Messenger 发送 Facebook 模板,而不是依赖连接器来转换英雄卡,你可以使用channel data。您可以像这样在 C# 中构造模板:
object data = new
{
attachment = new
{
type = "template",
payload = new
{
template_type = "generic",
elements = new[]
{
new
{
title = "generic title",
subtitle = "generic subtitle",
image_url = "",
buttons = new[]
{
new
{
type = "postback",
title = "postback title",
payload = "postback payload"
}
}
}
}
}
}
};
由于某种原因,SDK 不喜欢您使用这样的匿名类型,因此您需要在将其JObject
发送到 Facebook 之前将其转换为 a:
reply.ChannelData = JObject.FromObject(data);
await connector.Conversations.ReplyToActivityAsync(reply);
推荐阅读
- java - Spring Boot REST 端点忽略 Content-Type
- javascript - 按产品类从数组创建产品列表,然后保存在本地存储中
- sql - SQL Server 到 Azure 流程工作流迁移
- javascript - onchange 函数未在移动浏览器上触发
- sql - 查找每一行的日期范围。LEAD/LAG,但具有不同的类型/动作
- python - 创建 Geoviews quadmesh 时出错:没有可用的存储后端能够支持提供的数据格式
- python - Curl 的 Python 等效项给出 400 响应
- angular - 带有Angular 9的HammerJs手势
- java - 如何使用缓冲阅读器在java中不跳过行尾字符的情况下读取行
- javascript - jQuery 每行 textarea 发送 ajax 请求