ciqingloveless 2019-06-27
本文面向想通过Python学习公众号开发的同学。一站式解决新手开发微信公众号遇到的所有问题。
为了防止我的文章被到处转载,贴一下我的公众号【智能制造专栏】,欢迎大家关注。
github仓库地址https://github.com/injetlee/Python/tree/master/wechat
本篇文章首先实现与公众号的对接,实现简单的回复文字、图片内容。下一篇会对接腾讯的AI平台,实现对照片的人脸检测与分析,分析年龄、性别、表情、魅力值等。
首先要注册一个公众号,这个很简单,我们到微信公众平台注册就可以了,选择个人订阅号就可以。但是如果可以认证的,我建议选择认证订阅号,因为未认证的号很多功能权限都没有。好了,话不多说进入正题。
.\ngrok.exe http 80
即可启动。启动后如下图所示,图片中网址就是我们需要的import falcon from falcon import uri from wechatpy.utils import check_signature from wechatpy.exceptions import InvalidSignatureException class Connect(object): def on_get(self, req, resp): query_string = req.query_string query_list = query_string.split('&') b = {} for i in query_list: b[i.split('=')[0]] = i.split('=')[1] try: check_signature(token='这里填写配置界面你输入的token', signature=b['signature'], timestamp=b['timestamp'], nonce=b['nonce']) resp.body = (b['echostr']) except InvalidSignatureException: pass resp.status = falcon.HTTP_200 app = falcon.API() connect = Connect() app.add_route('/connect', connect)
waitress-serve --port=80 connect:app
在80端口启动我们的程序。启动后如下图,代表启动成功,这时候我们到微信的配置页面点击提交,会弹出提交成功消息框,代表我们成功接入微信import falcon from wechatpy.utils import check_signature from wechatpy.exceptions import InvalidSignatureException from wechatpy import parse_message from wechatpy.replies import TextReply, ImageReply class Connect(object): def on_get(self, req, resp): query_string = req.query_string query_list = query_string.split('&') b = {} for i in query_list: b[i.split('=')[0]] = i.split('=')[1] try: check_signature(token='xxxxx', signature=b['signature'], timestamp=b['timestamp'], nonce=b['nonce']) resp.body = (b['echostr']) except InvalidSignatureException: pass resp.status = falcon.HTTP_200 def on_post(self, req, resp): xml = req.stream.read() msg = parse_message(xml) if msg.type == 'text': reply = TextReply(content=msg.content, message=msg) xml = reply.render() resp.body = (xml) resp.status = falcon.HTTP_200 app = falcon.API() connect = Connect() app.add_route('/connect', connect)
import falcon from wechatpy.utils import check_signature from wechatpy.exceptions import InvalidSignatureException from wechatpy import parse_message from wechatpy.replies import TextReply, ImageReply class Connect(object): def on_get(self, req, resp): query_string = req.query_string query_list = query_string.split('&') b = {} for i in query_list: b[i.split('=')[0]] = i.split('=')[1] try: check_signature(token='xxxxxxx', signature=b['signature'], timestamp=b['timestamp'], nonce=b['nonce']) resp.body = (b['echostr']) except InvalidSignatureException: pass resp.status = falcon.HTTP_200 def on_post(self, req, resp): xml = req.stream.read() msg = parse_message(xml) if msg.type == 'text': reply = TextReply(content=msg.content, message=msg) xml = reply.render() resp.body = (xml) resp.status = falcon.HTTP_200 elif msg.type == 'image': reply = ImageReply(media_id=msg.media_id, message=msg) xml = reply.render() resp.body = (xml) resp.status = falcon.HTTP_200 app = falcon.API() connect = Connect() app.add_route('/connect', connect)