写在前面
《使用Python进行微信公众号开发》系列文章将与大家分享如何使用Python一步步搭建微信公众号后台服务器。
效果体验
扫码“是雯子吖”公众号进行体验
配置自己的公众号后台进行体验
想要体验效果的小伙伴可以将微信公众号后台的 开发-> 基本配置 -> 服务器配置 中的Token随意配置,服务器地址配置为 https://apit.toodo.fun/wechatMP/ + Token,如
Token = hello # 可随意设置,但是要保证与服务器地址最后一项相同
服务器地址:https://apit.toodo.fun/wechatMP/hello
消息加解密方式:明文模式或者兼容模式
如果配置成功的话,新关注你公众号的用户将会收到“Hi~ 终于等到你”,并且收到消息后会由我服务器上的机器人自动回复。
项目地址
GitHub:https://github.com/MR5356/toodoWechat
回复消息
当用户发送消息给公众号时,我们的服务器会收到一个POST请求,里面是XML格式的数据包,在上一篇文章中我们已经分享了如何接收消息,本篇文章我们来分享一下如何回复消息。
回复消息公共内容
所有的消息回复都应该是XML格式的数据包,里面必须包含以下内容
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
</xml>
我们先生成字典格式的回复内容,然后再使用xmltodict将字典格式的转换为XML格式的数据包。
def replyMsg(self, msg):
return {
'xml': {
'ToUserName': msg.get('FromUserName'),
'FromUserName': msg.get('ToUserName'),
'CreateTime': int(time.time()),
}
}
其中,ToUserName是接收到的消息中的FromUserName(用户id),同样FromUserName是接收到的消息中的ToUserName(公众号id),CreateTime是当前的时间戳。
回复文本消息
回复文本消息除公共部分外,还需要MsgType和回复的文本Content
def replyText(self, msg, text):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'text'
res['xml']['Content'] = text
return res
回复图片消息
回复图片消息,除公共部分外,还需要提供MsgType和图片的MediaId
def replyImage(self, msg, MediaId):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'image'
res['xml']['Image'] = {'MediaId': MediaId}
return res
回复语音消息
回复语音消息,除公共部分外,还需要提供MsgType和语音的MediaId
def replyVoice(self, msg, MediaId):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'voice'
res['xml']['Voice'] = {'MediaId': MediaId}
return res
回复视频消息
回复视频消息,除公共部分外,还需要提供MsgType和视频的MediaId,可选参数为视频的标题Title和简介Description
def replyVideo(self, msg, MediaId, title=None, desc=None):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'video'
res['xml']['Video'] = {} # {'MediaId': MediaId, 'Title': title, 'Description': desc}
res['xml']['Video']['MediaId'] = MediaId
if title: res['xml']['Video']['Title'] = title
if desc: res['xml']['Video']['Description'] = desc
return res
回复音乐消息
回复音乐消息,除公共部分外,还需要提供MsgType和音乐的封面ThumbMediaId,可选参数为音乐的标题Title,音乐的简介Description,音乐播放地址MusicURL和高清音乐播放链接HQMusicUrl
def replyMusic(self, msg, pic, title=None, desc=None, url=None, hqUrl=None):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'music'
res['xml']['Music'] = {'ThumbMediaId': pic}
if title: res['xml']['Music']['Title'] = title
if desc: res['xml']['Music']['Description'] = desc
if url: res['xml']['Music']['MusicURL'] = url
if hqUrl: res['xml']['Music']['HQMusicUrl'] = hqUrl
return res
回复图文消息
回复图文消息,除公共部分外,还需要提供MsgType和图文数量ArticleCount,以及图文列表(因为被动回复消息只能回复一条消息,所以这里不考虑多图文情况)
def replyArticles(self, msg, title, desc, pic, url):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'news'
res['xml']['ArticleCount'] = 1
res['xml']['Articles'] = {'item': {'Title': title, 'Description': desc, 'PicUrl': pic, 'Url': url}}
return res
本小节代码汇总
完整代码可参考上文中的Github链接
class WechatMP:
def replyMsg(self, msg):
return {
'xml': {
'ToUserName': msg.get('FromUserName'),
'FromUserName': msg.get('ToUserName'),
'CreateTime': int(time.time()),
}
}
def replyText(self, msg, text):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'text'
res['xml']['Content'] = text
return res
def replyImage(self, msg, MediaId):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'image'
res['xml']['Image'] = {'MediaId': MediaId}
return res
def replyVoice(self, msg, MediaId):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'voice'
res['xml']['Voice'] = {'MediaId': MediaId}
return res
def replyVideo(self, msg, MediaId, title=None, desc=None):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'video'
res['xml']['Video'] = {} # {'MediaId': MediaId, 'Title': title, 'Description': desc}
res['xml']['Video']['MediaId'] = MediaId
if title: res['xml']['Video']['Title'] = title
if desc: res['xml']['Video']['Description'] = desc
return res
def replyMusic(self, msg, pic, title=None, desc=None, url=None, hqUrl=None):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'music'
res['xml']['Music'] = {'ThumbMediaId': pic}
if title: res['xml']['Music']['Title'] = title
if desc: res['xml']['Music']['Description'] = desc
if url: res['xml']['Music']['MusicURL'] = url
if hqUrl: res['xml']['Music']['HQMusicUrl'] = hqUrl
return res
def replyArticles(self, msg, title, desc, pic, url):
res = self.replyMsg(msg)
res['xml']['MsgType'] = 'news'
res['xml']['ArticleCount'] = 1
res['xml']['Articles'] = {'item': {'Title': title, 'Description': desc, 'PicUrl': pic, 'Url': url}}
return res
系列文章
使用Python进行微信公众号开发(一)服务器对接:https://blog.youkuaiyun.com/m0_49475727/article/details/115236330
使用Python进行微信公众号开发(二)接收消息:https://blog.youkuaiyun.com/m0_49475727/article/details/115261389
参考文档
开发者文档-被动回复用户消息:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Passive_user_reply_message.html
下面是我自己运营的微信小程序“但行趣事”和公众号“微电脑”,更多的技术文章以及视频我会放到小程序和公众号当中,有志同道合的小伙伴也可以在小程序(联系客服按钮)或者公众号(直接留言)当中联系我们
![]() |
![]() |