2017-04-25 92 views
0

我有一个烧瓶应用程序部署到Heroku,并希望从Chatfuel(bot构建平台)接收文本并发回文本作为回报。连接API并通过Flask应用程序发回消息

现在,我所做的就是使用我的heroku应用程序作为Web钩子,以便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()功能的作品,因为我看到一个“世界,你好”消息,如果我在本地运行的应用程序。但是,json()api_echo()都不起作用,并且当我的服务器收到来自chatfuel的获取或发布请求时,它将返回404错误。

正如你所看到的,我真的很困惑,你的帮助将是非常宝贵的。

感谢

+0

请发送日志或回溯。 – stamaimer

+0

感谢您的评论。我不确定如何在heroku上获得这个。是通过运行'heroku logs -t'吗? – sandrus

+0

这就是我运行heroku时所看到的(我已经替换了真正的主机,请求和fwd值,'up'是我感兴趣的属性的名称):'at = info method = GET path =“/ webhook ?up = Hh&up = Hh“host = x.herokuapp.com request_id = y fwd =”z“dyno = web.1 connect = 0ms service = 8ms status = 404 bytes = 386 protocol = https' – sandrus

回答

0

你需要确保你已经注册与Chatfuel正确的网络挂接网址。对于你现在使用的代码来说,点击json端点url将是https://www.your_server.com/json

验证路由看起来像集线器挑战FB发送的,所以你必须注册你的站点的根目录(也就是说,代码)与FB打击验证功能。该网址看起来像这样https://www.your_site.com/

相关问题