python 群和群之间的自动转发功能
废话不多说 直接上代码
一个可以登录微信网页版的微信号 当做转发的机器人
技术:Python 安装 itchat pip install itchat
启动 控制台输入 python 文件名.py
import itchat
from itchat.content import TEXT
from itchat.content import *
itchat.auto_login(hotReload=True)
获取到想要的群名字 total
mpsList= itchat.get_chatrooms()
total=0
for it in mpsList:# mpsList 是账户的群列表
if it[‘NickName’] != ‘’:
if it[‘NickName’][0]== ‘安’:# 判断群第一个字是不是自己想要的
total=it[‘NickName’]
print(total) #打印是不是你想要的群名字
执行转发函数
@itchat.msg_register( [PICTURE, RECORDING, ATTACHMENT, VIDEO,TEXT], isGroupChat=True)
def group_text(msg):
group = itchat.get_chatrooms(update=True)
for g in group:
if g[‘NickName’] == total:#消息获取内容群名字
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:
if msg[‘Type’] == TEXT: #文字
itchat.send(’%s’ % msg[‘Content’],to_group)
elif msg[‘Type’] != TEXT: #不是文字
msg’Text’
itchat.send("@img@%s" % msg[“FileName”],to_group)
itchat.auto_login(hotReload=False)
itchat.run()
到这里基本就完事了 这个说一下 这个转发功能 只能转发 文字 和照片 基本的已经完成了 但是如果您 想要更多功能 比如 视频 ,聊天信息转发 。
都可以在 if msg['ActualUserName'] 里面的if 判断里面加 我现在的判断就是 不是图片就是文字。
重点 : 1,一定一定要是可以登录微信网页的账户,
2020_07_07 python3 最新代码
# #coding=utf-8
import itchat
from itchat.content import TEXT
from itchat.content import *
itchat.auto_login(hotReload=True)
# mpsList= itchat.get_chatrooms()
# totafrom_userl=0
# for it in mpsList:# mpsList 是账户的群列表
# if it['NickName'] != '':
# if it['NickName'][0] == '速':
# total=it['NickName']
# print('total',total) #打印是不是你想要的群名字
@itchat.msg_register( [PICTURE, RECORDING, ATTACHMENT, VIDEO,TEXT], isGroupChat=True)
def group_text(msg):
global from_user
msg.download("D:\wetchat\img\\"+msg['FileName'])
group = itchat.get_chatrooms(update=True)
for g in group:
if g['NickName'] == '测试a群':#消息获取内容群名字
from_group = g['UserName']
for menb in g['MemberList']:
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:
if msg['Type'] == TEXT:
itchat.send('%s' % msg['Content'],to_group)
elif msg['Type'] == PICTURE:
itchat.send_image("D:\wetchat\img\\"+msg['FileName'],to_group)
elif msg['Type'] == VIDEO:
itchat.send_video("D:\wetchat\img\\"+msg['FileName'],to_group)
elif msg['Type'] == ATTACHMENT:
itchat.send_file("D:\wetchat\img\\"+msg['FileName'],to_group)
elif msg['Type'] == RECORDING:
itchat.send_file("D:\wetchat\img\\"+msg['FileName'],to_group)
itchat.auto_login(hotReload=False)
itchat.run()