Matheus208
Matheus208

Reputation: 1429

"Payload" option on Facebook Bots buttons

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?

Upvotes: 8

Views: 13100

Answers (3)

Dragos Vana
Dragos Vana

Reputation: 66

This is how you read back the payload:

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

Upvotes: 1

ush189
ush189

Reputation: 1540

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.

Upvotes: 5

Juergen
Juergen

Reputation: 662

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

Upvotes: 7

Related Questions