首页 > 解决方案 > 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.

标签: c#botframework

解决方案


卡行为本质上是特定于通道的。

无论卡牌类型、行动类型或渠道如何,这都是全面的。虽然某些准则适用于卡片操作的属性,但您确实不能依赖于像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并将用作分配给传入活动TextValue属性的隐藏数据。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);

推荐阅读