Bot Framework 4 带有 ChannelData null 引用和 Facebook Messenger 的新活动

2023-12-15

我无法在 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 应用程序。

UPDATE 1

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

UPDATE 2

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

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);

希望这对其他人有帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Bot Framework 4 带有 ChannelData null 引用和 Facebook Messenger 的新活动 的相关文章

随机推荐