Bot Framework fails to send FacebookQuickReply using AddKeyboardCard or using ChannelData

帅比萌擦擦* 提交于 2019-12-29 01:45:13

问题


I am using Bot Framework 3.3.0 which I understand supports Facebook's quick replies implemented properly (as opposed to creating a dynamic object and sending it via ChannelData). The class name is Microsoft.Bot.Builder.ConnectorEx.FacebookQuickReply.

Here is how I create the quick replies from within an IDialog:

var reply = context.MakeMessage();
reply.Text = msg;
var quickReplies = new List<FacebookQuickReply>()
{
    new FacebookQuickReply(FacebookQuickReply.ContentTypes.Text, "Cathay", "Cathay"),
    new FacebookQuickReply(FacebookQuickReply.ContentTypes.Text, "HK Airlines", "Hong Kong Airlines"),
    new FacebookQuickReply(FacebookQuickReply.ContentTypes.Text, "HK Express", "HK Express")
};

And here are the two ways of how I'm trying to send them into the chat:

//I tried both of the lines below
//reply.ChannelData = quickReplies.ToArray();
reply.AddKeyboardCard<FacebookQuickReply>("quick_replies", quickReplies);

await context.PostAsync(reply);

The first way that I got from Ezequiel Jadib's article (hi, I know you're reading this!) didn't work for me on facebook messenger. Partly the reason is that it seems in v3.3 the FacebookQuickReply class is now sealed and cannot be derived from.

UPDATE: I got it to work by using this:

var channelData = new FacebookChannelData();

channelData.QuickReplies = new[]
{
    new FacebookQuickReply(FacebookQuickReply.ContentTypes.Text, "Cathay", "Cathay"),
    new FacebookQuickReply(FacebookQuickReply.ContentTypes.Text, "HK Airlines", "Hong Kong Airlines"),
    new FacebookQuickReply(FacebookQuickReply.ContentTypes.Text, "HK Express", "HK Express")
};

reply.ChannelData = channelData;

Where FacebookChannelData class is simply this:

public class FacebookChannelData
{
    [JsonProperty("quick_replies")]
    public FacebookQuickReply[] QuickReplies { get; set; }
}

Even though it works, it doesn't seem to be a proper way because the bot framework now provides ready to use classes for that.

END UPDATE

The second way (AddKeyboardCard) kind of works in the emulator (but doesn't work on actual facebook), but produces totally wrong JSON that I can see in the emulator. The resulting JSON uses the class name (why?) instead of the text I provided:

"attachments": [
    {
      "contentType": "application/vnd.microsoft.card.hero",
      "content": {
        "text": "quick_replies",
        "buttons": [
          {
            "type": "imBack",
            "title": "Microsoft.Bot.Builder.ConnectorEx.FacebookQuickReply",
            "value": "Microsoft.Bot.Builder.ConnectorEx.FacebookQuickReply"
          },
          {
            "type": "imBack",
            "title": "Microsoft.Bot.Builder.ConnectorEx.FacebookQuickReply",
            "value": "Microsoft.Bot.Builder.ConnectorEx.FacebookQuickReply"
          },
          {
            "type": "imBack",
            "title": "Microsoft.Bot.Builder.ConnectorEx.FacebookQuickReply",
            "value": "Microsoft.Bot.Builder.ConnectorEx.FacebookQuickReply"
          }
        ]
      }
    }
  ],

What am I doing wrong and how do you use FacebookQuickReply and AddKeyboardCard()?


回答1:


The key of the FacebookQuickReply feature recently added is on this commit.

It seems the way to go is to create a KeyboardCard and the use the ToFacebookMessage extension method to get the ChannelData value (as used here)




回答2:


If you are using a PromptDialog, you can use the following snippet to create quick replies in Messenger:

PromptDialog.Choice(
    context,
    resume,
    list of options as strings,
    "What do you want to choose?",
    retry: "I didn't understand, let's try that again",
    attempts: 2,
    promptStyle: PromptStyle.Keyboard
);


来源:https://stackoverflow.com/questions/40230436/bot-framework-fails-to-send-facebookquickreply-using-addkeyboardcard-or-using-ch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!