V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  brcehuai8  ›  全部回复第 1 页 / 共 1 页
回复总数  7
2018-03-08 10:25:39 +08:00
回复了 brcehuai8 创建的主题 Python 微信公众号接口与 hug 框架问题
@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&timestamp=1520475846&nonce=1060485406&openid=oCIOX1YmRCgRvKC_DOD0TqHI50YI HTTP/1.1" 400 -
@geek123 老哥,这个代码可以过 token 验证,就是报 http 500 的错误,你倒是认真看一下再回复啊
@yuhr123 老哥,我定义的机器人无法工作,404 能帮我看看吗
求教,我这个 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)
2018-03-07 16:01:30 +08:00
回复了 brcehuai8 创建的主题 Python 微信公众号接口与 hug 框架问题
换了 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)
2018-03-07 14:39:49 +08:00
回复了 brcehuai8 创建的主题 Python 微信公众号接口与 hug 框架问题
重新用 hug 写了一个只做微信 token 接口的代码
# -*- coding:utf-8 -*-

import hug
from hug import request

import hashlib


@hug.get('/wx_token')
def wechat():
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)
也报这个错
AttributeError: module 'falcon.request' has no attribute 'args'
127.0.0.1 - - [07/Mar/2018 14:38:39] "GET /wx_token?signature=c9c860983ab4bc4322a39023a14d7dc7d3ecc888&echostr=8708986455401151215&timestamp=1520404719&nonce=786176401 HTTP/1.1" 500 59
2018-03-07 14:27:11 +08:00
回复了 brcehuai8 创建的主题 Python 微信公众号接口与 hug 框架问题
@xpresslink 老哥。微信的 token 是 get 这个是没问题的,我把 get_response 改成 post 方法就 127.0.0.1 - - [07/Mar/2018 14:24:56] "GET /post_response?user_input=hi HTTP/1.1" 405 0 报错了,能不能详细讲解一下,谢谢
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2678 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 10:13 · PVG 18:13 · LAX 02:13 · JFK 05:13
Developed with CodeLauncher
♥ Do have faith in what you're doing.