How to

Bot Framework fails to send FacebookQuickReply using AddKeyboardCard or using ChannelData

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()?

Powered by WPeMatico

Leave a Reply

Your email address will not be published. Required fields are marked *