2016-05-13 156 views
1

我目前正在努力讓chatbot應用程序與新的FB messenger API一起工作。我正在使用Python的Flask運行一個後端應用程序,該應用程序託管在heroku上。現在,我讓我的服務器以正常方式發送響應時遇到了一些問題。這是我寫來處理POST請求到應用程序的代碼:FB messenger API太多回復

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

當您在FB開發人員控制檯中設置您的應用程序時,您可以在定義Messenger webhook時訂閱各種事件類型。

看起來在您的日誌記錄中,從FB接收的第二個事件可能是messages_deliveries事件,而您的代碼當前不區分這些不同的事件類型,因此也不區分響應流。

對於初始測試,您可以嘗試訂閱消息事件類型,然後根據需要添加其他消息。回發可能是您在結構化聊天漫遊器設計中最常用的一種。

希望這會有所幫助 - 凸輪

+0

這幫了很大的忙。謝謝! – MEric