I'm trying to make a direct POST request to Facebook, from my model (user.rb), to send a simple message to this user in Messenger. But I get this error:
{"error":{"message":"(#100) param recipient must be non-empty.","type":"OAuthException","code":100,"fbtrace_id":"F07BNxzgciW"}}
Here is my method:
def send_message(msg)
url = "https://graph.facebook.com/v2.6/me/messages?access_token="+ENV['PAGE_ACCESS_TOKEN']
data = {
'recipient': {
'id': self.facebook_id
},
'message': {
'text': msg
}
}
#data = JSON.pretty_generate(data)
puts data
uri = URI(url)
res = Net::HTTP.post_form(uri, data)
puts res.body
end
Seems like data is not in the right format, so the user id is not beeing recognized.
Uncommenting the #data line I get this error, because data is a string, and not a Hash:
NoMethodError: undefined method `map' for #<String:0x000000034bd248>
This is the request example from Facebook Docs that I'm trying to reproduce:
curl -X POST -H "Content-Type: application/json" -d '{
"recipient": {
"id": "USER_ID"
},
"message": {
"text": "hello, world!"
}
}' "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"