FB messenger API响应太多

时间:2016-05-13 00:20:59

标签: python facebook-graph-api heroku flask facebook-messenger

我目前正致力于使用聊天机器人应用程序来使用新的FB messenger API。我正在使用Python的Flask运行后端应用程序,该应用程序正在heroku上托管。现在,我在让服务器以正常方式发送响应方面遇到了一些麻烦。以下是我为处理对应用程序的POST请求而编写的代码:

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")

现在,当我发布我的应用程序时,我会得到一个连续的表单响应:

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

为什么我的应用会继续向用户发送响应,仅在提示响应时对其进行消息传递?我认为这是因为FB一直在解释POST请求,但我不太确定我是否遵循为什么POST请求继续由FB发送到我的服务器,如果我只对应用程序进行一次调查?

这是heroku日志的一部分:

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

由于我是一名webdev新手,所以非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

在FB开发者控制台中设置应用程序时,您可以在定义Messenger webhook时订阅各种事件类型。

在您的日志记录中,从FB收到的第二个事件可能是messages_deliveries事件,您的代码目前并不区分这些不同的事件类型,因此也就是响应流。

对于初始测试,您可以尝试只订阅messages事件类型,然后根据需要添加其他类型。回发可能是您在结构化聊天机器人设计中使用最多的那个。

希望这会有所帮助 - cam