工作日常之微信助手itchat

发布时间:2024-12-13 14:31

使用企业微信的智能助手处理日常任务 #生活技巧# #职场生存技巧# #职场沟通软件#

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

    我们找到了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、测试,小草在大群中发送文件,在小群中查看,发现“我”转发了小草在大群中发送的文件。

网址:工作日常之微信助手itchat https://www.yuejiaxmz.com/news/view/464612

相关内容

python3.7之itchat 或bot 登录微信时报错:loginCallback=loginCallback, exitCallback=exitCallback)
用python 玩微信
Python微信机器人之Python
Python骚操作
微信实用功能:从生活助手到工作效率神器
微信语音助手下载(暂无资源) 微信语音助手v3.1.0下载
微信语音助手下载手机版
微信语音助手APP
从零开始构建:Python自定义脚本自动化你的日常任务
微信生活助手

随便看看