-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmessage.py
222 lines (199 loc) · 6.69 KB
/
message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import enum
import json
import re
from functools import cached_property
from typing import Optional, Tuple
from loguru import logger
from pydantic import BaseModel, computed_field
from wechatter.bot import BotInfo
from wechatter.models.wechat.group import Group
from wechatter.models.wechat.person import Person
from wechatter.models.wechat.quoted_response import QUOTABLE_FORMAT
PERSON_FORWARDING_MESSAGE_FORMAT = "⤴️ [%s] 说:\n" "-------------------------\n"
GROUP_FORWARDING_MESSAGE_FORMAT = "⤴️ [%s] 在 [%s] 说:\n" "-------------------------\n"
class MessageType(enum.Enum):
"""
消息类型枚举类
"""
text = "text"
file = "file"
urlLink = "urlLink"
friendship = "friendship"
class MessageSenderType(enum.Enum):
"""
消息来源枚举
"""
PERSON = 0
GROUP = 1
# TODO: 公众号文章
# ARTICLE = 2
class Message(BaseModel):
"""
微信消息类(消息接收)
"""
type: MessageType
person: Person
group: Optional[Group] = None
content: str
is_mentioned: bool = False
id: Optional[int] = None
@classmethod
def from_api_msg(
cls,
type: MessageType,
content: str,
source: str,
is_mentioned: str,
):
"""
从API接口创建消息对象
:param type: 消息类型
:param content: 消息内容
:param source: 消息来源
:param is_mentioned: 是否@机器人
:return: 消息对象
"""
try:
source_json = json.loads(source)
except json.JSONDecodeError as e:
logger.error("消息来源解析失败")
raise e
# from为发送者信息,无论是个人消息还是群消息,都有from
payload = source_json.get("from").get("payload", {})
gender = int(payload.get("gender", -1))
g = "unknown"
if gender == 1:
g = "male"
elif gender == 0:
g = "female"
_person = Person(
id=payload.get("id", ""),
name=payload.get("name", ""),
alias=payload.get("alias", ""),
gender=g,
signature=payload.get("signature", ""),
province=payload.get("province", ""),
city=payload.get("city", ""),
# phone_list=payload.get("phone", []),
is_star=payload.get("star", ""),
is_friend=payload.get("friend", ""),
)
_group = None
# room为群信息,只有群消息才有room
if source_json["room"] != "":
g_data = source_json["room"]
payload = g_data.get("payload", {})
_group = Group(
id=g_data.get("id", ""),
name=payload.get("topic", ""),
admin_id_list=payload.get("adminIdList", []),
member_list=payload.get("memberList", []),
)
_content = content.replace("\u2005", " ", 1)
_is_mentioned = False
if is_mentioned == "1":
_is_mentioned = True
return cls(
type=type,
person=_person,
group=_group,
content=_content,
is_mentioned=is_mentioned,
)
@computed_field
@property
def is_group(self) -> bool:
"""
是否是群消息
:return: 是否是群消息
"""
return self.group is not None
@computed_field
@cached_property
def is_quoted(self) -> bool:
"""
是否引用机器人消息
:return: 是否引用机器人消息
"""
# 引用消息的正则
quote_pattern = r"(?s)「(.*?)」\n- - - - - - - - - - - - - - -"
match_result = re.match(quote_pattern, self.content)
# 判断是否为引用机器人消息
if match_result and self.content.startswith(f"「{BotInfo.name}"):
return True
return False
# TODO: 判断所有的引用消息,不仅仅是机器人消息
# 待解决:在群中如果有人设置了自己的群中名称,那么引用内容的名字会变化,导致无法匹配到用户
@computed_field
@property
def sender_name(self) -> str:
"""
返回消息发送对象名,如果是群则返回群名,如果不是则返回人名
:return: 消息发送对象名
"""
return self.group.name if self.is_group else self.person.name
@computed_field
@cached_property
def quotable_id(self) -> Optional[str]:
"""
获取引用消息的id
:return: 引用消息的id
"""
if self.is_quoted:
pattern = f'^「[^「]+{QUOTABLE_FORMAT % "(.{3})"}'
try:
return re.search(pattern, self.content).group(1)
except AttributeError:
return None
return None
@computed_field
@cached_property
def pure_content(self) -> str:
"""
获取不带引用的消息内容,即用户真实发送的消息
:return: 不带引用的消息内容
"""
if self.is_quoted:
pattern = "「[\s\S]+」\n- - - - - - - - - - - - - - -\n([\s\S]*)"
return re.search(pattern, self.content).group(1)
else:
return self.content
@computed_field
@cached_property
def forwarded_source(self) -> Optional[Tuple[str, bool]]:
"""
获取转发消息的来源的名字
:return: 消息来源的名字和是否为群的元组(source, is_group)
"""
if self.is_quoted:
# 先尝试匹配群消息
group_format = GROUP_FORWARDING_MESSAGE_FORMAT.replace("[", "\[").replace(
"]", "\]"
)
pattern = re.compile(f'{group_format % ("(.*)", "(.+)")}')
try:
# 将名字和该名字是否为群都返回,便于在回复时判断
return re.search(pattern, self.content).group(2), True
except AttributeError:
pass
# 再尝试匹配个人消息
person_format = PERSON_FORWARDING_MESSAGE_FORMAT.replace("[", "\[").replace(
"]", "\]"
)
pattern = re.compile(f'{person_format % "(.+)"}')
try:
return re.search(pattern, self.content).group(1), False
except AttributeError:
return None
else:
return None
def __str__(self) -> str:
source = self.person
if self.is_group:
source = self.group
return (
f"消息内容:{self.content}\n"
f"消息来源:{source}\n"
f"是否@:{self.is_mentioned}\n"
f"是否引用:{self.is_quoted}"
)