botframeworkfacebook-messenger

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


I can't get any buttons or images for that matter to display in Facebook Messenger. This is so frustrating. Nothing seems to work and the instructions, samples etc... seem so outdated. Here is my code. In other channels, I'm using SuggestedActions for this. I'm basically asking a Yes or No question.

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

I'm getting a Null reference exception when I try to Send with ChannelData. This is happening inside of OnMessageActivityAsync. This is a .Net Core 3.1 app.

UPDATE 1

I just updated all the packages from v4.11.1 to v4.14.1. I'm still getting the Null reference exception.

UPDATE 2

I'm no longer getting the Null reference exception after wrapping my code with the message property like so.

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",
                                            }
                                        },
                                        },
                                    } 
                                };

Any help is much appreciated! Thanks!


Solution

  • Here is the working solution. Please see Update 3 for an explanation.

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

    Hope this helps someone else!