update python_wechat.py
parent
eb7573ee26
commit
9156f11ec6
|
@ -4,9 +4,8 @@
|
|||
python_wechat.py by xianhu
|
||||
主要包括如下功能:
|
||||
(1) 自动提醒群红包
|
||||
(2) 自动提醒群中@自己或@all的内容
|
||||
(3) 自动提醒群中带有特殊关键字的消息
|
||||
(4) 自动保存被撤回消息,包括内容、文件、图片、语音、视频等
|
||||
(2) 自动提醒群中带有特殊关键字的消息
|
||||
(3) 自动保存被撤回消息,包括内容、文件、图片、语音、视频等
|
||||
"""
|
||||
|
||||
import re
|
||||
|
@ -18,15 +17,15 @@ from itchat.content import *
|
|||
|
||||
# 初始化
|
||||
my = itchat.new_instance()
|
||||
my.auto_login(hotReload=False, enableCmdQR=2)
|
||||
my.global_keys = ["人工智能", "机器学习", "算法", "数据挖掘"]
|
||||
my.auto_login(hotReload=False, enableCmdQR=-2)
|
||||
my.global_keys = ["创业", "齐现虎"]
|
||||
my.to_user_name = "filehelper"
|
||||
|
||||
# my还包括的以下属性,注意用点.查看:
|
||||
# (1) alive 是否还活着,isLogging 是否已登陆
|
||||
# (2) loginInfo 登陆信息,其中的User属性为自己的信息User字典类,包括UserName, NickName, RemarkName, Sex(1 or 2)等
|
||||
# (3) memberList 通讯录列表,每一项为一个User字典类
|
||||
# (4) chatroomList 群聊列表,每一项为一个Chatroom字典类,包括UserName, NickName, RemarkName, MemberCount等
|
||||
# (3) memberList 通讯录列表,每一项为一个User字典类,包括UserName, NickName, RemarkName, Sex(1 or 2)等
|
||||
# (4) chatroomList 群聊列表,每一项为一个Chatroom字典类,包括UserName, NickName, RemarkName, MemberCount, MemberList等
|
||||
# (5) mpList 订阅号列表,每一项为一个MassivePlatform字典类,包括UserName, NickName等
|
||||
|
||||
# 获取并更新通讯录: {UserName: UserInstance}
|
||||
|
@ -39,10 +38,23 @@ my.msg_store = {}
|
|||
# 消息提取函数
|
||||
def get_msg_list(msg):
|
||||
"""
|
||||
提取消息内容
|
||||
提取消息内容,消息来源分类:
|
||||
(1)来自好友的消息
|
||||
(2)来自自己的消息
|
||||
(3)来自文件传输助手的消息等
|
||||
(4)来自群聊的消息
|
||||
提取消息内容,消息类型分类:
|
||||
(1)文字
|
||||
(2)图片
|
||||
(3)语音
|
||||
(4)视频
|
||||
(5)地址
|
||||
(6)名片
|
||||
(7)Note
|
||||
(8)分享
|
||||
(9)附件
|
||||
"""
|
||||
logging.warning("%s", msg)
|
||||
|
||||
# logging.warning("%s", msg)
|
||||
msg_id = msg["MsgId"] # 消息ID
|
||||
from_user_name = msg["FromUserName"] # 消息发送者ID
|
||||
to_user_name = msg["ToUserName"] # 消息接受者ID
|
||||
|
@ -59,16 +71,15 @@ def get_msg_list(msg):
|
|||
)
|
||||
|
||||
if from_user_name.startswith("@@"):
|
||||
# 群消息
|
||||
nick_name = msg["ActualNickName"] if (msg["ActualUserName"] not in my.friends) or \
|
||||
(not my.friends[msg["ActualUserName"]]["RemarkName"]) else my.friends[msg["ActualUserName"]]["RemarkName"]
|
||||
else:
|
||||
# 个人消息
|
||||
nick_name = wind_name
|
||||
|
||||
we_type = msg["Type"] # 消息类型
|
||||
we_text = msg["Text"] # 消息内容
|
||||
|
||||
logging.warning("wind: %s, nick:%s, %s/%s, %s/%s, %s", wind_name, nick_name, msg_type, we_type, msg_content, we_text, msg_time)
|
||||
return msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, msg_file, msg_url, wind_name, nick_name, we_type, we_text
|
||||
|
||||
|
||||
|
@ -83,11 +94,12 @@ def text_reply(msg):
|
|||
|
||||
# 消息过滤, 只监测文字、注解、分享、图片、语音、视频、附件等
|
||||
if we_type not in ["Text", "Note", "Sharing", "Picture", "Recording", "Video", "Attachment"]:
|
||||
logging.warning("message ignored")
|
||||
logging.warning("message type isn't included, ignored")
|
||||
return
|
||||
|
||||
# 处理来自自己的消息
|
||||
if from_user_name == my.loginInfo["User"]["UserName"]:
|
||||
logging.warning("message is from myself, ignored")
|
||||
return
|
||||
|
||||
# 消息存储,删除过期消息
|
||||
|
@ -102,10 +114,7 @@ def text_reply(msg):
|
|||
if we_type == "Note" and we_text.find("收到红包,请在手机上查看") >= 0:
|
||||
my.send("【%s】中有红包,快抢!" % wind_name, toUserName=my.to_user_name)
|
||||
|
||||
# 提到自己消息处理
|
||||
if msg["IsAt"]:
|
||||
my.send("【%s】中有@你的消息:\nFrom: %s\nTime: %s\n%s" % (wind_name, nick_name, msg_time, msg_content), toUserName=my.to_user_name)
|
||||
|
||||
# 关键字消息处理
|
||||
for key in my.global_keys:
|
||||
if msg_content.find(key) >= 0:
|
||||
my.send("【%s】中有关键字【%s】:\nFrom: %s\nTime: %s\n%s" % (wind_name, key, nick_name, msg_time, msg_content), toUserName=my.to_user_name)
|
||||
|
@ -115,6 +124,7 @@ def text_reply(msg):
|
|||
if we_type == "Note" and we_text.find("撤回了一条消息") >= 0:
|
||||
old_msg = my.msg_store.get(msg_content[msg_content.find("<msgid>")+7: msg_content.find("</msgid>")])
|
||||
if not old_msg:
|
||||
logging.warning("not message id in my.msg_store")
|
||||
return
|
||||
|
||||
msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, msg_file, msg_url, wind_name, nick_name, we_type, we_text = get_msg_list(old_msg)
|
||||
|
|
Loading…
Reference in New Issue