magic-campus-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- magic_campus_cli-0.1.0.dist-info/METADATA +51 -0
- magic_campus_cli-0.1.0.dist-info/RECORD +53 -0
- magic_campus_cli-0.1.0.dist-info/WHEEL +5 -0
- magic_campus_cli-0.1.0.dist-info/entry_points.txt +2 -0
- magic_campus_cli-0.1.0.dist-info/top_level.txt +2 -0
- magiccampus_cli/__init__.py +1 -0
- magiccampus_cli/__main__.py +9 -0
- magiccampus_cli/accounts.py +144 -0
- magiccampus_cli/cli.py +176 -0
- magiccampus_cli/client.py +54 -0
- magiccampus_cli/commands/auth.py +32 -0
- magiccampus_cli/commands/comment.py +38 -0
- magiccampus_cli/commands/config.py +95 -0
- magiccampus_cli/commands/feed.py +26 -0
- magiccampus_cli/commands/group.py +346 -0
- magiccampus_cli/commands/im.py +47 -0
- magiccampus_cli/commands/notify.py +81 -0
- magiccampus_cli/commands/playgroup.py +49 -0
- magiccampus_cli/commands/reaction.py +50 -0
- magiccampus_cli/commands/space.py +134 -0
- magiccampus_cli/commands/user.py +311 -0
- magiccampus_cli/config_store.py +61 -0
- magiccampus_cli/notify.py +493 -0
- magiccampus_cli/output.py +104 -0
- magiccampus_cli/prompts.py +58 -0
- magiccampus_cli/targets.py +26 -0
- magiccampus_cli/update.py +149 -0
- magiccampus_cli/utils.py +67 -0
- magiccampus_sdk/__init__.py +43 -0
- magiccampus_sdk/auth.py +111 -0
- magiccampus_sdk/comment_client.py +107 -0
- magiccampus_sdk/feed_api.py +61 -0
- magiccampus_sdk/feed_client.py +30 -0
- magiccampus_sdk/group_api.py +139 -0
- magiccampus_sdk/group_client.py +242 -0
- magiccampus_sdk/http/__init__.py +1 -0
- magiccampus_sdk/http/group_info_api.py +47 -0
- magiccampus_sdk/http/http_api.py +58 -0
- magiccampus_sdk/http/http_send.py +758 -0
- magiccampus_sdk/http/media.py +839 -0
- magiccampus_sdk/http/models.py +645 -0
- magiccampus_sdk/http/self_user_info_api.py +182 -0
- magiccampus_sdk/http/text.py +75 -0
- magiccampus_sdk/models.py +279 -0
- magiccampus_sdk/platform_client.py +137 -0
- magiccampus_sdk/platform_http.py +71 -0
- magiccampus_sdk/playgroup_api.py +104 -0
- magiccampus_sdk/playgroup_client.py +45 -0
- magiccampus_sdk/reaction_api.py +110 -0
- magiccampus_sdk/space_api.py +170 -0
- magiccampus_sdk/space_client.py +93 -0
- magiccampus_sdk/user_client.py +211 -0
- magiccampus_sdk/user_info_api.py +593 -0
|
@@ -0,0 +1,758 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import logging
|
|
5
|
+
import os
|
|
6
|
+
import time
|
|
7
|
+
import uuid
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from typing import Any, Dict, Optional, Sequence
|
|
10
|
+
|
|
11
|
+
import httpx
|
|
12
|
+
|
|
13
|
+
from magiccampus_sdk.auth import MagicCampusCredentials, fetch_magiccampus_credentials, resolve_platform_id
|
|
14
|
+
from magiccampus_sdk.http.media import (
|
|
15
|
+
auto_video_snapshot,
|
|
16
|
+
build_at_text_content,
|
|
17
|
+
build_custom_content,
|
|
18
|
+
build_file_content_by_url,
|
|
19
|
+
build_file_content_for_upload,
|
|
20
|
+
build_markdown_ex,
|
|
21
|
+
build_picture_content_by_url,
|
|
22
|
+
build_picture_content_for_upload,
|
|
23
|
+
build_quote_content,
|
|
24
|
+
build_text_content,
|
|
25
|
+
build_video_content_by_url,
|
|
26
|
+
build_video_content_for_upload,
|
|
27
|
+
detect_content_type,
|
|
28
|
+
read_image_metadata,
|
|
29
|
+
upload_file,
|
|
30
|
+
)
|
|
31
|
+
from magiccampus_sdk.http.models import SelfUserInfo
|
|
32
|
+
from magiccampus_sdk.http.self_user_info_api import SelfUserInfoResolver, fetch_self_user_info
|
|
33
|
+
|
|
34
|
+
CONTENT_TEXT = 101
|
|
35
|
+
CONTENT_PICTURE = 102
|
|
36
|
+
CONTENT_VIDEO = 104
|
|
37
|
+
CONTENT_FILE = 105
|
|
38
|
+
CONTENT_AT_TEXT = 106
|
|
39
|
+
CONTENT_CUSTOM = 110
|
|
40
|
+
CONTENT_QUOTE = 114
|
|
41
|
+
SESSION_SINGLE_CHAT = 1
|
|
42
|
+
SESSION_GROUP_CHAT = 3
|
|
43
|
+
|
|
44
|
+
logger = logging.getLogger(__name__)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class MagicCampusHTTPSendHTTPError(Exception):
|
|
48
|
+
def __init__(self, status_code: int, body: str) -> None:
|
|
49
|
+
super().__init__(f"http status={status_code}, body={body}")
|
|
50
|
+
self.status_code = status_code
|
|
51
|
+
self.body = body
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class MagicCampusHTTPSendAPIError(Exception):
|
|
55
|
+
def __init__(self, code: int, message: str, detail: str = "") -> None:
|
|
56
|
+
super().__init__(f"[{code}] {message} {detail}".strip())
|
|
57
|
+
self.code = int(code)
|
|
58
|
+
self.message = message
|
|
59
|
+
self.detail = detail
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass(frozen=True)
|
|
63
|
+
class MagicCampusHTTPSendConfig:
|
|
64
|
+
apiurl: str
|
|
65
|
+
apikey: str
|
|
66
|
+
platform: str = "IOS"
|
|
67
|
+
credential_timeout_seconds: float = 15.0
|
|
68
|
+
request_timeout_seconds: float = 30.0
|
|
69
|
+
self_user_info_cache_ttl_seconds: float = 600.0
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass(frozen=True)
|
|
73
|
+
class SendAck:
|
|
74
|
+
client_msg_id: str
|
|
75
|
+
server_msg_id: str
|
|
76
|
+
send_time: int
|
|
77
|
+
|
|
78
|
+
def to_dict(self) -> dict[str, object]:
|
|
79
|
+
return {
|
|
80
|
+
"clientMsgID": self.client_msg_id,
|
|
81
|
+
"serverMsgID": self.server_msg_id,
|
|
82
|
+
"sendTime": self.send_time,
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class MagicCampusHTTPSendClient:
|
|
87
|
+
transport_kind = "http_api"
|
|
88
|
+
|
|
89
|
+
def __init__(self, config: MagicCampusHTTPSendConfig) -> None:
|
|
90
|
+
self._config = config
|
|
91
|
+
self._credentials: MagicCampusCredentials | None = None
|
|
92
|
+
self._platform_id = resolve_platform_id(config.platform)
|
|
93
|
+
self._self_user_info_resolver = SelfUserInfoResolver(
|
|
94
|
+
lambda: self._fetch_self_user_info(),
|
|
95
|
+
cache_ttl_seconds=config.self_user_info_cache_ttl_seconds,
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def credentials(self) -> MagicCampusCredentials | None:
|
|
100
|
+
return self._credentials
|
|
101
|
+
|
|
102
|
+
@property
|
|
103
|
+
def user_id(self) -> str:
|
|
104
|
+
return self._credentials.user_id if self._credentials is not None else ""
|
|
105
|
+
|
|
106
|
+
@property
|
|
107
|
+
def _api_addr(self) -> str:
|
|
108
|
+
return self._require_credentials().api_addr
|
|
109
|
+
|
|
110
|
+
def login(self) -> MagicCampusCredentials:
|
|
111
|
+
credentials = fetch_magiccampus_credentials(
|
|
112
|
+
apiurl=self._config.apiurl,
|
|
113
|
+
apikey=self._config.apikey,
|
|
114
|
+
platform=self._config.platform,
|
|
115
|
+
timeout_seconds=self._config.credential_timeout_seconds,
|
|
116
|
+
)
|
|
117
|
+
self._credentials = credentials
|
|
118
|
+
self._self_user_info_resolver.clear()
|
|
119
|
+
return credentials
|
|
120
|
+
|
|
121
|
+
def logout(self) -> None:
|
|
122
|
+
self._credentials = None
|
|
123
|
+
self._self_user_info_resolver.clear()
|
|
124
|
+
|
|
125
|
+
def send_text(
|
|
126
|
+
self,
|
|
127
|
+
text: str,
|
|
128
|
+
*,
|
|
129
|
+
recv_id: str = "",
|
|
130
|
+
group_id: str = "",
|
|
131
|
+
sender_platform_id: int = 0,
|
|
132
|
+
sender_nickname: str = "",
|
|
133
|
+
sender_face_url: str = "",
|
|
134
|
+
session_type: Optional[int] = None,
|
|
135
|
+
options: Optional[Dict[str, bool]] = None,
|
|
136
|
+
offline_push_info: Optional[Dict[str, Any]] = None,
|
|
137
|
+
ex: str = "",
|
|
138
|
+
) -> Dict[str, Any]:
|
|
139
|
+
del options, offline_push_info
|
|
140
|
+
return self._send_content(
|
|
141
|
+
content_type=CONTENT_TEXT,
|
|
142
|
+
content_payload=build_text_content(text),
|
|
143
|
+
recv_id=recv_id,
|
|
144
|
+
group_id=group_id,
|
|
145
|
+
sender_platform_id=sender_platform_id,
|
|
146
|
+
sender_nickname=sender_nickname,
|
|
147
|
+
sender_face_url=sender_face_url,
|
|
148
|
+
session_type=session_type,
|
|
149
|
+
ex=ex,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
def send_at_text(
|
|
153
|
+
self,
|
|
154
|
+
text: str,
|
|
155
|
+
*,
|
|
156
|
+
at_user_list: Sequence[str],
|
|
157
|
+
at_users_info: Optional[Sequence[Dict[str, str]]] = None,
|
|
158
|
+
quote_message: Optional[Dict[str, Any]] = None,
|
|
159
|
+
recv_id: str = "",
|
|
160
|
+
group_id: str = "",
|
|
161
|
+
sender_platform_id: int = 0,
|
|
162
|
+
sender_nickname: str = "",
|
|
163
|
+
sender_face_url: str = "",
|
|
164
|
+
session_type: Optional[int] = None,
|
|
165
|
+
options: Optional[Dict[str, bool]] = None,
|
|
166
|
+
offline_push_info: Optional[Dict[str, Any]] = None,
|
|
167
|
+
ex: str = "",
|
|
168
|
+
) -> Dict[str, Any]:
|
|
169
|
+
del options, offline_push_info
|
|
170
|
+
return self._send_content(
|
|
171
|
+
content_type=CONTENT_AT_TEXT,
|
|
172
|
+
content_payload=build_at_text_content(
|
|
173
|
+
text=text,
|
|
174
|
+
at_user_list=at_user_list,
|
|
175
|
+
at_users_info=at_users_info,
|
|
176
|
+
quote_message=quote_message,
|
|
177
|
+
),
|
|
178
|
+
at_user_id_list=at_user_list,
|
|
179
|
+
recv_id=recv_id,
|
|
180
|
+
group_id=group_id,
|
|
181
|
+
sender_platform_id=sender_platform_id,
|
|
182
|
+
sender_nickname=sender_nickname,
|
|
183
|
+
sender_face_url=sender_face_url,
|
|
184
|
+
session_type=session_type,
|
|
185
|
+
ex=ex,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
def send_markdown(
|
|
189
|
+
self,
|
|
190
|
+
markdown: str,
|
|
191
|
+
*,
|
|
192
|
+
recv_id: str = "",
|
|
193
|
+
group_id: str = "",
|
|
194
|
+
sender_platform_id: int = 0,
|
|
195
|
+
sender_nickname: str = "",
|
|
196
|
+
sender_face_url: str = "",
|
|
197
|
+
session_type: Optional[int] = None,
|
|
198
|
+
options: Optional[Dict[str, bool]] = None,
|
|
199
|
+
offline_push_info: Optional[Dict[str, Any]] = None,
|
|
200
|
+
ex: str = "",
|
|
201
|
+
) -> Dict[str, Any]:
|
|
202
|
+
if not markdown:
|
|
203
|
+
raise ValueError("markdown is required")
|
|
204
|
+
return self.send_text(
|
|
205
|
+
markdown,
|
|
206
|
+
recv_id=recv_id,
|
|
207
|
+
group_id=group_id,
|
|
208
|
+
sender_platform_id=sender_platform_id,
|
|
209
|
+
sender_nickname=sender_nickname,
|
|
210
|
+
sender_face_url=sender_face_url,
|
|
211
|
+
session_type=session_type,
|
|
212
|
+
options=options,
|
|
213
|
+
offline_push_info=offline_push_info,
|
|
214
|
+
ex=build_markdown_ex(ex),
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
def send_quote_text(
|
|
218
|
+
self,
|
|
219
|
+
text: str,
|
|
220
|
+
*,
|
|
221
|
+
quote_message: Dict[str, Any],
|
|
222
|
+
message_entity_list: Optional[Sequence[Dict[str, Any]]] = None,
|
|
223
|
+
recv_id: str = "",
|
|
224
|
+
group_id: str = "",
|
|
225
|
+
sender_platform_id: int = 0,
|
|
226
|
+
sender_nickname: str = "",
|
|
227
|
+
sender_face_url: str = "",
|
|
228
|
+
session_type: Optional[int] = None,
|
|
229
|
+
options: Optional[Dict[str, bool]] = None,
|
|
230
|
+
offline_push_info: Optional[Dict[str, Any]] = None,
|
|
231
|
+
ex: str = "",
|
|
232
|
+
) -> Dict[str, Any]:
|
|
233
|
+
del options, offline_push_info
|
|
234
|
+
if not text:
|
|
235
|
+
raise ValueError("text is required")
|
|
236
|
+
if not quote_message:
|
|
237
|
+
raise ValueError("quote_message is required")
|
|
238
|
+
return self._send_content(
|
|
239
|
+
content_type=CONTENT_QUOTE,
|
|
240
|
+
content_payload=build_quote_content(
|
|
241
|
+
text=text,
|
|
242
|
+
quote_message=quote_message,
|
|
243
|
+
message_entity_list=message_entity_list,
|
|
244
|
+
),
|
|
245
|
+
recv_id=recv_id,
|
|
246
|
+
group_id=group_id,
|
|
247
|
+
sender_platform_id=sender_platform_id,
|
|
248
|
+
sender_nickname=sender_nickname,
|
|
249
|
+
sender_face_url=sender_face_url,
|
|
250
|
+
session_type=session_type,
|
|
251
|
+
ex=ex,
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
def send_custom(
|
|
255
|
+
self,
|
|
256
|
+
custom: Any,
|
|
257
|
+
*,
|
|
258
|
+
extension: str = "",
|
|
259
|
+
description: str = "",
|
|
260
|
+
recv_id: str = "",
|
|
261
|
+
group_id: str = "",
|
|
262
|
+
sender_platform_id: int = 0,
|
|
263
|
+
sender_nickname: str = "",
|
|
264
|
+
sender_face_url: str = "",
|
|
265
|
+
session_type: Optional[int] = None,
|
|
266
|
+
ex: str = "",
|
|
267
|
+
) -> Dict[str, Any]:
|
|
268
|
+
return self._send_content(
|
|
269
|
+
content_type=CONTENT_CUSTOM,
|
|
270
|
+
content_payload=build_custom_content(
|
|
271
|
+
custom,
|
|
272
|
+
extension=extension,
|
|
273
|
+
description=description,
|
|
274
|
+
),
|
|
275
|
+
recv_id=recv_id,
|
|
276
|
+
group_id=group_id,
|
|
277
|
+
sender_platform_id=sender_platform_id,
|
|
278
|
+
sender_nickname=sender_nickname,
|
|
279
|
+
sender_face_url=sender_face_url,
|
|
280
|
+
session_type=session_type,
|
|
281
|
+
ex=ex,
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
def send_image(
|
|
285
|
+
self,
|
|
286
|
+
image_path: str,
|
|
287
|
+
*,
|
|
288
|
+
recv_id: str = "",
|
|
289
|
+
group_id: str = "",
|
|
290
|
+
sender_platform_id: int = 0,
|
|
291
|
+
sender_nickname: str = "",
|
|
292
|
+
sender_face_url: str = "",
|
|
293
|
+
session_type: Optional[int] = None,
|
|
294
|
+
options: Optional[Dict[str, bool]] = None,
|
|
295
|
+
offline_push_info: Optional[Dict[str, Any]] = None,
|
|
296
|
+
) -> Dict[str, Any]:
|
|
297
|
+
del options, offline_push_info
|
|
298
|
+
client_msg_id = self._generate_client_msg_id()
|
|
299
|
+
uploaded_url = self._upload_media_file(
|
|
300
|
+
file_path=image_path,
|
|
301
|
+
name=self._build_remote_name("picture", client_msg_id, image_path),
|
|
302
|
+
cause="msg-picture",
|
|
303
|
+
content_type=detect_content_type(image_path, default="image/*"),
|
|
304
|
+
)
|
|
305
|
+
return self._send_content(
|
|
306
|
+
content_type=CONTENT_PICTURE,
|
|
307
|
+
content_payload=build_picture_content_for_upload(image_path=image_path, uploaded_url=uploaded_url),
|
|
308
|
+
recv_id=recv_id,
|
|
309
|
+
group_id=group_id,
|
|
310
|
+
sender_platform_id=sender_platform_id,
|
|
311
|
+
sender_nickname=sender_nickname,
|
|
312
|
+
sender_face_url=sender_face_url,
|
|
313
|
+
session_type=session_type,
|
|
314
|
+
client_msg_id=client_msg_id,
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
def upload_image(self, image_path: str) -> Dict[str, Any]:
|
|
318
|
+
"""Upload a local image without sending an IM message."""
|
|
319
|
+
metadata = read_image_metadata(image_path)
|
|
320
|
+
url = self._upload_media_file(
|
|
321
|
+
file_path=image_path,
|
|
322
|
+
name=self._build_remote_name("picture", self._generate_client_msg_id(), image_path),
|
|
323
|
+
cause="msg-picture",
|
|
324
|
+
content_type=metadata.content_type,
|
|
325
|
+
)
|
|
326
|
+
return {
|
|
327
|
+
"url": url,
|
|
328
|
+
"width": metadata.width,
|
|
329
|
+
"height": metadata.height,
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
def send_image_by_url(
|
|
333
|
+
self,
|
|
334
|
+
*,
|
|
335
|
+
source_url: str,
|
|
336
|
+
width: int,
|
|
337
|
+
height: int,
|
|
338
|
+
recv_id: str = "",
|
|
339
|
+
group_id: str = "",
|
|
340
|
+
size: int = 0,
|
|
341
|
+
source_path: str = "",
|
|
342
|
+
content_type: str = "",
|
|
343
|
+
big_picture_url: str = "",
|
|
344
|
+
snapshot_url: str = "",
|
|
345
|
+
snapshot_width: int = 0,
|
|
346
|
+
snapshot_height: int = 0,
|
|
347
|
+
sender_platform_id: int = 0,
|
|
348
|
+
sender_nickname: str = "",
|
|
349
|
+
sender_face_url: str = "",
|
|
350
|
+
session_type: Optional[int] = None,
|
|
351
|
+
options: Optional[Dict[str, bool]] = None,
|
|
352
|
+
offline_push_info: Optional[Dict[str, Any]] = None,
|
|
353
|
+
) -> Dict[str, Any]:
|
|
354
|
+
del options, offline_push_info
|
|
355
|
+
return self._send_content(
|
|
356
|
+
content_type=CONTENT_PICTURE,
|
|
357
|
+
content_payload=build_picture_content_by_url(
|
|
358
|
+
source_url=source_url,
|
|
359
|
+
width=width,
|
|
360
|
+
height=height,
|
|
361
|
+
size=size,
|
|
362
|
+
source_path=source_path,
|
|
363
|
+
content_type=content_type,
|
|
364
|
+
big_picture_url=big_picture_url,
|
|
365
|
+
snapshot_url=snapshot_url,
|
|
366
|
+
snapshot_width=snapshot_width,
|
|
367
|
+
snapshot_height=snapshot_height,
|
|
368
|
+
),
|
|
369
|
+
recv_id=recv_id,
|
|
370
|
+
group_id=group_id,
|
|
371
|
+
sender_platform_id=sender_platform_id,
|
|
372
|
+
sender_nickname=sender_nickname,
|
|
373
|
+
sender_face_url=sender_face_url,
|
|
374
|
+
session_type=session_type,
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
def send_video(
|
|
378
|
+
self,
|
|
379
|
+
video_path: str,
|
|
380
|
+
*,
|
|
381
|
+
duration: int,
|
|
382
|
+
recv_id: str = "",
|
|
383
|
+
group_id: str = "",
|
|
384
|
+
video_type: str = "",
|
|
385
|
+
snapshot_path: str = "",
|
|
386
|
+
sender_platform_id: int = 0,
|
|
387
|
+
sender_nickname: str = "",
|
|
388
|
+
sender_face_url: str = "",
|
|
389
|
+
session_type: Optional[int] = None,
|
|
390
|
+
options: Optional[Dict[str, bool]] = None,
|
|
391
|
+
offline_push_info: Optional[Dict[str, Any]] = None,
|
|
392
|
+
) -> Dict[str, Any]:
|
|
393
|
+
del options, offline_push_info
|
|
394
|
+
client_msg_id = self._generate_client_msg_id()
|
|
395
|
+
detected_video_type = video_type or detect_content_type(video_path, default="application/octet-stream")
|
|
396
|
+
with auto_video_snapshot(video_path, snapshot_path) as resolved_snapshot_path:
|
|
397
|
+
snapshot_url = ""
|
|
398
|
+
if resolved_snapshot_path:
|
|
399
|
+
snapshot_url = self._upload_media_file(
|
|
400
|
+
file_path=resolved_snapshot_path,
|
|
401
|
+
name=self._build_remote_name("videoSnapshot", client_msg_id, resolved_snapshot_path),
|
|
402
|
+
cause="msg-video-snapshot",
|
|
403
|
+
)
|
|
404
|
+
uploaded_url = self._upload_media_file(
|
|
405
|
+
file_path=video_path,
|
|
406
|
+
name=self._build_remote_name("video", client_msg_id, video_path),
|
|
407
|
+
cause="msg-video",
|
|
408
|
+
content_type=detected_video_type,
|
|
409
|
+
)
|
|
410
|
+
return self._send_content(
|
|
411
|
+
content_type=CONTENT_VIDEO,
|
|
412
|
+
content_payload=build_video_content_for_upload(
|
|
413
|
+
video_path=video_path,
|
|
414
|
+
uploaded_url=uploaded_url,
|
|
415
|
+
duration=duration,
|
|
416
|
+
video_type=detected_video_type,
|
|
417
|
+
snapshot_path=resolved_snapshot_path,
|
|
418
|
+
snapshot_url=snapshot_url,
|
|
419
|
+
),
|
|
420
|
+
recv_id=recv_id,
|
|
421
|
+
group_id=group_id,
|
|
422
|
+
sender_platform_id=sender_platform_id,
|
|
423
|
+
sender_nickname=sender_nickname,
|
|
424
|
+
sender_face_url=sender_face_url,
|
|
425
|
+
session_type=session_type,
|
|
426
|
+
client_msg_id=client_msg_id,
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
def send_video_by_url(
|
|
430
|
+
self,
|
|
431
|
+
*,
|
|
432
|
+
video_url: str,
|
|
433
|
+
duration: int,
|
|
434
|
+
recv_id: str = "",
|
|
435
|
+
group_id: str = "",
|
|
436
|
+
video_size: int = 0,
|
|
437
|
+
video_path: str = "",
|
|
438
|
+
video_type: str = "",
|
|
439
|
+
snapshot_url: str = "",
|
|
440
|
+
snapshot_path: str = "",
|
|
441
|
+
snapshot_size: int = 0,
|
|
442
|
+
snapshot_width: int = 0,
|
|
443
|
+
snapshot_height: int = 0,
|
|
444
|
+
snapshot_type: str = "",
|
|
445
|
+
sender_platform_id: int = 0,
|
|
446
|
+
sender_nickname: str = "",
|
|
447
|
+
sender_face_url: str = "",
|
|
448
|
+
session_type: Optional[int] = None,
|
|
449
|
+
options: Optional[Dict[str, bool]] = None,
|
|
450
|
+
offline_push_info: Optional[Dict[str, Any]] = None,
|
|
451
|
+
) -> Dict[str, Any]:
|
|
452
|
+
del options, offline_push_info
|
|
453
|
+
return self._send_content(
|
|
454
|
+
content_type=CONTENT_VIDEO,
|
|
455
|
+
content_payload=build_video_content_by_url(
|
|
456
|
+
video_url=video_url,
|
|
457
|
+
duration=duration,
|
|
458
|
+
video_size=video_size,
|
|
459
|
+
video_path=video_path,
|
|
460
|
+
video_type=video_type,
|
|
461
|
+
snapshot_url=snapshot_url,
|
|
462
|
+
snapshot_path=snapshot_path,
|
|
463
|
+
snapshot_size=snapshot_size,
|
|
464
|
+
snapshot_width=snapshot_width,
|
|
465
|
+
snapshot_height=snapshot_height,
|
|
466
|
+
snapshot_type=snapshot_type,
|
|
467
|
+
),
|
|
468
|
+
recv_id=recv_id,
|
|
469
|
+
group_id=group_id,
|
|
470
|
+
sender_platform_id=sender_platform_id,
|
|
471
|
+
sender_nickname=sender_nickname,
|
|
472
|
+
sender_face_url=sender_face_url,
|
|
473
|
+
session_type=session_type,
|
|
474
|
+
)
|
|
475
|
+
|
|
476
|
+
def send_file(
|
|
477
|
+
self,
|
|
478
|
+
file_path: str,
|
|
479
|
+
*,
|
|
480
|
+
recv_id: str = "",
|
|
481
|
+
group_id: str = "",
|
|
482
|
+
file_name: str = "",
|
|
483
|
+
file_type: str = "",
|
|
484
|
+
sender_platform_id: int = 0,
|
|
485
|
+
sender_nickname: str = "",
|
|
486
|
+
sender_face_url: str = "",
|
|
487
|
+
session_type: Optional[int] = None,
|
|
488
|
+
options: Optional[Dict[str, bool]] = None,
|
|
489
|
+
offline_push_info: Optional[Dict[str, Any]] = None,
|
|
490
|
+
) -> Dict[str, Any]:
|
|
491
|
+
del options, offline_push_info
|
|
492
|
+
client_msg_id = self._generate_client_msg_id()
|
|
493
|
+
resolved_name = file_name or os.path.basename(file_path)
|
|
494
|
+
detected_file_type = file_type or detect_content_type(resolved_name or file_path, default="application/octet-stream")
|
|
495
|
+
uploaded_url = self._upload_media_file(
|
|
496
|
+
file_path=file_path,
|
|
497
|
+
name=self._build_remote_name("file", client_msg_id, resolved_name),
|
|
498
|
+
cause="msg-file",
|
|
499
|
+
content_type=detected_file_type,
|
|
500
|
+
)
|
|
501
|
+
return self._send_content(
|
|
502
|
+
content_type=CONTENT_FILE,
|
|
503
|
+
content_payload=build_file_content_for_upload(
|
|
504
|
+
file_path=file_path,
|
|
505
|
+
uploaded_url=uploaded_url,
|
|
506
|
+
file_name=resolved_name,
|
|
507
|
+
file_type=detected_file_type,
|
|
508
|
+
),
|
|
509
|
+
recv_id=recv_id,
|
|
510
|
+
group_id=group_id,
|
|
511
|
+
sender_platform_id=sender_platform_id,
|
|
512
|
+
sender_nickname=sender_nickname,
|
|
513
|
+
sender_face_url=sender_face_url,
|
|
514
|
+
session_type=session_type,
|
|
515
|
+
client_msg_id=client_msg_id,
|
|
516
|
+
)
|
|
517
|
+
|
|
518
|
+
def send_file_by_url(
|
|
519
|
+
self,
|
|
520
|
+
*,
|
|
521
|
+
source_url: str,
|
|
522
|
+
file_name: str,
|
|
523
|
+
recv_id: str = "",
|
|
524
|
+
group_id: str = "",
|
|
525
|
+
file_size: int = 0,
|
|
526
|
+
file_path: str = "",
|
|
527
|
+
file_type: str = "",
|
|
528
|
+
sender_platform_id: int = 0,
|
|
529
|
+
sender_nickname: str = "",
|
|
530
|
+
sender_face_url: str = "",
|
|
531
|
+
session_type: Optional[int] = None,
|
|
532
|
+
options: Optional[Dict[str, bool]] = None,
|
|
533
|
+
offline_push_info: Optional[Dict[str, Any]] = None,
|
|
534
|
+
) -> Dict[str, Any]:
|
|
535
|
+
del options, offline_push_info
|
|
536
|
+
return self._send_content(
|
|
537
|
+
content_type=CONTENT_FILE,
|
|
538
|
+
content_payload=build_file_content_by_url(
|
|
539
|
+
source_url=source_url,
|
|
540
|
+
file_name=file_name,
|
|
541
|
+
file_size=file_size,
|
|
542
|
+
file_path=file_path,
|
|
543
|
+
file_type=file_type,
|
|
544
|
+
),
|
|
545
|
+
recv_id=recv_id,
|
|
546
|
+
group_id=group_id,
|
|
547
|
+
sender_platform_id=sender_platform_id,
|
|
548
|
+
sender_nickname=sender_nickname,
|
|
549
|
+
sender_face_url=sender_face_url,
|
|
550
|
+
session_type=session_type,
|
|
551
|
+
)
|
|
552
|
+
|
|
553
|
+
def _send_content(
|
|
554
|
+
self,
|
|
555
|
+
*,
|
|
556
|
+
content_type: int,
|
|
557
|
+
content_payload: Dict[str, Any],
|
|
558
|
+
recv_id: str,
|
|
559
|
+
group_id: str,
|
|
560
|
+
sender_platform_id: int,
|
|
561
|
+
sender_nickname: str,
|
|
562
|
+
sender_face_url: str,
|
|
563
|
+
session_type: Optional[int],
|
|
564
|
+
at_user_id_list: Optional[Sequence[str]] = None,
|
|
565
|
+
ex: str = "",
|
|
566
|
+
client_msg_id: Optional[str] = None,
|
|
567
|
+
) -> Dict[str, Any]:
|
|
568
|
+
credentials = self._require_credentials()
|
|
569
|
+
now = int(time.time() * 1000)
|
|
570
|
+
resolved_client_msg_id = client_msg_id or self._generate_client_msg_id()
|
|
571
|
+
resolved_session_type = session_type or (SESSION_SINGLE_CHAT if recv_id else SESSION_GROUP_CHAT)
|
|
572
|
+
sender_nickname, sender_face_url = self._self_user_info_resolver.resolve_sender_profile(
|
|
573
|
+
sender_nickname,
|
|
574
|
+
sender_face_url,
|
|
575
|
+
on_error=lambda exc: logger.warning("get self user info failed: %s", exc),
|
|
576
|
+
)
|
|
577
|
+
payload: Dict[str, Any] = {
|
|
578
|
+
"recvID": recv_id,
|
|
579
|
+
"groupId": group_id,
|
|
580
|
+
"sessionType": int(resolved_session_type),
|
|
581
|
+
"contentType": int(content_type),
|
|
582
|
+
"senderPlatformID": int(sender_platform_id or self._platform_id),
|
|
583
|
+
"content": content_payload,
|
|
584
|
+
"clientMsgID": resolved_client_msg_id,
|
|
585
|
+
}
|
|
586
|
+
normalized_at_user_id_list = self._normalize_string_list(at_user_id_list)
|
|
587
|
+
if normalized_at_user_id_list:
|
|
588
|
+
payload["atUserIDList"] = normalized_at_user_id_list
|
|
589
|
+
if sender_nickname:
|
|
590
|
+
payload["senderNickname"] = sender_nickname
|
|
591
|
+
if sender_face_url:
|
|
592
|
+
payload["senderFaceURL"] = sender_face_url
|
|
593
|
+
if ex:
|
|
594
|
+
payload["ex"] = ex
|
|
595
|
+
|
|
596
|
+
response_data = self._post_send_message(payload)
|
|
597
|
+
ack = self._build_ack(response_data, client_msg_id=resolved_client_msg_id, default_send_time=now)
|
|
598
|
+
if not ack.client_msg_id:
|
|
599
|
+
return {
|
|
600
|
+
"clientMsgID": resolved_client_msg_id,
|
|
601
|
+
"serverMsgID": ack.server_msg_id,
|
|
602
|
+
"sendTime": ack.send_time,
|
|
603
|
+
"sendID": credentials.user_id,
|
|
604
|
+
}
|
|
605
|
+
return ack.to_dict() | {"sendID": credentials.user_id}
|
|
606
|
+
|
|
607
|
+
@staticmethod
|
|
608
|
+
def _normalize_string_list(values: Optional[Sequence[str]]) -> list[str]:
|
|
609
|
+
if not values:
|
|
610
|
+
return []
|
|
611
|
+
normalized: list[str] = []
|
|
612
|
+
seen: set[str] = set()
|
|
613
|
+
for value in values:
|
|
614
|
+
text = str(value or "").strip()
|
|
615
|
+
if text and text not in seen:
|
|
616
|
+
seen.add(text)
|
|
617
|
+
normalized.append(text)
|
|
618
|
+
return normalized
|
|
619
|
+
|
|
620
|
+
def _post_send_message(self, payload: Dict[str, Any]) -> Dict[str, Any]:
|
|
621
|
+
url = self._config.apiurl.rstrip("/") + "/api/v1/im/sendMessage"
|
|
622
|
+
response = httpx.post(
|
|
623
|
+
url,
|
|
624
|
+
headers={
|
|
625
|
+
"Authorization": f"Bearer {self._config.apikey}",
|
|
626
|
+
"Content-Type": "application/json",
|
|
627
|
+
},
|
|
628
|
+
json=payload,
|
|
629
|
+
timeout=self._config.request_timeout_seconds,
|
|
630
|
+
)
|
|
631
|
+
try:
|
|
632
|
+
response.raise_for_status()
|
|
633
|
+
except httpx.HTTPStatusError as exc:
|
|
634
|
+
raise MagicCampusHTTPSendHTTPError(response.status_code, response.text) from exc
|
|
635
|
+
|
|
636
|
+
body = response.json()
|
|
637
|
+
if not isinstance(body, dict):
|
|
638
|
+
raise ValueError("sendMessage response must be a JSON object")
|
|
639
|
+
return self._unwrap_send_response(body)
|
|
640
|
+
|
|
641
|
+
@staticmethod
|
|
642
|
+
def _unwrap_send_response(body: Dict[str, Any]) -> Dict[str, Any]:
|
|
643
|
+
if "errCode" in body:
|
|
644
|
+
code = int(body.get("errCode", 0) or 0)
|
|
645
|
+
if code != 0:
|
|
646
|
+
raise MagicCampusHTTPSendAPIError(
|
|
647
|
+
code,
|
|
648
|
+
str(body.get("errMsg", "") or ""),
|
|
649
|
+
str(body.get("errDlt", "") or ""),
|
|
650
|
+
)
|
|
651
|
+
data = body.get("data", {})
|
|
652
|
+
return data if isinstance(data, dict) else {}
|
|
653
|
+
|
|
654
|
+
if "code" in body:
|
|
655
|
+
code = int(body.get("code", 0) or 0)
|
|
656
|
+
if code not in (0, 200):
|
|
657
|
+
raise MagicCampusHTTPSendAPIError(
|
|
658
|
+
code,
|
|
659
|
+
str(body.get("msg", "") or ""),
|
|
660
|
+
str(body.get("detail", "") or ""),
|
|
661
|
+
)
|
|
662
|
+
data = body.get("data", {})
|
|
663
|
+
return data if isinstance(data, dict) else {}
|
|
664
|
+
|
|
665
|
+
return body
|
|
666
|
+
|
|
667
|
+
@staticmethod
|
|
668
|
+
def _build_ack(response_data: Dict[str, Any], *, client_msg_id: str, default_send_time: int) -> SendAck:
|
|
669
|
+
msg_data = response_data.get("msgData") if isinstance(response_data.get("msgData"), dict) else {}
|
|
670
|
+
return SendAck(
|
|
671
|
+
client_msg_id=str(
|
|
672
|
+
response_data.get("clientMsgID")
|
|
673
|
+
or response_data.get("clientMsgId")
|
|
674
|
+
or msg_data.get("clientMsgID")
|
|
675
|
+
or msg_data.get("clientMsgId")
|
|
676
|
+
or client_msg_id
|
|
677
|
+
),
|
|
678
|
+
server_msg_id=str(
|
|
679
|
+
response_data.get("serverMsgID")
|
|
680
|
+
or response_data.get("serverMsgId")
|
|
681
|
+
or response_data.get("msgID")
|
|
682
|
+
or response_data.get("msgId")
|
|
683
|
+
or msg_data.get("serverMsgID")
|
|
684
|
+
or msg_data.get("serverMsgId")
|
|
685
|
+
or msg_data.get("msgID")
|
|
686
|
+
or msg_data.get("msgId")
|
|
687
|
+
or ""
|
|
688
|
+
),
|
|
689
|
+
send_time=int(
|
|
690
|
+
response_data.get("sendTime")
|
|
691
|
+
or response_data.get("createTime")
|
|
692
|
+
or response_data.get("timestamp")
|
|
693
|
+
or msg_data.get("sendTime")
|
|
694
|
+
or msg_data.get("createTime")
|
|
695
|
+
or default_send_time
|
|
696
|
+
),
|
|
697
|
+
)
|
|
698
|
+
|
|
699
|
+
def _upload_media_file(
|
|
700
|
+
self,
|
|
701
|
+
*,
|
|
702
|
+
file_path: str,
|
|
703
|
+
name: str,
|
|
704
|
+
cause: str,
|
|
705
|
+
content_type: str = "",
|
|
706
|
+
) -> str:
|
|
707
|
+
credentials = self._require_credentials()
|
|
708
|
+
return upload_file(
|
|
709
|
+
api_addr=credentials.api_addr,
|
|
710
|
+
token=credentials.token,
|
|
711
|
+
timeout_seconds=self._config.request_timeout_seconds,
|
|
712
|
+
login_user_id=credentials.user_id,
|
|
713
|
+
operation_id_factory=self._operation_id,
|
|
714
|
+
file_path=file_path,
|
|
715
|
+
name=name,
|
|
716
|
+
cause=cause,
|
|
717
|
+
content_type=content_type,
|
|
718
|
+
make_http_error=lambda status, body: MagicCampusHTTPSendHTTPError(status, body),
|
|
719
|
+
make_sdk_error=lambda code, msg, dlt: MagicCampusHTTPSendAPIError(code, msg, dlt),
|
|
720
|
+
)
|
|
721
|
+
|
|
722
|
+
def _require_credentials(self) -> MagicCampusCredentials:
|
|
723
|
+
if self._credentials is None:
|
|
724
|
+
raise RuntimeError("MagicCampusHTTPSendClient is not logged in. Call login() first.")
|
|
725
|
+
return self._credentials
|
|
726
|
+
|
|
727
|
+
def _fetch_self_user_info(self) -> SelfUserInfo:
|
|
728
|
+
credentials = self._require_credentials()
|
|
729
|
+
return fetch_self_user_info(
|
|
730
|
+
api_addr=credentials.api_addr,
|
|
731
|
+
user_id=credentials.user_id,
|
|
732
|
+
token=credentials.token,
|
|
733
|
+
operation_id=self._operation_id(),
|
|
734
|
+
timeout_seconds=self._config.request_timeout_seconds,
|
|
735
|
+
make_http_error=lambda status, body: MagicCampusHTTPSendHTTPError(status, body),
|
|
736
|
+
make_sdk_error=lambda code, msg, dlt: MagicCampusHTTPSendAPIError(code, msg, dlt),
|
|
737
|
+
make_not_found_error=lambda uid: MagicCampusHTTPSendAPIError(
|
|
738
|
+
-1,
|
|
739
|
+
"self user info not found",
|
|
740
|
+
f"userID={uid}",
|
|
741
|
+
),
|
|
742
|
+
)
|
|
743
|
+
|
|
744
|
+
def _operation_id(self) -> str:
|
|
745
|
+
return f"{int(time.time() * 1e9)}{uuid.uuid4().hex[:6]}"
|
|
746
|
+
|
|
747
|
+
def _generate_client_msg_id(self) -> str:
|
|
748
|
+
raw = f"{time.time_ns()}{self.user_id}{uuid.uuid4().hex}".encode("utf-8")
|
|
749
|
+
return hashlib.md5(raw).hexdigest()
|
|
750
|
+
|
|
751
|
+
@staticmethod
|
|
752
|
+
def _build_remote_name(prefix: str, client_msg_id: str, path_or_name: str) -> str:
|
|
753
|
+
_, ext = os.path.splitext(path_or_name)
|
|
754
|
+
safe_prefix = prefix.strip("/") or "file"
|
|
755
|
+
if safe_prefix == "file":
|
|
756
|
+
base_name = os.path.basename(path_or_name)
|
|
757
|
+
return f"{safe_prefix}_{client_msg_id}/{base_name}"
|
|
758
|
+
return f"{safe_prefix}_{client_msg_id}{ext}"
|