8

Facebook Send API mentions a "payload" type you can set for buttons on the Generic Response Template. However, they don't say how it works, other than:

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

But how is it sent back? I don't receive any messages when I click the payload button. Has anyone successfully used it?

Matheus208
  • 1,289
  • 2
  • 13
  • 26
  • 3
    did you subscribed to messaging_postbacks option while setting up web-hook for messenger bot..? https://developers.facebook.com/docs/messenger-platform/webhook-reference – piyushmandovra May 27 '16 at 06:21

3 Answers3

7

I tested it and it works for me. The payload of a button acts like the value on a html button. This means it's not visible to the user but it's the value that's send back to you.

If you create a button like that:

'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'
             }
        ]
 }

A callback including a payload looks like that:

{'timestamp': 1461697120850, 'postback': {'payload': 'this is the value you get back'}, 'recipient': {'id': xxxxxxxx}, 'sender': {'id': xxxxxxxx}}
Juergen
  • 662
  • 4
  • 9
  • how to capture it's value in php ? – runningmark Jul 21 '16 at 12:04
  • 1
    @runningmark `if(isset($input['entry'][0]['messaging'][0]['message']['quick_reply'])){ $payload_txt = $input['entry'][0]['messaging'][0]['message']['quick_reply']['payload'];` – Abhishek Oct 25 '16 at 20:50
  • Have you test these lines ? I tried it it's not working – Shahroze Nawaz Dec 16 '16 at 07:35
  • 2
    During debugging I log all requests received into a textfile: file_put_contents("testfile.txt", json_encode( $input ) . PHP_EOL, FILE_APPEND | LOCK_EX); – Igor L. Dec 23 '16 at 11:44
  • Good for viewing request structure – Igor L. Dec 23 '16 at 11:44
  • Is anyone able to see docs on the maximum allowed size of the payload. Can't find it [here](https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_postbacks) – Mike H-R Jan 11 '18 at 16:23
  • how can i add custom keys to the default button array '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' 'custom_field': number } ] } – Mani David Jan 18 '18 at 08:08
5

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

You have to handle the payload like this:

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 Getting Started Guide from Facebook except for the else if where you can react on the payload.

ush189
  • 1,342
  • 6
  • 22
  • 30
1

This is how you read back the payload:

$payload = $input['entry'][0]['messaging'][0]['postback']['payload'];
Dragos Vana
  • 66
  • 1
  • 1
  • 6