一个计算机技术爱好者与学习者

0%

好好学Python:Django开发微信公众平台管理系统——第1章

1. 前言

紧接着《Django开发微信公众平台管理系统——第0章》,本文学习一下微信公众平台各种消息的接收和回复,主要参考WeRoBot文档

消息类型包括:TextMessage、ImageMessage、LinkMessage、LocationMessage、VoiceMessage、VideoMessage、UnknownMessage;
事件类型包括:SubscribeEvent、UnSubscribeEvent、ScanEvent、ScanCodePushEvent、ScanCodeWaitMsgEvent、PicSysphotoEvent、PicPhotoOrAlbumEvent、PicWeixinEvent、LocationSelectEvent、ClickEvent、ViewEvent、LocationEvent、TemplateSendJobFinishEvent、UserScanProductEvent、UserScanProductEnterSessionEvent、UserScanProductAsyncEvent、UserScanProductVerifyActionEvent、CardPassCheckEvent、CardNotPassCheckEvent、UserGetCardEvent、UserGiftingCardEvent、UserDelCardEvent、UserConsumeCardEvent、UserPayFromPayCellEvent、UserViewCardEvent、UserEnterSessionFromCardEvent、UpdateMemberCardEvent、CardSkuRemindEvent、CardPayOrderEvent、SubmitMembercardUserInfoEvent、UnknownEvent;
回复类型包括:TextReply、ImageReply、VoiceReply、VideoReply、ArticlesReply、MusicReply、TransferCustomerServiceReply、SuccessReply。

2. 原理

微信发给 /main/wechat/ 接口的信息(message),都传给了robot对象,robot对象根据消息类型调用自己的handler,处理后返回结果给微信。

3. 实现

3.1. 根据消息类型返回信息

修改 wecms/main/views_wechat.py ,内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import werobot
robot = werobot.WeRoBot(token='vkwechat')

@robot.handler
def index(message):
return 'Today is wonderful day!'

@robot.text
def text(message):
return '您发送了文本消息,内容为:' + message.content

@robot.image
def image(message):
return '您发送了图片消息,图片为:' + message.img

@robot.link
def link(message):
return '您发送了链接消息,链接为:' + message.url

@robot.location
def location(message):
return '您发送了位置消息,位置为:' + message.label

@robot.voice
def voice(message):
return '您发送了声音消息,media_id为:' + message.media_id

@robot.video
def video(message):
return '您发送了视频消息,media_id为:' + message.media_id

此时给公众号发送不同类型的消息,返回的内容也是不同的。

3.2. 返回图片消息

1、登录微信公众平台,查看“公众号开发信息”中的“开发者ID(AppID)”和“开发者密码(AppSecret)”,并记录下来。

2、修改 wecms/main/views_wechat.py ,添加AppID和AppSecret的配置,添加media函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import werobot
from werobot.replies import ImageReply

robot = werobot.WeRoBot(token='vkwechat')
robot.config['APP_ID'] = 'app_id'
robot.config['APP_SECRET'] = 'app_secret'
client = robot.client

@robot.handler
def index(message):
return 'Today is wonderful day!'

@robot.filter('image')
def media(message):
media_id = client.upload_permanent_media('image', open(r'C:\Users\haojin\Desktop\favicon.png', 'rb'))['media_id']
reply = ImageReply(message=message, media_id=media_id)
return reply

# other code

需要注意的是,robot.filter(‘image’)需要放在robot.text的前面。因为werobot是链式匹配的,如果robot.text在前,匹配上了“image”,就会被text()函数处理。

2、查看本机的出口IP,修改“公众号开发信息”中的“IP白名单”,把出口IP填进去。

此时给公众号发送“image”,会返回一张图片。

3.3. 事件处理

以订阅事件和取消事件为例,添加事件处理:

1
2
3
4
5
6
7
8
9
@robot.subscribe
def subscribe(event):
print('用户' + event.source + '关注了公众号')
return '感谢关注voidking,您的ID为:' + event.source

@robot.unsubscribe
def unsubscribe(event):
print('用户' + event.source + '取消了关注')
return ''

此时关注公众号,会收到自己的OpenID。

4. 配置文件

前面的开发中,用到了token、APP_ID和APP_SECRET。这些信息应该写在配置文件中,而不是代码中,因此需要调整。

1、编辑 wecms/wecms/settings.py,添加:

1
2
3
4
# Wechat Config
TOKEN = 'vkwechat'
APP_ID = 'app_id'
APP_SECRET = 'app_secret'

2、编辑 wecms/main/views_wechat.py ,修改为:

1
2
3
4
5
6
7
8
9
10
from django.conf import settings
import werobot
from werobot.replies import ImageReply

robot = werobot.WeRoBot(token=settings.TOKEN)
robot.config['APP_ID'] = settings.APP_ID
robot.config['APP_SECRET'] = settings.APP_SECRET
client = robot.client

# other code

5. 源码分享

https://github.com/voidking/wecms/releases/tag/v0.1.0

  • 本文作者: 好好学习的郝
  • 原文链接: https://www.voidking.com/dev-django-wechat-1/
  • 版权声明: 本文采用 BY-NC-SA 许可协议,转载请注明出处!源站会即时更新知识点并修正错误,欢迎访问~
  • 微信公众号同步更新,欢迎关注~