Repository navigation
Expand file tree
/
Copy pathrunner.py
More file actions
1309 lines (1160 loc) · 51.8 KB
/
Copy pathrunner.py
File metadata and controls
1309 lines (1160 loc) · 51.8 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
import json
import time
import uuid
import random
import asyncio
import tomllib
import platform
import contextlib
from typing import Any, Dict, List, Tuple, Union, Optional
from pathlib import Path
import websockets
from aiohttp import web
from gsuid_core.i18n import t
from gsuid_core.logger import logger
from gsuid_core.models import Message, MessageSend, MessageReceive, normalize_node_items
from gsuid_core.utils.plugins_config.gs_config import bm_config
from .sign import verify_and_respond
from .config import (
QQBotUIDConfig,
is_master,
stream_setting,
update_bot_name,
maintenance_rule,
group_name_enabled,
maintenance_message,
master_only_enabled,
input_notify_seconds,
)
from .qq_api import (
FILE_TYPE_AUDIO,
FILE_TYPE_VIDEO,
SHARE_CALLBACK_LIMIT,
QQSender,
MaterialImage,
QQMateriaHost,
QQTokenManager,
brief_text,
file_bytes,
image_size,
public_image_url,
cached_group_name,
core_upload_image,
patch_image_fallback,
cancel_group_refreshes,
)
from .ws_api import ACTIVE_TEST, ADMIN_REQUEST, SHARE_LINK_REQUEST, handle_control
from .convert import (
at_tag,
send_endpoint,
build_keyboard,
event_reply_id,
is_bot_message,
recall_endpoint,
event_to_receive,
fallback_nickname,
normalize_buttons,
interaction_ack_id,
is_reference_message,
messages_to_plain_text,
)
from .webhook import WebhookServer, shared_server
from .proactive import PassiveWindow, ProactiveDispatcher
from .user_info import UserRecorder, ensure_table as ensure_user_table
from .group_info import ensure_table as ensure_group_table
from .core_bridge import CoreBridge
OP_DISPATCH = 0
OP_HEARTBEAT = 1
OP_IDENTIFY = 2
OP_RESUME = 6
OP_RECONNECT = 7
OP_INVALID_SESSION = 9
OP_HELLO = 10
# Webhook 回调专用:12 是 ACK,13 是平台发来的回调地址校验
OP_CALLBACK_ACK = 12
OP_CALLBACK_VALIDATION = 13
# 1<<0 频道 / 1<<10 频道表情表态 / 1<<24 群成员 / 1<<25 群@与单聊 / 1<<26 按钮回调 / 1<<30 公域频道消息
INTENTS = (1 << 0) | (1 << 10) | (1 << 24) | (1 << 25) | (1 << 26) | (1 << 30)
UA_PLUGIN_TAG = "QQBotPlugin/1.7.2"
CLIENT_VERSION = "9.9.9"
# 主动消息错误码以这个前缀放在回执 id 位
SEND_ERROR_PREFIX = "__send_error__:"
def _read_version() -> str:
try:
with (Path(__file__).with_name("pyproject.toml")).open("rb") as fp:
return str(tomllib.load(fp)["project"]["version"])
except (OSError, KeyError, ValueError):
return "1.0.0"
QQBOTUID_VERSION = _read_version()
STABLE_CONNECT_SECONDS = 5.0
FAST_RETRY_LIMIT = 3
FAST_RETRY_FLOOR_MS = 1000
_BROKEN_IMAGE_DATA_PATTERN = re.compile(r"Message\(type='image', data='(?P<data>[^']*)'")
# markdown 图片尺寸
_MD_SIZE_PATTERN = re.compile(r"#(?P<w>\d+)px\s*#(?P<h>\d+)px")
# 本地绝对路径:Windows 盘符 / UNC / POSIX
_LOCAL_IMAGE_PATH_PATTERN = re.compile(r"^(?:[A-Za-z]:[\\/]|\\\\|/)")
def _local_image_path(target: str) -> str:
"""markdown 图片地址是本地文件时返回文件系统路径,否则返回空串。"""
path = target[7:] if target.startswith("file://") else target
if "://" in path:
return ""
if len(path) > 3 and path[0] == "/" and path[2] == ":":
path = path[1:]
return path if _LOCAL_IMAGE_PATH_PATTERN.match(path) else ""
# 主动消息权限测试
ACTIVE_TEST_TEXT = "✅主动权限确认成功"
def _qq_error_text(data: Any) -> str:
"""把 QQ 开放平台的错误体。"""
if not isinstance(data, dict):
return brief_text(data, 200) or "平台拒绝发送"
message = str(data.get("message") or data.get("msg") or "").strip()
code = data.get("code") or data.get("err_code") or ""
if message and code:
return f"{message}(code={code})"
if message:
return message
if code:
return f"code={code}"
return brief_text(data, 200) or "平台拒绝发送"
def _warn_no_target(send: MessageSend) -> None:
"""发送目标推断不出来时统一告警"""
logger.warning(
t(
"[QQBotUID] 无法推断发送目标: {target_type}/{target_id}",
target_type=send.target_type,
target_id=send.target_id,
)
)
def _alt_size(alt: str) -> Optional[Tuple[int, int]]:
found = _MD_SIZE_PATTERN.search(alt)
if found is None:
return None
return (int(found.group("w")), int(found.group("h")))
def _with_size(alt: str, size: Optional[Tuple[int, int]]) -> str:
"""`#宽px #高px`"""
if not size or _MD_SIZE_PATTERN.search(alt):
return alt
return f"{alt} #{size[0]}px #{size[1]}px"
_PLAIN_LABELS = {"record": "[语音]", "video": "[视频]", "file": "[文件]"}
# 富媒体段 → 上传时的 file_type
_MEDIA_FILE_TYPES = {"record": FILE_TYPE_AUDIO, "video": FILE_TYPE_VIDEO}
# 这些走流式
_STREAMABLE_TYPES = {"text", "markdown", "at"}
def _pick_media(items: List[Message]) -> Tuple[Optional[Message], int]:
"""挑出第一条要单独发的富媒体段;file_type 为 0 表示走文件上传。"""
for item in items:
if not isinstance(item.data, str) or not item.data:
continue
if item.type == "file":
return item, 0
file_type = _MEDIA_FILE_TYPES.get(str(item.type))
if file_type is not None:
return item, file_type
return None, 0
def _size_after(items: List[Message], index: int) -> Optional[Tuple[int, int]]:
for item in items[index + 1 :]:
if item.type == "image_size":
data = item.data
if isinstance(data, (tuple, list)) and len(data) == 2:
return (int(data[0]), int(data[1]))
return None
if item.type == "image":
return None
return None
def _join_pieces(pieces: List[Tuple[str, bool]]) -> str:
out = ""
for text, is_image in pieces:
if not text:
continue
if is_image:
out = out.rstrip("\n")
if out:
out += "\n\n"
out += f"{text.strip()}\n\n"
continue
if out.endswith("\n\n"):
text = text.lstrip("\n")
out += text
return out.rstrip("\n")
def _iter_md_images(content: str) -> List[Tuple[int, int, int]]:
spans: List[Tuple[int, int, int]] = []
cursor = 0
while True:
start = content.find("
if alt_end < 0:
cursor = start + 2
continue
depth = 1
pos = alt_end + 2
while pos < len(content) and depth:
char = content[pos]
if char == "(":
depth += 1
elif char == ")":
depth -= 1
pos += 1
if depth:
return spans
spans.append((start, alt_end, pos))
cursor = pos
_MD_AT_PATTERN = re.compile(r"<@!?[A-Za-z0-9_\-]+>")
_AT_TAG_PATTERN = re.compile(r'<@!?[A-Za-z0-9_\-]+>|<qqbot-at-user id="[^"]*"\s*/>|<qqbot-at-everyone />')
_MD_SOFT_BREAK = re.compile(r"(?<!\n)\n(?!\n)")
_MD_IMAGE_PATTERN = re.compile(r"!\[[^\]]*\]\([^)]+\)")
def _md_hard_break(content: str) -> str:
return _MD_SOFT_BREAK.sub(" \n", content)
def _msg_seq() -> int:
return random.randint(1000000, 9999999)
def _platform_selected(config_key: str, platform: str) -> bool:
"""读核心「按钮与Markdown配置」里的平台列表"""
if not platform:
return False
selected = bm_config.get_config(config_key).data
return isinstance(selected, list) and platform in selected
def _strip_at(text: str, target_type: str) -> str:
"""过滤私聊@"""
return _AT_TAG_PATTERN.sub("", text) if target_type == "direct" else text
_REPLY_TARGETS = {"group": "group", "direct": "direct", "channel": "channel", "sub_channel": "sub_channel"}
def _reply_target_type(user_type: str) -> Optional[str]:
return _REPLY_TARGETS.get(str(user_type or ""))
def _is_lifecycle(receive: MessageReceive) -> bool:
content = receive.content or []
return bool(content) and str(content[0].type or "").startswith("meta-")
def _duration_seconds(value: Any) -> Optional[int]:
if isinstance(value, bool):
return None
if isinstance(value, int):
return max(value, 0)
if isinstance(value, str) and value.strip().isdigit():
return int(value.strip())
return None
def _md_text(item: Message) -> str:
data = item.data
if isinstance(data, dict):
content = str(data.get("content") or "")
else:
content = "" if data is None else str(data)
return _MD_AT_PATTERN.sub("", content)
def _has_image(send: MessageSend) -> bool:
for item in send.content or []:
if item.type == "image":
return True
if item.type in {"markdown", "text"} and _MD_IMAGE_PATTERN.search(_md_text(item)):
return True
return False
def _markdown_body(markdown: Optional[Message]) -> Dict[str, Any]:
if markdown is not None and isinstance(markdown.data, dict):
return dict(markdown.data)
return {}
def _user_agent(client_name: str) -> str:
runtime = f"Python/{platform.python_version()}; {platform.system().lower()}"
name = client_name.strip() or "QQBotUID"
return f"{UA_PLUGIN_TAG} ({runtime}; {name}/{CLIENT_VERSION})"
def _admin_str(data: Dict[str, Any], *keys: str) -> str:
"""按顺序取第一个非空字符串字段。"""
for key in keys:
value = str(data.get(key) or "").strip()
if value:
return value
return ""
def _admin_list(data: Dict[str, Any], *keys: str) -> List[str]:
"""取 openid 列表,单个字符串也当一项。"""
for key in keys:
raw = data.get(key)
if isinstance(raw, str):
raw = [raw]
if not isinstance(raw, list):
continue
items = [str(item).strip() for item in raw if str(item).strip()]
if items:
return items
return []
async def _admin_kick(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
group_id = _admin_str(data, "group_openid", "group_id")
openids = _admin_list(data, "member_openids", "member_openid")
if not group_id or not openids:
return {"ok": False, "error": "缺少 group_openid 或 member_openids"}
ok = await bot.sender.kick_group_members(group_id, openids, bool(data.get("add_to_blacklist")))
return {"ok": ok}
async def _admin_mute(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
group_id = _admin_str(data, "group_openid", "group_id")
openid = _admin_str(data, "member_openid", "user_id")
seconds = _duration_seconds(data.get("seconds", data.get("duration")))
if not group_id or not openid or seconds is None:
return {"ok": False, "error": "缺少 group_openid / member_openid / seconds"}
ok = await bot.sender.set_group_member_mute(group_id, openid, seconds)
return {"ok": ok}
async def _admin_mute_state(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
group_id = _admin_str(data, "group_openid", "group_id")
if not group_id:
return {"ok": False, "error": "缺少 group_openid"}
return {"ok": True, "data": await bot.sender.get_group_mute_state(group_id)}
async def _admin_bot_state(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
group_id = _admin_str(data, "group_openid", "group_id")
if not group_id:
return {"ok": False, "error": "缺少 group_openid"}
return {"ok": True, "data": await bot.sender.get_group_bot_state(group_id)}
async def _admin_group_list(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
return {"ok": True, "data": await bot.sender.get_group_list()}
async def _admin_join_requests(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
group_id = _admin_str(data, "group_openid", "group_id")
if not group_id:
return {"ok": False, "error": "缺少 group_openid"}
return {"ok": True, "data": await bot.sender.list_join_requests(group_id)}
async def _admin_approve(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
group_id = _admin_str(data, "group_openid", "group_id")
openid = _admin_str(data, "member_openid", "user_id")
if not group_id or not openid:
return {"ok": False, "error": "缺少 group_openid 或 member_openid"}
ok = await bot.sender.approve_join_request(group_id, openid, _admin_str(data, "join_request_id"))
return {"ok": ok}
async def _admin_decline(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
group_id = _admin_str(data, "group_openid", "group_id")
openid = _admin_str(data, "member_openid", "user_id")
if not group_id or not openid:
return {"ok": False, "error": "缺少 group_openid 或 member_openid"}
ok = await bot.sender.decline_join_request(
group_id,
openid,
_admin_str(data, "join_request_id"),
_admin_str(data, "reason", "reject_reason"),
bool(data.get("add_to_blacklist")),
)
return {"ok": ok}
async def _admin_blacklist(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
group_id = _admin_str(data, "group_openid", "group_id")
if not group_id:
return {"ok": False, "error": "缺少 group_openid"}
return {"ok": True, "data": await bot.sender.get_member_blacklist(group_id)}
async def _admin_blacklist_change(bot: "QQBotUID", data: Dict[str, Any], op: str) -> Dict[str, Any]:
group_id = _admin_str(data, "group_openid", "group_id")
openids = _admin_list(data, "member_openids", "member_openid")
if not group_id or not openids:
return {"ok": False, "error": "缺少 group_openid 或 member_openids"}
ok = await bot.sender.change_member_blacklist(group_id, op, openids)
return {"ok": ok}
async def _admin_blacklist_add(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
return await _admin_blacklist_change(bot, data, "add")
async def _admin_blacklist_del(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
return await _admin_blacklist_change(bot, data, "del")
async def _admin_menu_get(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
return {"ok": True, "data": await bot.sender.get_menu()}
async def _admin_menu_set(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
menu = data.get("menu")
if not isinstance(menu, dict) or not menu:
return {"ok": False, "error": "menu 必须是非空 dict"}
return {"ok": await bot.sender.change_menu(menu)}
async def _admin_panel_list(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
scope = _admin_str(data, "scope") or "group"
return {"ok": True, "data": await bot.sender.list_panels(scope)}
async def _admin_panel_create(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
panel = data.get("panel")
if not isinstance(panel, dict) or not panel:
return {"ok": False, "error": "panel 必须是非空 dict"}
scope = _admin_str(data, "scope") or "group"
target_type = _admin_str(data, "target_type") or "all"
result = await bot.sender.create_panel(
scope,
target_type,
panel,
_admin_list(data, "user_openids"),
_admin_list(data, "group_openids"),
)
return {"ok": bool(result), "data": result}
async def _admin_panel_delete(bot: "QQBotUID", data: Dict[str, Any]) -> Dict[str, Any]:
panel_id = _admin_str(data, "panel_id")
if not panel_id:
return {"ok": False, "error": "缺少 panel_id"}
return {"ok": await bot.sender.delete_panel(panel_id)}
_ADMIN_ACTIONS = {
"kick": _admin_kick,
"mute": _admin_mute,
"mute_state": _admin_mute_state,
"bot_state": _admin_bot_state,
"group_list": _admin_group_list,
"join_requests": _admin_join_requests,
"approve": _admin_approve,
"decline": _admin_decline,
"blacklist": _admin_blacklist,
"blacklist_add": _admin_blacklist_add,
"blacklist_del": _admin_blacklist_del,
"menu_get": _admin_menu_get,
"menu_set": _admin_menu_set,
"panel_list": _admin_panel_list,
"panel_create": _admin_panel_create,
"panel_delete": _admin_panel_delete,
}
class _Deferred:
def __bool__(self) -> bool:
return False
class QQBotUID:
def __init__(self, config: QQBotUIDConfig, bridge: CoreBridge):
self.config = config
self.bridge = bridge
self.tm = QQTokenManager(config)
self.sender = QQSender(self.tm)
self.material = QQMateriaHost(self.tm)
self.proactive = ProactiveDispatcher()
self.passive = PassiveWindow()
self.users = UserRecorder()
self._tasks: List[asyncio.Task[Any]] = []
self._webhook: Optional[WebhookServer] = None
self._ws = None
self._session_id: Optional[str] = None
self._seq: Optional[int] = None
self._heartbeat_interval = 45.0
self._heartbeat_task: Optional[asyncio.Task[Any]] = None
self._closed = False
self._gateway_url = ""
self._bot_name = ""
@property
def self_id(self) -> str:
return self.config.self_id
def apply_config(self, config: QQBotUIDConfig) -> None:
self.config = config
self.tm.config = config
async def start(self) -> None:
self.config.validate()
self._closed = False
patch_image_fallback()
await ensure_group_table()
await ensure_user_table()
self.bridge.add_target(self)
try:
if self.config.mode == "wh":
if self.config.webhook_enabled:
await self.start_webhook()
else:
logger.warning(
t(
"[QQBotUID] {appid} 是 Webhook 模式但未启用「启用 Webhook」,已跳过监听",
appid=self.config.appid,
)
)
if self.config.mode == "ws":
self._tasks.append(asyncio.create_task(self.qq_ws_forever()))
except Exception:
await self.stop()
raise
async def stop(self) -> None:
self._closed = True
self.bridge.remove_target(self)
if self._heartbeat_task:
self._heartbeat_task.cancel()
for task in self._tasks:
task.cancel()
await asyncio.gather(*self._tasks, return_exceptions=True)
self._tasks.clear()
if self._webhook is not None:
await self._webhook.remove(self.config.webhook_path)
self._webhook = None
await self.proactive.close()
await self.users.close()
cancel_group_refreshes()
await self.tm.aclose()
async def deliver(self, send: MessageSend) -> None:
"""核心下发的一帧:确认收件人是我,再转成 QQ 官方接口调用。"""
if send.bot_self_id and send.bot_self_id != self.self_id:
return
message_id = await self.handle_core_send(send)
if isinstance(message_id, _Deferred):
return
await self.send_recall_ack(send, message_id)
async def qq_ws_forever(self) -> None:
fails = 0
while not self._closed:
started = time.monotonic()
try:
url = await self.get_gateway_url()
headers = {"User-Agent": _user_agent(self.config.client_name)}
async with websockets.connect(url, additional_headers=headers) as ws:
self._ws = ws
async for message in ws:
await self.handle_qq_ws_message(message)
except asyncio.CancelledError:
raise
except Exception as e:
if self._closed:
continue
self._gateway_url = ""
fails = 0 if time.monotonic() - started >= STABLE_CONNECT_SECONDS else fails + 1
delay_ms = self._reconnect_delay_ms(fails)
when = f"{delay_ms}ms 后重连" if delay_ms else "立刻重连"
logger.warning(t("[QQBotUID] QQ官方WS断开,{when}: {err}", when=when, err=e))
await asyncio.sleep(delay_ms / 1000)
finally:
self._ws = None
def _reconnect_delay_ms(self, fails: int) -> int:
"""重连间隔"""
interval = max(int(self.config.reconnect_interval), 0)
if interval:
return interval
return FAST_RETRY_FLOOR_MS if fails > FAST_RETRY_LIMIT else 0
async def get_gateway_url(self) -> str:
if self.config.gateway_url:
return self.config.gateway_url
if self._gateway_url:
return self._gateway_url
client = await self.tm.client()
resp = await client.get("/gateway/bot", headers={"Authorization": await self.tm.authorization()})
data = resp.json()
self._gateway_url = str(data.get("url") or "")
if not self._gateway_url:
raise RuntimeError(f"获取 QQBot 网关失败: {data}")
return self._gateway_url
async def handle_qq_ws_message(self, message: Union[str, bytes]) -> None:
try:
payload = json.loads(message)
except json.JSONDecodeError:
return
op = payload.get("op")
if payload.get("s") is not None:
self._seq = int(payload["s"])
if op == OP_HELLO:
self._heartbeat_interval = float(payload.get("d", {}).get("heartbeat_interval", 45000)) / 1000
if self._heartbeat_task:
self._heartbeat_task.cancel()
self._heartbeat_task = asyncio.create_task(self.heartbeat_loop())
if self._session_id and self._seq is not None:
await self.send_op(
OP_RESUME,
{"token": f"QQBot {await self.tm.token()}", "session_id": self._session_id, "seq": self._seq},
)
else:
await self.send_op(
OP_IDENTIFY, {"token": f"QQBot {await self.tm.token()}", "intents": INTENTS, "shard": [0, 1]}
)
elif op == OP_DISPATCH:
await self.handle_dispatch(payload)
elif op == OP_RECONNECT:
raise RuntimeError("QQBot gateway requested reconnect")
elif op == OP_INVALID_SESSION:
self._session_id = None
self._seq = None
self._gateway_url = ""
raise RuntimeError("QQBot invalid session")
async def send_op(self, op: int, data: Any) -> None:
if self._ws is not None:
await self._ws.send(json.dumps({"op": op, "d": data}))
async def heartbeat_loop(self) -> None:
while not self._closed:
await asyncio.sleep(self._heartbeat_interval)
with contextlib.suppress(Exception):
await self.send_op(OP_HEARTBEAT, self._seq)
async def group_name(self, payload: Dict[str, Any]) -> str:
if not group_name_enabled():
return ""
raw = payload.get("d")
d = raw if isinstance(raw, dict) else {}
group_id = str(d.get("group_openid") or d.get("group_id") or "")
if not group_id:
return ""
return await cached_group_name(self.tm, group_id)
async def handle_dispatch(self, payload: Dict[str, Any]) -> None:
event_type = payload.get("t")
if event_type == "READY":
data = payload.get("d", {}) if isinstance(payload.get("d"), dict) else {}
self._session_id = data.get("session_id")
raw_user = data.get("user")
user = raw_user if isinstance(raw_user, dict) else {}
self._bot_name = str(
user.get("username")
or user.get("name")
or self.config.client_name
or self.config.self_id
or self.config.route_bot_id
)
update_bot_name(self.config.appid, self._bot_name)
logger.success(t("QQBotUID v{version} {name} 已连接", version=QQBOTUID_VERSION, name=self._bot_name))
return
if event_type == "RESUMED":
logger.success(t("[QQBotUID] QQ官方WS会话已恢复"))
return
await self.ack_interaction(payload)
receive = event_to_receive(
payload,
self.config.platform_bot_id,
self.config.self_id,
self.config.appid,
await self.group_name(payload),
await fallback_nickname(payload.get("d") or {}, self.config.platform_bot_id),
)
if receive is not None:
# debug 下打印原始事件
logger.debug(
t("[QQBotUID] WS 事件 {event_type}: {raw}", event_type=event_type, raw=brief_text(payload, 1500))
)
if self._skip_other_bot(payload):
return
self._warn_unresolved_reference(payload, receive)
self.passive.mark(receive.msg_id)
self.users.observe(receive)
await self.maybe_input_notify(receive)
if await self.maintenance_reply(receive):
return
await self.bridge.push(receive)
async def maybe_input_notify(self, receive: MessageReceive) -> None:
"""私聊先发「正在输入」;剩最后一次被动额度时不发,留给真回复。"""
seconds = input_notify_seconds()
if not seconds or receive.user_type != "direct" or not receive.user_id:
return
if self.passive.remaining(receive.msg_id) <= 1 or not self.passive.take(receive.msg_id):
return
await self.sender.send_input_notify(receive.user_id, seconds, receive.msg_id)
async def ack_interaction(self, payload: Dict[str, Any]) -> None:
"""按钮回调立刻回应"""
interaction_id = interaction_ack_id(payload)
if not interaction_id:
return
try:
ok = await self.sender.reply_interaction(interaction_id)
except Exception as error:
logger.warning(t("[QQBotUID] 按钮回调回应失败: {error}", error=error))
return
if not ok:
logger.debug(t("[QQBotUID] 按钮回调回应未成功: {id}", id=interaction_id))
def _skip_other_bot(self, payload: Dict[str, Any]) -> bool:
"""开了「忽略其他机器人消息」时,别的 bot 发的消息不进核心。"""
if not self.config.ignore_bot_message or not is_bot_message(payload):
return False
logger.debug(t("[QQBotUID] 已忽略其他机器人消息: {event}", event=str(payload.get("t") or "")))
return True
def _warn_unresolved_reference(self, payload: Dict[str, Any], receive: MessageReceive) -> None:
if any(item.type == "reply_id" for item in receive.content):
return
if not is_reference_message(payload):
return
raw_d = payload.get("d")
d = raw_d if isinstance(raw_d, dict) else {}
elements = [
{key: item.get(key) for key in ("msg_idx", "type")}
for item in d.get("msg_elements") or []
if isinstance(item, dict)
]
logger.warning(
t(
"[QQBotUID] 引用消息没解析出引用 ID: message_type={message_type} scene={scene} elements={elements}",
message_type=d.get("message_type"),
scene=d.get("message_scene"),
elements=elements,
)
)
async def maintenance_reply(self, receive: MessageReceive) -> bool:
"""维护期间用提示顶替核心的回复,返回 True 表示已回提示。"""
message = self._maintenance_message(receive)
if not message:
return False
target_type = _reply_target_type(receive.user_type)
if target_type is None:
return False
target_id = receive.user_id if target_type == "direct" else receive.group_id
if not target_id:
return False
send = MessageSend(
bot_id=receive.bot_id,
bot_self_id=receive.bot_self_id,
msg_id=receive.msg_id,
target_type=target_type,
target_id=target_id,
content=[Message(type="text", data=message)],
)
endpoint = send_endpoint(send)
if endpoint is None:
return False
logger.info(f"[QQBotUID] 维护中,已回提示: {target_type}/{target_id}")
await self.post_message(send, endpoint, receive.msg_id)
return True
def _maintenance_message(self, receive: MessageReceive) -> str:
"""该回维护提示就返回文案;放行返回空串。"""
if _is_lifecycle(receive):
# 进群 / 退群这类事件不是「有人在说话」,别往群里刷维护提示
return ""
if master_only_enabled() and not is_master(receive.user_id):
return maintenance_message()
rule = maintenance_rule()
if rule is None:
return ""
active = getattr(self.bridge, "maintenance_active", None)
if not callable(active) or not active(rule.threshold):
return ""
if not rule.matches(messages_to_plain_text(receive.content or [], receive.user_type)):
return ""
return rule.message
async def start_webhook(self) -> None:
self._webhook = shared_server(self.config.webhook_host, self.config.webhook_port)
await self._webhook.add(self.config.webhook_path, self.handle_webhook)
async def handle_webhook(self, request: web.Request) -> web.Response:
raw = await request.read()
try:
body = json.loads(raw)
except json.JSONDecodeError:
return web.json_response({"error": "invalid JSON"}, status=400)
if body.get("op") == OP_CALLBACK_VALIDATION:
resp = verify_and_respond(body, self.config.secret)
if resp is None:
return web.json_response({"error": "invalid validation"}, status=400)
return web.json_response(resp)
await self.ack_interaction(body)
receive = event_to_receive(
body,
self.config.platform_bot_id,
self.config.self_id,
self.config.appid,
await self.group_name(body),
await fallback_nickname(body.get("d") or {}, self.config.platform_bot_id),
)
if receive is not None:
logger.debug(
t("[QQBotUID] Webhook 事件 {event_type}: {raw}", event_type=body.get("t"), raw=brief_text(body, 1500))
)
if not self._skip_other_bot(body):
self.passive.mark(receive.msg_id)
self.users.observe(receive)
await self.maybe_input_notify(receive)
if not await self.maintenance_reply(receive):
await self.bridge.push(receive)
if body.get("t") == "INTERACTION_CREATE":
return web.json_response({"op": OP_CALLBACK_ACK, "d": {}})
return web.json_response({})
async def send_recall_ack(self, send: MessageSend, message_id: Optional[Union[str, List[str]]]) -> None:
if not send.echo:
return
ack = event_to_receive(
{
"t": "QQBOTUID_RECALL_ACK",
"d": {
"id": "",
"content": "",
"author": {"id": ""},
},
},
self.config.platform_bot_id,
self.config.self_id,
self.config.appid,
)
if ack is None:
return
ack.content = [Message(type="recall_message_id", data={"echo": send.echo, "id": message_id})]
await self.bridge.push(ack)
async def handle_ban(self, data: Dict[str, Any]) -> None:
"""核心 bot.ban() 下发的禁言"""
user_id = str(data.get("user_id") or "")
group_id = str(data.get("group_id") or "")
seconds = _duration_seconds(data.get("duration"))
if not user_id or not group_id or seconds is None:
logger.warning(
t(
"[QQBotUID] 禁言参数不完整,已忽略: user={user_id} group={group_id} duration={duration}",
user_id=user_id,
group_id=group_id,
duration=data.get("duration"),
)
)
return
if await self.sender.set_group_member_mute(group_id, user_id, seconds):
if seconds:
logger.info(t("[QQBotUID] 已禁言 {user_id} {sec} 秒", user_id=user_id, sec=seconds))
else:
logger.info(t("[QQBotUID] 已解除禁言 {user_id}", user_id=user_id))
async def handle_core_send(self, send: MessageSend) -> Optional[Union[str, List[str], _Deferred]]:
if not send.content:
return None
first = send.content[0]
if send.bot_id not in {self.config.platform_bot_id, self.config.route_bot_id}:
logger.debug(t("[QQBotUID] 忽略非官机平台下发消息: bot_id={bot_id}", bot_id=send.bot_id))
return None
control = await handle_control(first.type, first.data, self.config.platform_bot_id)
if control is not None:
return control
if first.type == ACTIVE_TEST:
return await self.active_test_reply(send, first.data)
if first.type == ADMIN_REQUEST:
return await self.admin_reply(first.data)
if first.type == SHARE_LINK_REQUEST:
return await self.share_link_reply(first.data)
if first.type == "excute_delete_message" and isinstance(first.data, dict):
mid = str(first.data.get("message_id") or "")
endpoint = recall_endpoint(send, mid) if mid else None
return mid if endpoint and await self.sender.delete(endpoint) else None
if first.type == "excute_ban_user" and isinstance(first.data, dict):
await self.handle_ban(first.data)
return None
node = next((item for item in send.content if item.type == "node" and isinstance(item.data, list)), None)
if node is not None:
message_ids = await self.send_node_items(send, node.data, send.msg_id)
return message_ids or _Deferred()
endpoint = send_endpoint(send)
if endpoint is None:
_warn_no_target(send)
return None
if not self.passive.take(send.msg_id):
self._log_passive_fallback(send)
self.proactive.submit(send.target_type, lambda: self.send_proactive(send, endpoint))
return _Deferred()
streamed = await self.try_stream(send, send.msg_id)
if streamed is not None:
return streamed
return await self.post_message(send, endpoint, send.msg_id)
async def try_stream(self, send: MessageSend, msg_id: str) -> Optional[str]:
"""私聊纯文本走流式;返回 message_id 表示已发出,``None`` 表示该回退普通发送。"""
setting = stream_setting()
if setting is None or send.target_type != "direct" or not send.target_id:
return None
if _has_image(send):
logger.debug("[QQBotUID] 回复里带图片,不走流式,直接普通发送")
return None
if any(str(item.type or "") not in _STREAMABLE_TYPES for item in send.content or []):
return None
content = await self._segments_markdown(send)
if not content.strip():
return None
if not any(item.type == "markdown" for item in send.content or []):
content = _md_hard_break(content)
chunk_size = setting.chunk_size
if chunk_size >= len(content):
chunk_size = max(1, (len(content) + 1) // 2)
event_id = event_reply_id(msg_id)
ok, stream_id = await self.sender.send_stream(
send.target_id,
content,
msg_id="" if event_id else msg_id,
event_id=event_id,
chunk_size=chunk_size,
delay_ms=setting.delay_ms,
)
if ok:
return stream_id or None
if stream_id:
return stream_id
logger.warning("[QQBotUID] 流式发送失败,回退普通消息")
return None
def _log_passive_fallback(self, send: MessageSend) -> None:
"""被动额度用完(5 次)或已过期,改走主动通道。"""
if not send.msg_id:
return
logger.debug(
f"[QQBotUID] 被动回复不可用(剩余 {self.passive.remaining(send.msg_id)} 次),"
f"转主动发送: {send.target_type}/{send.target_id} msg_id={send.msg_id}"
)
async def active_test_reply(self, send: MessageSend, data: Any) -> str:
"""核心通用协议 gscore_qqbot_active_test"""
payload = data if isinstance(data, dict) else {}
target_type = "direct" if str(payload.get("target_type") or "") == "direct" else "group"
target_id = str(payload.get("target_id") or "")
probe = MessageSend(
bot_id=send.bot_id,
bot_self_id=send.bot_self_id,
target_type=target_type,
target_id=target_id,
content=[Message(type="text", data=ACTIVE_TEST_TEXT)],
)
endpoint = send_endpoint(probe)
if endpoint is None:
return json.dumps({"ok": False, "message_id": None, "error": "缺少有效的发送目标"}, ensure_ascii=False)
message_id, error = await self.post_message_detail(probe, endpoint, "")
logger.debug(
f"[QQBotUID] 主动消息权限测试: target={target_type}/{target_id} "
f"ok={bool(message_id)} error={error!r}"
)
if message_id:
return json.dumps({"ok": True, "message_id": message_id, "error": ""}, ensure_ascii=False)
return json.dumps(
{"ok": False, "message_id": None, "error": error or "主动发送失败,可能是权限不足或频次限制"},
ensure_ascii=False,
)
async def share_link_reply(self, data: Any) -> str: