Reputation: 966
I am trying to setup a messenger chatbot using the newly released messenger platform api. I setup a Python Flask server hosted on Heroku and have been adapting these instruction to try to get my page receiving the messages my server sends it: https://developers.facebook.com/docs/messenger-platform/quickstart
Thus far I have validated a callback url and have been able to receive messages when I post to my page on FB (i.e. when I message my page which is linked to my app on FB, my heroku logs show that the POST request is being received). However, when I try to send messages from my server to my app, I get the following JSON error response:
400: {"error":{"message":"(#100) param recipient must be non-empty.","type":"OAuthException","code":100,"fbtrace_id":"B3cni+LAmYU"}}
I am using the requests library to send requests to the page. Below is the code I am using to service POST requests:
import json
import os
import requests
from flask import Flask, request
app = Flask(__name__)
FB_MESSAGES_ENDPOINT = "https://graph.facebook.com/v2.6/me/messages"
FB_TOKEN = "EAADKWAcVj...AZDZD"
@app.route('/', methods=["POST"])
def chatbot_response():
req_data = request.data
data = json.loads(req_data)
print "Data: ", data
sender_id = data["entry"][0]["messaging"][0]["sender"]["id"]
print "Sender id: ", sender_id
send_back_to_fb = {
"entry": [{"messaging": [{"recipient": {"id": str(sender_id)}}]}],
"recipient": {
"id": str(sender_id)},
"message": "this is a test response message",
"recipient": str(sender_id), "access_token": FB_TOKEN
}
params_input = {"access_token": FB_TOKEN, "recipient": sender_id}
fb_response = requests.post(FB_MESSAGES_ENDPOINT,
params={"access_token": FB_TOKEN, "recipient": {"id": str(sender_id)}, "json": "recipient": {"id": str(sender_id)}},
data=json.dumps(send_back_to_fb))
print "Json of response: ", fb_response.json()
# handle the response to the subrequest you made
if not fb_response.ok:
# log some useful info for yourself, for debugging
print 'jeepers. %s: %s' % (fb_response.status_code, fb_response.text)
return "OK", 200
if __name__ == '__main__':
app.run(host="0.0.0.0")
I've tried countless different types of key/value encodings of the 'recipients' element into json but none of them seem to be understood by the FB graph service. How can I encode my request so that FB knows what the 'recipient' param is?
Thanks!
Edit:
It turns out I had to manually set the encoding type in the header of the POST request. Adding the following line made it so I could send interpretable text responses to FB:
headers = {'content-type': 'application/json'}
Upvotes: 3
Views: 3857
Reputation: 19
how about python library for using facebook messenger platform?
https://github.com/conbus/fbmq
from flask import Flask, request
from fbmq import Page
page = fbmq.Page(PAGE_ACCESS_TOKEN)
@app.route('/webhook', methods=['POST'])
def webhook():
page.handle_webhook(request.get_data(as_text=True))
return "ok"
@page.handle_message
def message_handler(event):
page.send(event.sender_id, "HI!!!")
Upvotes: 0
Reputation: 2822
You can try these, both should work
headers = {'Content-type': 'application/json'}
response = requests.post(url, data=json.dumps(send_back_to_fb), headers=headers)
OR
response = requests.post(url, json=send_back_to_fb)
Upvotes: 3