1
votes

I'm trying to define a 'Payload' in Ruby for my Facebook Messenger bot. My use case is, when a user sees structured content (a story with a button) and they click the button, instead of sending them to a URL, I want to hit my webhook and return more structured content.

So, I've tried several iterations of this and not sure where I'm getting tripped up. I've started by adding a messaging event:

messaging_event["entry"].first["messaging"].each do |msg|
     puts msg
     sender = msg["sender"]["id"]
     if msg["message"] && msg["message"]["text"]
       payload = msg["message"][“payload”]

I did something very similar for adding text, which was:

post '/webhook/' do
    messaging_event = JSON.parse(request.body.read)
    puts messaging_event
    messaging_event["entry"].first["messaging"].each do |msg|
      puts msg
      sender = msg["sender"]["id"]
      if msg["message"] && msg["message"]["text"]
        text = msg["message"]["text"]
        puts "Sender ID: #{sender}, Text: #{text}"

So, at this point, I'm not sure if I need to further def the payload like:

def payload(sender, payload)
     data = {
       recipient: { id: sender },
       message: payload
     }
     send_message(data)
   end

Or, if I simply need to create a variable and call that variable within my Module object like:

module Messages
    SIMPLE_ONE_BUTTON_PAYLOAD = {
"attachment":{
      "type":"template",
      "payload":{
        "template_type":"button",
        "text":"Here's a simple button message",
        "buttons":[
          "type":"postback",
            "title":"Button One",
            "payload":"[variable I need to call]",
        ]
      }
    }
  }
end

So, there's really two things here -- Do I need the payload messaging event and to define the payload and what do I need to call within the object? Any thoughts or feedback here would be really helpful. Thanks in advance!

1
Not sure if I am too late, but I created a gem to handle these use cases: github.com/tinkerbox/message_quickly - Jaryl

1 Answers

1
votes

You need to handle postbacks in your code. When a user hits a button, facebook calls your webhook with a post back (unless it's a url).

From the docs:

Postbacks are back end calls to your webhook when buttons are tapped. These calls contain the payload that is set for the button. Buttons on Structured Messages support opening URLs and postbacks.

The structure of postback is different than a text message.

Text Message:

{"object":"page","entry":[{"id":654321,"time":1460624758100,"messaging":[{"sender":{"id":123456},"recipient":{"id":654321},"timestamp":1460624758089,"message":{"mid":"mid.987654","seq":12632,"text":"This is the message."}}]}]}

Postback:

{"object":"page","entry":[{"id":654321,"time":1460625294253,"messaging":[{"sender":{"id":123456},"recipient":{"id":654321},"timestamp":1460625294253,"postback":{"payload":"Payload defined in the button"}}]}]}

You're doing if msg["message"] && msg["message"]["text"] but a post back doesn't have the ["message"] element. You need to make another case for msg["postback"].

So, change this:

if msg["message"] && msg["message"]["text"]
  payload = msg["message"]["payload"]

to this:

if msg["postback"] && msg["postback"]["payload"]
  payload = msg["postback"]["payload"]

You can read further on handling postbacks here in the official documentation (Point 8 Handling Postbacks).