Sam Tassell Sam Tassell - 3 months ago 28
Node.js Question

How to handle postback from Facebook sourceEvent botframework node

So as mentioned in the title I need to figure out how to handle a postback from the user tapping/clicking a button in facebook messenger. I'm aware this card can be handled with a bot builder card but there are some other elements I want to test too. Please see the following code:

bot.dialog('/', [
function (session) {
var msg = new builder.Message(session).sourceEvent({
facebook: {
notification_type: "REGULAR",
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [{
title: "Title here",
image_url: "http://docs.botframework.com/images/demo_bot_image.png",
subtitle: "Subtitle here",
buttons: [{
type: "postback",
title: "GO",
payload: "dosomething"
}]
}]
}
}
}
});
session.send(msg);
},
function(session, results){
session.send(JSON.stringify(results));
session.send(JSON.stringify(session));
}
]);


So basically once the user clicks the 'GO' button I want to action another dialog in node, the card works but I just don't know how to handle it. I thought it might work with the waterfall but the shown code didn't return anything.

Many thanks,

Answer

So I've pulled my head out of the clouds and answered my own question. The clue was in the button type: type: "postback".

See the following:

bot.dialog('/', [
    function (session) {
        var msg = new builder.Message(session).sourceEvent({
                facebook: {
                        notification_type: "REGULAR",
                        attachment: {
                            type: "template",
                            payload: {
                                template_type: "generic",
                                elements: [{
                                    title: "Great title",
                                    image_url: "http://docs.botframework.com/images/demo_bot_image.png",
                                    subtitle: "Even better subtitle",
                                    buttons: [{
                                        type: "postback",
                                        title: "GO",
                                        payload: "action?stacktest"
                                    }]
                                }]
                            }
                        }
                    }
            });
        session.send(msg);
    }
]);

So all I've done is posted back the action 'stacktest' and handled it with the following:

bot.beginDialogAction('stacktest', '/stacktest');
bot.dialog('/stacktest', function(session){
    session.send("Woot, stacktest worked a treat.");
});

Hope this helps someone.

Cheers.

Comments