Payload option on Facebook Bots buttons

The Facebook Submission API mentions the type of “payload” that you can set for buttons in the Generic Response template. However, they do not say how this works, except:

For postback buttons, this data will be sent to you via webhook

But how is he sent back? I do not receive any messages when I click the payload button. Has anyone used it successfully?

+7
facebook bots facebook messenger
source share
3 answers

I tested it and it works for me. The button payload acts as the value on the html button. This means that it is not displayed to the user, but this is the value that is returned to you.

If you create a button like this:

'attachment': { 'type': 'template', 'payload': { 'template_type': 'button', 'text': 'This is the description', 'buttons': [ { 'type': 'postback', 'title': 'This is the visible text', 'payload': 'This is the value you get back' } ] } 

The callback involving the payload is as follows:

 {'timestamp': 1461697120850, 'postback': {'payload': 'this is the value you get back'}, 'recipient': {'id': xxxxxxxx}, 'sender': {'id': xxxxxxxx}} 
+5
source share

When you click the button, a POST message is sent to your /webhook .

You should handle the payload as follows:

 app.post('/webhook/', function (req, res) { messaging_events = req.body.entry[0].messaging; for (i = 0; i < messaging_events.length; i++) { event = req.body.entry[0].messaging[i]; sender = event.sender.id; if (event.message && event.message.text) { text = event.message.text; // Handle a text message from this sender } else if (event.postback && event.postback.payload) { payload = event.postback.payload; // Handle a payload from this sender } } res.sendStatus(200); }); 

This code snippet is from the Facebook Getting Started Guide, except for else if , where you can respond to the payload.

+3
source share

Here's how you read the payload:

 $payload = $input['entry'][0]['messaging'][0]['postback']['payload']; 
0
source share

All Articles