老哥们好,我现在用 chatterbot 做了个聊天机器人,用 python3 hug 框架封装好 API 了,路径是 xxx/get_response?user_input=
微信公众号的 token 验证需要返回哈希值所以机器人的应答路径用不了,重新定义了一个 get 路径,/wx_token。
那么问题来了,我的 hug 该怎么写才能又通过微信公众平台 token 验证,又可以 get_response?user_input 呢
这是现在 hug 的代码:
#!/usr/bin/env python
# coding: utf-8
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from hug import request
import hug
bot = ChatBot("hugbot", read_only=True)
bot.set_trainer(ListTrainer)
bot.train("ListTrainer")
@hug.get()
def get_response(user_input):
response = bot.get_response(user_input).text
return {"response":response}
@hug.get('/wx_token')
def wx_token(signature, timestamp, nonce, echostr):
if request.method == 'GET'
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 ""
微信公众号的 token 验证需要返回哈希值所以机器人的应答路径用不了,重新定义了一个 get 路径,/wx_token。
那么问题来了,我的 hug 该怎么写才能又通过微信公众平台 token 验证,又可以 get_response?user_input 呢
这是现在 hug 的代码:
#!/usr/bin/env python
# coding: utf-8
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from hug import request
import hug
bot = ChatBot("hugbot", read_only=True)
bot.set_trainer(ListTrainer)
bot.train("ListTrainer")
@hug.get()
def get_response(user_input):
response = bot.get_response(user_input).text
return {"response":response}
@hug.get('/wx_token')
def wx_token(signature, timestamp, nonce, echostr):
if request.method == 'GET'
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 ""