Bot Framework 4 new Activity with ChannelData null reference & Facebook Messenger

Bot Framework 4 new Activity with ChannelData null reference & Facebook Messenger

我无法在 Facebook Messenger 中显示任何按钮或图像。这太令人沮丧了。似乎没有任何效果,说明、样品等……似乎已经过时了。这是我的代码。在其他渠道中,我为此使用了 SuggestedActions。我基本上是在问是或否的问题。

var attachment = new
                                {
                                    type = "template",
                                    payload = new
                                    {
                                        template_type = "button",
                                        text = "Please make a selection.",
                                        buttons = new object[]
                                        {                                                
                                            new
                                            {
                                                type = "postback",                                            
                                                title = "Yes",
                                                payload = "Yes",
                                            },
                                            new
                                            {
                                                type = "postback",
                                                title = "No",
                                                payload = "No",
                                            }
                                        },
                                    },
                                };


                                var reply = (turnContext.Activity as Activity).CreateReply();                                
                                reply.Type = ActivityTypes.Message;
                                reply.Text = "Does this work for you?";
                                reply.ChannelData = JObject.FromObject(new 
                                {
                                    notification_type = "REGULAR",
                                    attachment
                                });


await turnContext.SendActivityAsync(reply);

当我尝试使用 ChannelData 发送时,出现空引用异常。这是在 OnMessageActivityAsync 内部发生的。这是一个 .Net Core 3.1 应用程序。

更新 1

我刚刚将所有软件包从 v4.11.1 更新到 v4.14.1。我仍然收到 Null 引用异常。

更新 2

像这样用消息 属性 包装我的代码后,我不再收到 Null 引用异常。

https://developers.facebook.com/docs/messenger-platform/reference/templates/button

var message = new
                                {
                                    attachment = new
                                    {
                                        type = "template",
                                        payload = new
                                        {
                                            template_type = "button",
                                            text = "Please make a selection.",
                                            buttons = new object[]
                                        {
                                            //new
                                            //{
                                            //    type = "web_url",
                                            //    url = "https://mybot.azurewebsites.net/",
                                            //    title = "Sign Up!"
                                            //},
                                            new
                                            {
                                                type = "postback",
                                                title = "Yes",
                                                payload = "Yes",
                                            },
                                            new
                                            {
                                                type = "postback",
                                                title = "No",
                                                payload = "No",
                                            }
                                        },
                                        },
                                    } 
                                };

非常感谢任何帮助!谢谢!

这是可行的解决方案。请参阅更新 3 以获得解释。

var recipient = new
                                {
                                    id = turnContext.Activity.From.Id
                                };

var message = new
                                {
                                    attachment = new
                                    {
                                        type = "template",
                                        payload = new
                                        {
                                            template_type = "button",
                                            text = "Please make a selection.",
                                            buttons = new object[]
                                        {
                                            //new
                                            //{
                                            //    type = "web_url",
                                            //    url = "https://mybot.azurewebsites.net/",
                                            //    title = "Sign Up!"
                                            //},
                                            new
                                            {
                                                type = "postback",
                                                title = "Yes",
                                                payload = "Yes",
                                            },
                                            new
                                            {
                                                type = "postback",
                                                title = "No",
                                                payload = "No",
                                            }
                                        },
                                        },
                                    } 
                                };

var reply = (turnContext.Activity as Activity).CreateReply();
                                reply.Type = ActivityTypes.Message;
                                reply.Text = "Does this work for you?
                                reply.ChannelData = JObject.FromObject(new 
                                {
                                    recipient,
                                    message
                                });

await turnContext.SendActivityAsync(reply, cancellationToken);

希望这对其他人有帮助!