python发信息给微信群_利用Python转发微信群通知信息

案例:作为班长需要实时转发年级群发布的有关通知、文件到自己班的微信群,这种无脑的重复性工作适用于编写脚本自动帮忙处理。
准备:服务器(可选择阿里云学生服务器)、itchat模块、一颗爱折腾的心
代码:
#coding=utf-8
import itchat
from itchat.content import *
@itchat.msg_register(TEXT, isGroupChat=True)
def group_text(msg):
group = itchat.get_chatrooms(update=True)
from_user = ''
for g in group:
if g['NickName'] == '材硕18级班长支书们':#从群中找到指定的群聊
from_group = g['UserName']
for menb in g['MemberList']:
#print(menb['NickName'])
if menb['NickName'] == "这是发布通知的大佬名字":
from_user = menb['UserName']
break
if g['NickName'] == '材硕1801':#把消息发到这个群
to_group = g['UserName']
if msg['FromUserName'] == from_group:
if msg['ActualUserName'] == from_user:
itchat.send('[大佬有话要说] %s:%s'%(msg['ActualNickName'],msg['Content']),to_group)
@itchat.msg_register([PICTURE, ATTACHMENT, VIDEO], isGroupChat=True)
def group_reply_media(msg):
group = itchat.get_chatrooms(update=True)
from_user = ''
for g in group:
if g['NickName'] == '材硕18级班长支书们':#从群中找到指定的群聊
from_group = g['UserName']
for menb in g['MemberList']:
#print(menb['NickName'])
if menb['NickName'] == "这是发布通知的大佬名字":
from_user = menb['UserName']
break
if g['NickName'] == '材硕1801':#把消息发到这个群
to_group = g['UserName']
msg['Text'](msg['FileName'])
if msg['FromUserName'] == from_group:
if msg['ActualUserName'] == from_user:
msg['Text'](msg['FileName'])
itchat.send('@%s@%s'%({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'),msg['FileName']),to_group)
itchat.auto_login(hotReload=False,enableCmdQR=2)
itchat.run()
上传服务器后通过screen命令进行设置后台运行即可(如下操作)
screen -S cs1 #创建一个screen
python3 cs.py
【python发信息给微信群_利用Python转发微信群通知信息】Ctrl+A+D跳出screen让其后台运行

    推荐阅读