itchat 群聊转发,自动回复

来源:互联网 发布:nginx的日志文件在哪 编辑:程序博客网 时间:2024/06/14 21:31
  1. itchat自动把好友发来的消息,回复给他
    仅能实现自动回复 原文给 好友发来的文本消息、图片表情消息。
#!/usr/bin/python#coding=utf-8import itchatfrom itchat.content import *@itchat.msg_register([PICTURE,TEXT])def simple_reply(msg):    if msg['Type'] == TEXT:        ReplyContent = 'I received message: '+msg['Content']    if msg['Type'] == PICTURE:        ReplyContent = 'I received picture: '+msg['FileName']    itchat.send_msg(ReplyContent,msg['FromUserName'])itchat.auto_login()itchat.run()

这里注册了两个消息类型,文本和图片(表情),当微信接收到这两个消息时就会进入注册的函数simple_reply,msg是一个字典类型里面包含了消息数据包,有发送者、接收者、消息类型、消息内容等超多的信息
itchat要注册消息类型,比如注册了TEXT(itchat.content.text),就会接收文本消息,其他消息不会触发函数。消息类型见库中的content.py文件
消息类型判断,msg[‘Type’]
消息发起者,msg[‘FromUserName’]
消息接收者,msg[‘ToUserName’]
文本消息,msg[‘Content’]
文件名字,msg[‘FileName’],注:如果是自带的表情就会显示表情

2.自动转发指定的群聊消息给指定的好友。
应用场景:每天会在微信群内收集订餐的小伙伴名单,订餐的回复+1,
由于时间跨度,群消息太多,手工上下翻 +1 的消息难免遗漏,所以
这段脚本正好满足此需求。
转发的内容是:群内昵称:+1

#!/usr/bin/python#coding=UTF-8import itchatfrom itchat.content import *@itchat.msg_register([PICTURE,TEXT],isGroupChat=True)def simple_reply(msg):    users = itchat.search_friends(name=u'测试23')#通讯录中好友备注名    userName = users[0]['UserName']    if msg['Content'] == "+1":        itchat.send(u'%s\u2005: %s '%(msg['ActualNickName'],msg['Content']),toUserName=userName)itchat.auto_login()#enableCmdQR=True 可以在命令行显示二维码itchat.run()