@
xpresslink 谢谢老哥,我现在用 flask 的一个加工一下,token 过了,然后从微信 post 过来的请求跳转到一个 get 应答上没做好,代码如下:
# -*- coding:utf-8 -*-
from flask import Flask
from flask import request
from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot
import hashlib
bot = ChatBot("hugbot", read_only=True)
bot.set_trainer(ListTrainer)
bot.train("ListTrainer")
app = Flask(__name__)
app.debug = True
@
app.route('/wx_flask',methods=['GET','POST'])
def wechat():
if request.method == 'GET':
#这里改写你在微信公众平台里输入的 token
token = 'weixin'
#获取输入参数
data = request.args
signature = data.get('signature','')
timestamp = data.get('timestamp','')
nonce = data.get('nonce','')
echostr = data.get('echostr','')
#字典排序
list = [token, timestamp, nonce]
list.sort()
s = list[0] + list[1] + list[2]
#sha1 加密算法
hascode = hashlib.sha1(s.encode('utf-8')).hexdigest()
#如果是来自微信的请求,则回复 echostr
if hascode == signature:
return echostr
else:
return ""
if request.method == 'POST':
if request.form['user_input']=='*':
return redirect(url_for('get_response',username=request.form['user_input']))
@
app.route('/get_response')
def get_response(user_input):
response = bot.get_response(user_input).text
return {"response":response}
if __name__ == '__main__':
app.run(port=8000)
报错信息如下:
127.0.0.1 - - [08/Mar/2018 10:24:06] "POST /wx_flask?signature=b10cc129d5ab20da719de490a06c07d388a58c03×tamp=1520475846&nonce=1060485406&openid=oCIOX1YmRCgRvKC_DOD0TqHI50YI HTTP/1.1" 400 -