WeRoBot是最新的服务很多的开发微信公众号的一款基于Python的微信机器人框架,里面的功能也是非常的强大的,最新的版本更是新加以及修复了很多你需要的功能!
WeRoBot怎么用 使用说明
首先看看怎么用
[python] view plain copy 在CODE上查看代码片派生到我的代码片
from .weixin import handler as HD 聽
@HD.subscribe 聽
def subscribe(xml): 聽
return "welcome to brain" 聽
@HD.unsubscribe 聽
def subscribe(xml): 聽
print "leave" 聽
return "leave 聽brain" 聽
上面处理了关注和取关事件,通过装饰器处理的还算透明。
处理文本消息,回复图文消息如下:
[python] view plain copy 在CODE上查看代码片派生到我的代码片
@HD.text 聽
def text(xml): 聽
content = xml.Content 聽
if content == "111": 聽
return {"Title":"美女", "Description":"比基尼美女", "PicUrl":"http://9smv.com/static/mm/uploads/150411/2-150411115450247.jpg", "Url":"http://9smv.com/beauty/list?category=5"} 聽
elif content == "222": 聽
return [ 聽
["比基尼美女", "比基尼美女", "http://9smv.com/static/mm/uploads/150411/2-150411115450247.jpg", "http://9smv.com/beauty/list?category=5"], 聽
["长腿美女", "长腿美女", "http://9smv.com/static/mm/uploads/150506/2-150506111A9648.jpg", "http://9smv.com/beauty/list?category=8"] 聽
] 聽
elif content == "push": 聽
Helper.send_text_message(xml.FromUserName, "推送消息测试") 聽
return "push ok" 聽
return "hello world" 聽
如何文本是111或222,我们回复图文消息,如何使push,我们使用客服接口推送消息,其它返回“hello world"
一般我们会使用oauth网页授权获取用户的openid,如果是多个链接都需要通过oauth处理,代码会很难看,通过装饰器可以很好的处理这个问题。
[python] view plain copy 在CODE上查看代码片派生到我的代码片
def sns_userinfo_callback(callback=None): 聽
"""网页授权获取用户信息装饰器聽
callback(openid, userinfo):聽
return user聽
""" 聽
def wrap(func): 聽
@wraps(func) 聽
def inner(*args, **kwargs): 聽
request = args[0] 聽#django第一个参数request 聽
openid = request.COOKIES.get('openid') 聽
userinfo = None 聽
if not openid: 聽
code = request.GET.get("code") 聽
if not code: 聽
current = "http://"+ request.get_host() + request.get_full_path() 聽
return redirect(WeixinHelper.oauth2(current)) 聽
else: 聽
data = json.loads(WeixinHelper.getAccessTokenByCode(code)) 聽
access_token, openid, refresh_token = data["access_token"], data["openid"], data["refresh_token"] 聽
#WeixinHelper.refreshAccessToken(refresh_token) 聽
userinfo = json.loads(WeixinHelper.getSnsapiUserInfo(access_token, openid)) 聽
else: 聽
ok, openid = Helper.check_cookie(openid) 聽
if not ok: 聽
return redirect("/") 聽
request.openid = openid 聽
if callable(callback): 聽
request.user = callback(openid, userinfo) 聽
response = func(request) 聽
return response 聽
return inner 聽
return wrap 聽
sns_userinfo = sns_userinfo_callback() 聽
在所有需要用户openid的函数前使用sns_userinfo装饰器就可以了,callback函数接收openid,userinfo,返回用户实例,这样
就可以使用request.user获取当前用户
[python] view plain copy 在CODE上查看代码片派生到我的代码片
@sns_userinfo 聽
def oauth(request): 聽
"""网页授权获取用户信息""" 聽
resp = HttpResponse(request.openid) 聽
resp.set_cookie("openid", Helper.sign_cookie(request.openid)) 聽
return resp 聽
使用oauth需要保存cookie,不然每次用户请求都需要授权,需要走一遍完整的oauth流程,拖慢整体响应。
WeRoBot 中文版更新日志
增加对消息加解密的支持
重写 werobot.messages, 完善对 Event 的支持
将微信消息的 id 属性重命名为 message_id
增加 werobot.reply.SuccessReply
增加 werobot.reply.ImageReply
增加 werobot.reply.VoiceReply
增加 werobot.reply.VideoReply
删除 werobot.reply.create_reply()
为 werobot.reply.WeChatReply 增加 process_args 方法
为 werobot.robot.BaseRoBot 增加 parse_message 方法
为 werobot.robot.BaseRoBot 增加 get_encrypted_reply 方法
删去了 Reply 中过时的 flag
修复 werobot.session.filestorage.FileStorage 在 PyPy 下的兼容性问题
增加 werobot.session.sqlitestorage.SQLiteStorage
将默认的 SessionBackend 切换为 werobot.session.sqlitestorage.SQLiteStorage
将图文消息单个消息的渲染函数放到 werobot.replies.Article 内
取消对 Python2.6, Python3.3 的支持
增加与 Django 1.6+, Flask, Bottle, Tornado 集成的支持
替换 inspect.getargspec()
下载仅供下载体验和测试学习,不得商用和正当使用。
下载体验