brcehuai8
2018-03-07 16:03:25 +08:00
求教,我这个 flask 框架写的东西可以验证 token,无法用机器人
# -*- coding:utf-8 -*-
from flask import Flask
from flask import request
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import hashlib
app = Flask(__name__)
app.debug = True
bot = ChatBot("bot", read_only=True)
bot.set_trainer(ListTrainer)
bot.train("ListTrainer")
@app.route('/get_respnse',methods=['GET','POST'])
def response(user_input):
if request.method == 'POST':
response = bot.get_response(user_input).text
return {"response":response}
@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 __name__ == '__main__':
app.run(port=8000)