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!