问题
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.
回答1:
Card behavior is channel-specific by nature.
This is true across the board, regardless of card type, action type, or channel. While certain guidelines apply to, say, the properties of card actions, you really can't depend on a property like displayText
behaving the way you expect it to. You need to test the card yourself. Here's some code that will help you test a variety of card action types and properties:
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);
These three action types (ImBack, PostBack, and MessageBack) have some expected behavior associated with them:
- ImBack is meant to display a message in the conversation history as though the user typed it
- PostBack is meant to send an invisible message to the bot with hidden metadata
- MessageBack is meant to send a message to the bot that gets displayed in the conversation history and contains hidden metadata, combining the other two types
Again, you cannot count on this behavior to be implemented consistently across different channels. Of the three of these, it turns out that the Facebook Messenger Platform only has the PostBack type, but it actually behaves like a MessageBack in that it displays text to the user as well as sending alternate text to the bot. In a hero card, CardAction.Title
will be used for the button's label and the text displayed in the conversation history, and CardAction.Value
will be used as hidden data assigned to both the incoming activity's Text
and Value
properties. CardAction.Text
and CardAction.DisplayText
will be ignored by the Facebook Messenger connector.
Since you just want text to be displayed in the conversation history, you're in luck. It actually doesn't matter which action type you use. The Facebook connector automatically converts a hero card with any of those three action types to a generic template with PostBack buttons. Suggested actions get converted to Quick Replies, which behave a bit differently in that the data sent to your bot will be in a different format, but you can extract the same information from them.
If you want to send a Facebook template to Messenger directly instead of depending on the connector to convert a hero card, you can use channel data. You could construct the template in C# like this:
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"
}
}
}
}
}
}
};
For some reason the SDK doesn't like it when you use an anonymous type like that, so you'll want to convert it to a JObject
before sending it to Facebook:
reply.ChannelData = JObject.FromObject(data);
await connector.Conversations.ReplyToActivityAsync(reply);
来源:https://stackoverflow.com/questions/55626960/display-text-doesnt-echo-back