Search code examples
facebookfacebook-graph-apibotframeworkfacebook-messenger-bot

Callbacks not rendered as native Facebook messages?


According to this scenario.

Used to test it using sample message - text. And it works fine.

I should implement that by using buttons (callbacks).

The real problem is :

Sample text :

context.Activity.Id
"mid.$cAAOmNGtaSJ9i1_f223cprKylFOpb"

on button click (callback) :

context.Activity.Id
"9Yw5TAcq1qr"

This is the code i used to post human support button :

                    msg.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                    msg.Attachments = new List<Attachment>();
 
                    ThumbnailCard thumbnailCard2 = new ThumbnailCard()
                    {
                        Buttons = new List<CardAction>
                        {
                                new CardAction ()
                            {
                                Value = "Method_Support",
                                Type = "postBack",
                                Title = "Human Support"
 
                            },
                        },
                    };
                    msg.Attachments.Add(thumbnailCard2.ToAttachment());
                    await context.PostAsync(msg);

It returns activity.Id : 9Yw5TAcq1qr - as i already mentioned. I can't use that id to query Facebook graph api.

What's the best way to handle scenario like this ?

Is it possible to render all incoming messages as native Facebook (sample-text) messages ?

Is it possible to make call to graph api using callback's activity.id ?


Solution

  • Following my previous answer about how to link the Activity from botFramework to the messages in Conversation in Facebook's Graph API, I had a look to your problem.

    Analysis

    From my point of view, there is a strange thing in this Activity.Id value because on Facebook's side, all messages have the same id format, even for callbacks.

    Here is a capture of 2 messages (1st one is the latest one) from Facebook Graph API:

    {
        "data": [{
            "messages": {
                "data": [
                    {
                        "id": "m_mid.$cAAC3Jml0RcBi2JK3Clcp01uu8FFe",
                        "message": "Human support"
                    },
                    {
                        "id": "m_mid.$cAAC3Jml0RcBi2I5bXlcp0kScM7af",
                        "message": ""
                    }
                ]
            },
            "id": "t_mid.$..."
        }]
    }
    
    • Message with empty text is a ThumbnailCard with a CardAction inside (type = postback, Value = Method_HumanSupport)
    • Human support is what is sent on the click on the CardAction

    What is surprising is that on the bot side, for those 2 messages we have the following values for Activity.Id:

    • For "id": "m_mid.$cAAC3Jml0RcBi2I5bXlcp0kScM7af" (my Thumbnail) => mid.$cAAC3Jml0RcBi2I5bXlcp0kScM7af, so it's matching
    • For "id": "m_mid.$cAAC3Jml0RcBi2JK3Clcp01uu8FFe" (the callback) => DWhE2C0VO79, no match at all

    For information my ThumbnailCard is made like the OP said:

    var msg = context.MakeMessage();
    msg.AttachmentLayout = AttachmentLayoutTypes.Carousel;
    msg.Attachments = new List<Attachment>();
    msg.Text = "updates & support";
    
    ThumbnailCard thumbnailCard2 = new ThumbnailCard()
    {
        Buttons = new List<CardAction>
        {
    
            new CardAction ()
            {
                Value = "Method_HumanSupport",
                Type = "postBack",
                Title = "Human support"
            }
        }
    };
    

    I will add a question on Bot Framework GitHub Project. Edit: issue logged: https://github.com/Microsoft/BotBuilder/issues/2950

    Workaround

    There are several workaround that will depend on your global use cases: you could try to log the Activity.Id of a "normal" message in the discussion and use this value later when your callback is clicked, or you can also try to change your flow to avoid making this kind of link between Bot Framework and Facebook Graph API.