Reputation: 966
I'm currently working on getting a chatbot app to work with the new FB messenger API. I'm using Python's Flask to run a backend app, which is being hosted on heroku. Right now, I'm having a bit of trouble getting my server to send responses in a normal fashion. Here is the code I have written to handle POST requests to the app:
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 = "OMITTED"
count = 0
@app.route('/', methods=["POST"])
def chatbot_response():
global count
req_data = request.data
data = json.loads(req_data)
req_args = request.args
print "Data: ", data
sender_id = data["entry"][0]["messaging"][0]["sender"]["id"]
send_back_to_fb = {
"recipient": {"id": sender_id}, "message": { "text": "sending it back"+str(count)}
}
count += 1
print "Send back to fb: ", send_back_to_fb
params_input = {"access_token": FB_TOKEN, "recipient": sender_id}
headers = {'content-type': 'application/json'}
# the big change: use another library to send an HTTP request back to FB
fb_response = requests.post(FB_MESSAGES_ENDPOINT, headers=headers, params=params_input, data=json.dumps(send_back_to_fb))
print "response status code: ", fb_response.status_code, " ", fb_response.text
# 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)
print "whoa there buddy"
# always return 200 to Facebook's original POST request so they know you
# handled their request
return "Ok", 200
if __name__ == '__main__':
app.run(host="0.0.0.0")
Right now when I messenge my app, I get a continuous stream of responses of the form:
sending it back0
sending it back1
sending it back0
sending it back2
sending it back1
sending it back3
sending it back4
sending it back5
sending it back2
sending it back6
sending it back7
sending it back8
sending it back9
sending it back3
sending it back4
sending it back10
sending it back11
sending it back12
sending it back5
sending it back6
sending it back7
sending it back8
sending it back13
Why does my app keep sending responses to the user that messenges it when prompted only response? I figure this is because FB keeps interpreting POST requests, but I'm not quite sure I follow why POST requests continue to be sent by FB to my server if I only messenge the app once?
This is part of the heroku logs:
Data: {u'object': u'page', u'entry': [{u'time': 1463097986863, u'id': 267701720229635, u'messaging': [{u'sender': {u'id': 1022501574495987}, u'recipient': {u'id': 267701720229635}, u'message': {u'seq': 334, u'mid': u'mid.1463097986829:5267967865d8ca4230', u'text': u'alright break'}, u'timestamp': 1463097986837}]}]}
2016-05-13T00:06:27.342096+00:00 app[web.1]: Send back to fb: {'recipient': {'id': 1022501574495987}, 'message': {'text': 'sending it back0'}}
2016-05-13T00:06:28.034903+00:00 app[web.1]: response status code: 200 {"recipient_id":"1022501574495987","message_id":"mid.1463097987637:2dec6b0062f98e1832"}
2016-05-13T00:06:28.034916+00:00 app[web.1]: whoa there buddy
2016-05-13T00:06:28.087649+00:00 heroku[router]: at=info method=POST path="/" host=gentle-plateau-81579.herokuapp.com request_id=09b6fdf9-9d4a-4620-b522-f91682e20469 fwd="31.13.110.121" dyno=web.1 connect=1ms service=703ms status=200 bytes=161
2016-05-13T00:06:28.713916+00:00 app[web.1]: Data: {u'object': u'page', u'entry': [{u'time': 1463097988125, u'id': 267701720229635, u'messaging': [{u'sender': {u'id': 1022501574495987}, u'recipient': {u'id': 267701720229635}, u'delivery': {u'watermark': 1463097987675, u'seq': 336, u'mids': [u'mid.1463097987637:2dec6b0062f98e1832']}}]}]}
2016-05-13T00:06:28.714027+00:00 app[web.1]: Send back to fb: {'recipient': {'id': 1022501574495987}, 'message': {'text': 'sending it back1'}}
2016-05-13T00:06:29.321337+00:00 heroku[router]: at=info method=POST path="/" host=gentle-plateau-81579.herokuapp.com request_id=bebdf9ab-4bc5-416c-b7f0-1f5efd0b5351 fwd="31.13.102.98" dyno=web.1 connect=1ms service=608ms status=200 bytes=161
As I'm a bit of webdev newbie, any help would be greatly appreciated!
Upvotes: 1
Views: 623
Reputation: 136
When you set up your app in the FB developer console, you had various event types you could subscribe to when defining the Messenger webhook.
It looks like in your logging the 2nd event received from FB is likely the messages_deliveries event and your code currently isn't differentiating between these different event types and hence the stream of responses.
For initial testing you could try just subscribing to the messages event type and then add in the others as required I.e. postback is probably the one you will use the most in a structured chat bot design.
Hope this helps - cam
Upvotes: 1