"Payload" option on Facebook Bots buttons
Asked Answered
B

3

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?

Bookerbookie answered 25/4, 2016 at 21:6 Comment(1)
did you subscribed to messaging_postbacks option while setting up web-hook for messenger bot..? developers.facebook.com/docs/messenger-platform/…Sisyphus
K
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}}
Kornegay answered 27/4, 2016 at 8:31 Comment(7)
how to capture it's value in php ?Amish
@Amish if(isset($input['entry'][0]['messaging'][0]['message']['quick_reply'])){ $payload_txt = $input['entry'][0]['messaging'][0]['message']['quick_reply']['payload'];Fairlead
Have you test these lines ? I tried it it's not workingBackpack
During debugging I log all requests received into a textfile: file_put_contents("testfile.txt", json_encode( $input ) . PHP_EOL, FILE_APPEND | LOCK_EX);Seven
Good for viewing request structureSeven
Is anyone able to see docs on the maximum allowed size of the payload. Can't find it hereEdana
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 } ] }Oppression
S
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.

Shiver answered 5/5, 2016 at 10:38 Comment(1)
hi, how can we implement with php, unable to find perfect way to get the value of payloadAmish
I
1

This is how you read back the payload:

$payload = $input['entry'][0]['messaging'][0]['postback']['payload'];
Indistinct answered 16/6, 2017 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.