安装itchat
pip install itchat关于itchat API的讲解,这里比较清楚详细,我这写作水平也讲不清楚,大家看这儿吧主要讲讲我的代码,和实现过程、效果需要的库import reimport timeimport itchatfrom itchat.content import *下面的代码,仅仅实现了记录文本信息@itchat.msg_register([TEXT, PICTURE, MAP, CARD, NOTE, SHARING, RECORDING, ATTACHMENT, VIDEO])def text_reply(msg): PRint(msg['Text'])itchat.auto_login()itchat.run()消息注册:@itchat.msg_register([TEXT, PICTURE, MAP, CARD, NOTE, SHARING, RECORDING, ATTACHMENT, VIDEO])关于消息类型,详细的可以参考上面给出的链接在这里,除了好友邀请和无用消息都进行了注册登录itchat.auto_login() 登录的是你个人微信的web端,然后会弹出一个二维码,用微信扫一扫即可登录。itchat.run()自动运行,检测消息然后对上面代码进行扩充。造血造肉。
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, NOTE, SHARING, RECORDING, ATTACHMENT, VIDEO])def text_reply(msg): if msg['Type'] == 'Text': reply_content = msg['Text'] elif msg['Type'] == 'Picture': reply_content = r"图片: " + msg['FileName'] elif msg['Type'] == 'Card': reply_content = r" " + msg['RecommendInfo']['NickName'] + r" 的名片" elif msg['Type'] == 'Map': x, y, location = re.search("<location x=/"(.*?)/" y=/"(.*?)/".*label=/"(.*?)/".*", msg['OriContent']).group(1, 2, 3) if location is None: reply_content = r"位置: 纬度->" + x.__str__() + " 经度->" + y.__str__() else: reply_content = r"位置: " + location elif msg['Type'] == 'Note': reply_content = r"通知" elif msg['Type'] == 'Sharing': reply_content = r"分享" elif msg['Type'] == 'Recording': reply_content = r"语音" elif msg['Type'] == 'Attachment': reply_content = r"文件: " + msg['FileName'] elif msg['Type'] == 'Video': reply_content = r"视频: " + msg['FileName'] else: reply_content = r"消息" friend = itchat.search_friends(userName=msg['FromUserName']) itchat.send(r"Friend:%s -- %s " r"Time:%s " r" Message:%s" % (friend['NickName'], friend['RemarkName'], time.ctime(), reply_content), toUserName='filehelper') itchat.send(r"我已经收到你在【%s】发送的消息【%s】稍后回复。--微信助手(Python版)" % (time.ctime(), reply_content), toUserName=msg['FromUserName'])itchat.auto_login()itchat.run()开头的各种if else是为了判断消息类型进行各种不同的回复然后是消息的发送函数send(message, toUserName)发送的消息和发送对象防撤回功能是原理是将消息备份发送到文件助手toUserName='filehelper',由于是接入微信API,读取消息会比手机端快一些。
自动回复功能是将发送对象改为给你发送消息的好友,大家可以再自定义其他消息回复规则实现个性化回复,针对不同好友的回复。
其他功能还在思考中...过几天试试通过手机端发送消息到文件助手,电脑端处理消息,解析命令而执行命令。敬请指导、关注注:如果不知道msg还有哪些参数,可以通过print(msg)来查看详细信息,真的特别详细。喜欢点个赞再走呗(/≧▽≦)/~┴┴
新闻热点
疑难解答