为Facebook设置ChannelData

时间:2017-03-01 15:53:58

标签: c# facebook botframework

当我尝试在facebook上设置ChannelData时,我发现如果我没有附加任何附件,那么Facebook就不会显示我在ChannelData中附加的内容。例如:

如果我寄这个:

var channelData = new JObject();
var child = new JObject();

child.Add("content_type", "text");
child.Add("title", "Red");
child.Add("payload", "DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_RED");

channelData.Add("quick_replies", new JArray(child));

var replyOrder = context.MakeMessage();
var attachments = CardFactoryFacebook.GetMainContactThumbnailCard(); 

replyOrder.Attachments.Add(attachments);
replyOrder.ChannelData = channelData;

await context.PostAsync(replyOrder);

所以在这种情况下,我们将在信使中看到ThumbnailCard,并在我在ChannelData" Red"中创建的按钮下面。

但是,如果我设置ChannelData而不添加任何附件,那么不会在messenger中显示任何内容,例如:

var replyOrder = context.MakeMessage();

replyOrder.ChannelData = channelData;

await context.PostAsync(replyOrder);

我错过了什么吗? 感谢

3 个答案:

答案 0 :(得分:1)

我认为你不需要进行"活动"附件,一切都应该在频道数据上定义(当它是关于facebook和它的卡片时),在这种情况下有一个" facebook"附件,但它在channelData内。我有这段代码工作:

 Activity replyToConversation = activity.CreateReply("Quick Replies");

                dynamic messageData = new JObject();
                messageData.attachment = new JObject();
                messageData.attachment.type = "template";
                messageData.attachment.payload = new JObject();
                messageData.attachment.payload.template_type = "generic";


                messageData.attachment.payload.elements
                    = new JArray(
                        new JObject(
                            new JProperty("title", "hola"),
                            new JProperty("subtitle", "Mundo"),
                            new JProperty("buttons",
                                new JArray(
                                    new JObject(
                                        new JProperty("type", "element_share")
                                    )
                                )
                            )
                        )
                    );


                replyToConversation.ChannelData = messageData;
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                await connector.Conversations.ReplyToActivityAsync(replyToConversation);

答案 1 :(得分:1)

Quick Replies作为邮件的一部分发送。根据Facebook的文档,消息对象必须包含有效的文本或附件。

Quick Replies Doc

使用您创建的频道数据对象(格式正确),您只需发送消息的“quick_replies”部分。

尝试在邮件中添加文字,例如:

replyOrder.Text = "What's your favorite color?";

请注意,在快速回复的特定情况下,BotBuilder库已经提供了一种使用它们的方法,而无需使用KeyboardCard手动创建ChannelData。查看this了解详情。

答案 2 :(得分:0)

基于Botframework API的Facebook消息,Attachment是您放置Facebook消息的地方。发送没有消息的消息没有多大意义。