Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...