工作日常之微信助手itchat

    微信作为用户最多最广泛的聊天工具,其使用场景不必多说。因其自身客户端存在诸多限制(如图片视频发送每次不超过九个)以及没有我们想要的一些功能,于是我们想到了自己做一个微信的客户端,利用第三方接口。

    我们找到了itchat,itchat提供了与微信交互的python接口(itchat也存在nodeJS、java等的版本,但是使用人数少,实践少,不推荐使用)。

    itchat的官方文档:http://itchat.readthedocs.io/zh/latest/tutorial/tutorial0/

    itchat使用方式十分简单,以登录、设置群文件自动转发为例:

        需要:两个微信账号,两个微信账号都在的同一个“大群”,以及一个用于接受文件的“小群”。

    1、在有python环境的情况下,首先安装itchat模块。

        pip install itchat

    2、编写python脚本,内容十分简单。

        import   3、运行python脚本。

    
<spancourier new',="" monospace;="" font-size:="" 13px;="" font-style:="" normal;="" font-variant:="" letter-spacing:="" line-height:="" 18.5714px;="" orphans:="" auto;="" text-align:="" start;="" text-indent:="" 0px;="" text-transform:="" none;="" white-space:="" pre-wrap;="" widows:="" 1;="" word-spacing:="" -webkit-text-stroke-width:="" background-color:="" rgb(245,="" 245,="" 245);"="" style="padding: 0px; margin: 0px;">   4、编写群文件自动转发内容。其中“大群”为被转发群的群名称,“小草”是被转发群中被转发人的名称,“小群”则是要转发到的群的名称。
#coding=utf-8
import itchat
from itchat.content import TEXT
from itchat.content import PICTURE
from itchat.content import *
@itchat.msg_register(PICTURE, isGroupChat=True)#图片
def group_image(msg):
group = itchat.get_chatrooms(update=True)
from_user = ''
for g in group:
if g['NickName'] == '大群':#从群中找到指定的群聊
print(g['NickName'])
from_group = g['UserName']
for menb in g['MemberList']:
print(menb['NickName'])
if menb['NickName'] == "小草":#从群成员列表找到用户,只转发他的消息
from_user = menb['UserName']
break
if g['NickName'] == '小群':#把消息发到这个群
to_group = g['UserName']
if msg['FromUserName'] == from_group:
if msg['ActualUserName'] == from_user:
#itchat.send(msg['Content'],to_group)
msg['Text']("E:\\my_create_resource\\wechat-itchat\\mytest\\"+msg['FileName']) #保存图片到对应的路径
itchat.send_image("E:\\my_create_resource\\wechat-itchat\\mytest\\"+msg['FileName'],to_group) #发送图片


@itchat.msg_register(VIDEO, isGroupChat=True)#视频
def group_video(msg):
group = itchat.get_chatrooms(update=True)
from_user = ''
for g in group:
if g['NickName'] == '大群':#从群中找到指定的群聊
print(g['NickName'])
from_group = g['UserName']
for menb in g['MemberList']:
print(menb['NickName'])
if menb['NickName'] == "小草":#从群成员列表找到用户,只转发他的消息
from_user = menb['UserName']
break
if g['NickName'] == '小群':#把消息发到这个群
to_group = g['UserName']
if msg['FromUserName'] == from_group:
if msg['ActualUserName'] == from_user:
#itchat.send(msg['Content'],to_group)
msg['Text']("E:\\my_create_resource\\wechat-itchat\\mytest\\video\\"+msg['FileName']) #保存视频到对应的路径
itchat.send_video("E:\\my_create_resource\\wechat-itchat\\mytest\\video\\"+msg['FileName'],to_group) #发送视频

@itchat.msg_register(ATTACHMENT, isGroupChat=True)#附件
def group_attachment(msg):
group = itchat.get_chatrooms(update=True)
from_user = ''
for g in group:
if g['NickName'] == '大群':#从群中找到指定的群聊
print(g['NickName'])
from_group = g['UserName']
for menb in g['MemberList']:
print(menb['NickName'])
if menb['NickName'] == "小草":#从群成员列表找到用户,只转发他的消息
from_user = menb['UserName']
break
if g['NickName'] == '小群':#把消息发到这个群
to_group = g['UserName']
if msg['FromUserName'] == from_group:
if msg['ActualUserName'] == from_user:
#itchat.send(msg['Content'],to_group)
msg['Text']("E:\\my_create_resource\\wechat-itchat\\mytest\\file\\"+msg['FileName']) #保存附件到对应的路径
itchat.send_file("E:\\my_create_resource\\wechat-itchat\\mytest\\file\\"+msg['FileName'],to_group) #发送附件

itchat.auto_login(hotReload=False)
itchat.run()

    5、运行脚本。

        python testSendFile.py

    6、测试,小草在大群中发送文件,在小群中查看,发现“我”转发了小草在大群中发送的文件。

    

猜你喜欢

转载自blog.csdn.net/qq_35946969/article/details/80968398