import itchat
import time
import datetime
from itchat.content import *
itchat.auto_login(True)
#设置关键群
#u'测试1' 为需要接收信息群
#u'测试2' 为需要转发信息群
#u'金叶' 为需要转发此人信息
a = itchat.search_chatrooms(name=u'测试1')[0]['UserName']
b = itchat.search_chatrooms(name=u'测试2')[0]['UserName']
c = itchat.search_chatrooms(name=u'测试3')[0]['UserName']
e = itchat.search_friends(name=u'金叶')[0]['UserName']
#打印登陆者微信名字
print(itchat.search_friends()['NickName'])
#微信收到消息触发本事件
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
#先判断消息来源是否来自需要转发的群(无特定来源可删除此if)
if msg['FromUserName'] == b:
#判断消息来源是否来自需要转发的人(无特定来源可删除此if)
if msg['ActualUserName'] == e:
#转发
itchat.send(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) + msg['ActualNickName'] +':'+ msg['Content'], c)
itchat.run()
练习时只需要下载itchat模块就可以了 , 测试的时候需要将群保存至测试号的通讯簿里才可以使用 , 要不然会出错 , 想自己添加验证信息也可以。