-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathwechat.py
More file actions
155 lines (128 loc) · 5.53 KB
/
wechat.py
File metadata and controls
155 lines (128 loc) · 5.53 KB
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
import requests
class WeChat:
def __init__(self):
self.BASE_URL = 'http://127.0.0.1:52700/wechat-plugin/'
# [GET] 最近聊天列表,可选参数: [keyword]
self.USER_URL = self.BASE_URL + 'user'
# [GET] 根据用户 id 查询指定数量聊天记录,可选参数:[userId, count]
self.CHAT_LOG_URL = self.BASE_URL + 'chatlog'
# [POST] 根据用户 id 发送消息,可选参数:[userId, content, srvId](Content-Type: application/x-www-form-urlencoded)
self.SEND_MESSAGE_URL = self.BASE_URL + 'send-message'
# [POST] 打开与指定好友的聊天窗口,参数:[userId](Content-Type: application/x-www-form-urlencoded)
self.OPEN_SESSION_URL = self.BASE_URL + 'open-session'
def _get_user_id_by_name(self, users, name):
"""根据名称获取 userId
Args:
users (list): 用户字典列表
name (str): 待匹配的名称
Return:
list: 待匹配名称对应 userId
"""
return [
user['userId']
for user in users
if name == user['title'].replace('[群聊]', '').split('(')[0]
]
def search_user_by_keyword(self, keyword):
"""根据关键词搜索用户
Args:
keyword (str): 关键词
Returns:
dict: 用户相关信息列表
"""
return requests.get(self.USER_URL, params={"keyword": keyword}).json()
def search_user_by_name(self, name, group=False):
"""根据名称获取 userId
Args:
name (str): 待匹配的名称
group (bool, optional): 是否多选,默认为 False,若 False 在匹配多个情况下默认返回最近的联系人
Returns:
list: userId 列表
"""
r = requests.get(self.USER_URL).json()
user_ids = self._get_user_id_by_name(r, name)
if not user_ids:
return []
if group:
return user_ids
else:
return [user_ids[0]]
def get_chat_log_by_name(self, name, count):
"""根据待匹配名称获取聊天记录
Args:
name (str): 用户 userId
count (int): 返回聊天记录条目数
Raises:
ValueError: 待匹配名称重复,例如有两个相同的人名
Returns:
dict: 聊天记录字典
"""
user_ids = self.search_user_by_name(name, group=True)
if len(user_ids) > 1:
raise ValueError("用户 ID 数量大于 1,请检查是否同名")
r = requests.get(self.CHAT_LOG_URL, params={
"userId": user_ids, "count": count})
if r:
return {i['subTitle']: i['copyText'] for i in r.json()[:1:-1]}
else:
return {}
def get_chat_log_by_id(self, user_id, count):
"""根据 userId 获取聊天记录
Args:
user_id (int): 用户 userId
count (int): 返回聊天记录条目数
Returns:
dict: 聊天记录字典
"""
r = requests.get(self.CHAT_LOG_URL, params={
"userId": user_id, "count": count})
if r:
return {i['subTitle']: i['copyText'] for i in r.json()[:1:-1]}
else:
return {}
def send_message_by_name(self, name, content, srvId=1, group=False):
"""通过待匹配名称发送消息
Args:
name (str): 待匹配名称
content (str): 待发送的消息
srvId (int, optional): Defaults to 1.
group (bool, optional): 是否群发,默认 False
Returns:
bool: 布尔值
"""
headers = {
"Content-Type": "application/x-www-form-urlencoded",
}
user_ids = self.search_user_by_name(name, group=group)
for user_id in user_ids:
r = requests.post(self.SEND_MESSAGE_URL, headers=headers, data={
"userId": user_id, "content": content, "srvId": srvId})
return True
def send_message_by_ids(self, user_ids, content, srvId=1):
"""通过 userId 发送消息
Args:
user_ids (list): 需要发送消息的 userId 列表
content (str): 待发送的消息
srvId (int, optional): [int]. Defaults to 1.
Returns:
bool: 布尔值
"""
headers = {
"Content-Type": "application/x-www-form-urlencoded",
}
for user_id in user_ids:
r = requests.post(self.SEND_MESSAGE_URL, headers=headers, data={
"userId": user_id, "content": content, "srvId": srvId})
return True
if __name__ == '__main__':
wechat = WeChat()
print(wechat.search_user_by_keyword('狗狗币')) # 搜索显示名称为狗狗币的联系人/群组(速度较慢)
print(wechat.search_user_by_name('狗狗币')) # 搜索显示名称为狗狗币的联系人/群组(速度较快)
print(wechat.get_chat_log_by_name('狗狗币', 5)) # 返回与狗狗币最近的五条聊天记录
# 返回与 userId 为 dogecoin 的最近的五条聊天记录
print(wechat.get_chat_log_by_id('DogeCoin', 5))
# 给显示名称为汪汪汪的联系人/群组发送消息
print(wechat.send_message_by_name('汪汪汪', '哟,这不狗狗币么,几天不见,这么拉了啊'))
# 给 userId 为 DogeCoin 和 ElonMusk 的联系人/群组发送消息
print(wechat.send_message_by_ids(
['DogeCoin', 'ElonMusk'], 'Everything to the moon!'))