我有一个flask应用程序部署到Heroku,并希望从Chatfuel (机器人构建平台)接收文本,并发送回文本作为回报。
现在,我所做的是使用我的heroku应用程序作为一个web-hook,这样Chatfuel就可以对我的API进行简单的GET或POST查询。问题是我没有使用Flask或API的经验,所以我不确定我的应用程序如何接收信息( json格式)并将其发送回chatfuel。
到目前为止,我是这样写的:
import os
import sys
import json
import requests
from flask import Flask, jsonify, render_template, request
app = Flask(__name__)
@app.route('/', methods=['GET'])
def verify():
# when the endpoint is registered as a webhook, it must echo back
# the 'hub.challenge' value it receives in the query arguments
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
if not request.args.get("hub.verify_token") == os.environ["VERIFY_TOKEN"]:
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "Hello world", 200
@app.route("/json", methods=['GET','POST'])
def json():
url = "chatfuel_api"
data = json.load(urllib2.urlopen(url))
if request.json:
mydata = request.json
return "Thanks",200
else:
return "no json received"
@app.route('/hello', methods = ['GET','POST'])
def api_echo():
if request.method == 'GET':
return "ECHO: GET\n",200
if __name__ == '__main__':
app.run(debug=True)verify()函数起作用了,因为如果我在本地运行应用程序,我会看到一条'Hello world‘消息。然而,json()和api_echo()都不能工作,当我的服务器收到来自chatfuel的get或post请求时,它会返回一个404错误。
如你所见,我真的有很多困惑,你的帮助将是无价的。
谢谢
发布于 2017-05-07 10:38:59
您需要确保您已经向Chatfuel注册了正确的webhook url。对于您当前已有的代码,要命中json端点,url应为https://www.your_server.com/json
验证路由看起来像是FB发送的集线器质询,因此您必须使用FB注册站点的根目录(即,使用当前代码)才能使用验证功能。该url将如下所示的https://www.your_site.com/
https://stackoverflow.com/questions/43617586
复制相似问题