美文网首页Python数据从入门到实践
Python—itchat实现微信自动回复

Python—itchat实现微信自动回复

作者: Sunflow007 | 来源:发表于2020-03-03 23:00 被阅读0次
10.jpg

前言:
Python爬虫和微信结合到一起会产生怎样的火花?原则上,肉眼可见的页面都是可以被爬虫爬取到的,无论是网页还是手机app,即使是微信也不例外,于是乎github上就有大神开源了这样一个库—itchat:littlecodersh/ItChat​github.com

图标

原理是利用爬虫技术和微信网页版的API接口来用Python模拟登陆网页版的微信,可以实现绝大部分网页版微信可以实现的功能!(具体原理详见:抓包及原理入门 - itchat)什么添加删除好友、查看好友头像图片、昵称、签名、消息转发.....都是不在话下,而且由于是软件执行,所以可以进行批量操作!于是乎可以批量群发消息,批量查看好友信息头像等等,也是无比方便!多说无益,itchat官网给出的教程非常清晰,大家可以直接跟着教程一步步尝试下,看看能实现哪些有趣的功能:项目简介 - itchat​itchat.readthedocs.io

1 废话不多说: 11.jpg
#_*_ coding:utf-8 _*_
#__author__='阳光流淌007'
import re
import time
import itchat
from itchat.content import *

@itchat.msg_register([TEXT])
def text_reply(msg):
    friend = itchat.search_friends(userName=msg['FromUserName'])
    replyContent = "收到您于%s发送的【%s】" % (time.strftime('%m-%d %H:%M',time.localtime()), msg['Type'])
    if msg['Type'] == 'Text':
        if re.search(r"快乐",msg['Content']):
            replyContent += "【衷心感谢您的祝福,祝您:新年快乐???,开开心心[耶][耶][耶],身体健康[發][發][發],狗年大吉旺旺旺???】"
            itchat.send('@img@%s' % '/Users/xxx/moneyGod.jpg',toUserName=msg['FromUserName'])
    itchat.send("好友:【%s(昵称:%s)】于:【%s】发来消息: 【%s】" % (friend['NickName'], friend['RemarkName'], time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()), msg['Text']),toUserName='filehelper')
    itchat.send(replyContent,toUserName=msg['FromUserName'])
    print("于【%s】收到好友【%s(昵称:%s)】发来的【%s】: 【%s】" % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()), friend['NickName'], friend['RemarkName'], msg['Type'], msg['Content']))
    print("于【%s】回复:%s" % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()), replyContent)+'\n')
itchat.auto_login(hotReload=True)
itchat.run()

程序运行效果如下:

image

文件传输助手:

image

电脑控制台:

image

首先,说一下程序怎么玩:

程序运行,电脑会弹出一个二维码,模拟登陆网页版微信的二维码,需要手机扫描后确认登陆。登陆后就开启了自动回复功能,好友发送的消息,都会自动回复你自己设定好的回复,另外,当发送的消息中包含关键词“快乐”时,回复内容包含一条祝福语和一个财神拜年的图片。

在自动回复好友的同时,向你个人的【文件传输助手】发送一条同样的消息,用来备份。

下面详细说一下,这几行代码做了什么事:

重写itchat库里text_reply()方法,msg['FromUserName']是加密过后的好友微信昵称,类似这样:'FromUserName': '@69bce2e06abac6829501f3ed9c25583d',我们要用itchat.search_friends()方法获得好友的信息表(frined)

friend = itchat.search_friends(userName=msg['FromUserName'])

然后才方便查找好友微信昵称friend['NickName']和好友备注名friend['RemarkName']。

replyContent = "收到您于%s发送的【%s】" % (time.strftime('%m-%d %H:%M',time.localtime()), msg['Type'])

replyContent是String类的自定义回复内容,msg['Type']是消息类型,例子中只用到了Text类型,还有语音、图片、文件、名片、等类型(PICTURE, MAP, CARD, NOTE, SHARING, RECORDING, ATTACHMENT, VIDEO)详见itchat官网。

if msg['Type'] == 'Text':
    ...

规定如果收到的消息类型是文本Text且文字内容里包含“快乐”,则用itchat.send()发送一张财神爷拜年的图片过去。

itchat.send('@img@%s' % '/Users/xxx/moneyGod.jpg',toUserName=msg['FromUserName'])

否则就按照replyContent设置的内容正常回复。toUserName=msg['FromUserName']表示谁给你发了消息,你就回复给谁!

itchat.send(replyContent,toUserName=msg['FromUserName'])

回复好友消息的同时,给自己的【文件传输助手】发一份消息来做备份。toUserName='filehelper'表示消息接收对象是“文件传输助手”

(itchat.send("好友:【%s(昵称:%s)】于:【%s】发来消息: 【%s】" % (friend['NickName'], friend['RemarkName'], time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()), msg['Text']),toUserName='filehelper'))

为了方便在电脑控制台查看消息,你可以在text_reply()方法中加上:

#可以加上print(msg)打印整条消息字典
print("于【%s】收到好友【%s(昵称:%s)】发来的【%s】: 【%s】" % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()), friend['NickName'], friend['RemarkName'], msg['Type'], msg['Content']))
print("于【%s】回复:%s" % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()), replyContent)+'\n')

控制台输出如下:

image

最后,通过如下命令登陆(hotReload=True),即使程序关闭,一定时间内重新开启也可以不用重新扫码。

itchat.auto_login(hotReload=True)

彩蛋:

下一篇文章:Python实现微信查天气+火车+飞机+快递!!!

对了,不只是集成了查询,还有简单实现了回复TDD/KTT(退订/开通自动回复)的功能,然后发过来的消息类型支持img、vedio、语音、文件下载到电脑备份。

相关文章

网友评论

    本文标题:Python—itchat实现微信自动回复

    本文链接:https://www.haomeiwen.com/subject/gblrlhtx.html