私は現在、新しいFBメッセンジャーAPIで動作するようにchatbotアプリケーションを作成しています。私は、herokuでホストされているバックエンドアプリを実行するためにPythonのFlaskを使用しています。今、私のサーバーが通常の方法で応答を送信するのに苦労しています。ここで私はアプリにPOSTリクエストを処理するために書かれているコードは次のとおりです。私は私のアプリをmessengeときFBメッセンジャー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要求を解釈し続けるためだと思っていますが、なぜ私がアプリケーションを一度だけ壊すとFBが私のサーバーにPOST要求を送信し続けるのかわかりません。
これは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初心者のビットだとして、任意の助けをいただければ幸いです!
多くの助けになりました。ありがとう! – MEric