Skip to content

The agent that grows with you

refactor(gateway/platforms): qqbot chunked_upload/keyboards/onboard second pass — fold layouts, drop banners, compact docs

Teknium30cc3d763c4b
diff --git a/gateway/platforms/qqbot/chunked_upload.py b/gateway/platforms/qqbot/chunked_upload.py
index 8b9c5c448f3..87434faffa5 100644
--- a/gateway/platforms/qqbot/chunked_upload.py
+++ b/gateway/platforms/qqbot/chunked_upload.py
@@ -20,11 +20,11 @@ from gateway.platforms.qqbot.constants import FILE_UPLOAD_TIMEOUT
logger = logging.getLogger(__name__)
-_BIZ_CODE_DAILY_LIMIT = 40093002 # upload_prepare: daily cumulative limit
+_BIZ_CODE_DAILY_LIMIT = 40093002 # upload_prepare: daily cumulative limit
_BIZ_CODE_PART_RETRYABLE = 40093001 # upload_part_finish: transient
_DEFAULT_CONCURRENT_PARTS = 1
_MAX_CONCURRENT_PARTS = 10
-_PART_UPLOAD_TIMEOUT = 300.0 # 5 minutes per COS PUT
+_PART_UPLOAD_TIMEOUT = 300.0 # 5 minutes per COS PUT
_PART_UPLOAD_MAX_RETRIES = 2
_PART_FINISH_RETRY_INTERVAL = 1.0
_PART_FINISH_DEFAULT_TIMEOUT = 120.0
@@ -33,8 +33,6 @@ _COMPLETE_UPLOAD_MAX_RETRIES = 2
_COMPLETE_UPLOAD_BASE_DELAY = 2.0
_MD5_10M_SIZE = 10_002_432 # first N bytes used for the ``md5_10m`` hash (per QQ API spec)
-# ── Exceptions ──
-
class _UploadError(Exception):
def __init__(self, file_name: str, file_size: int, message: str) -> None:
self.file_name = file_name
@@ -59,18 +57,14 @@ class UploadFileTooLargeError(_UploadError):
def __init__(self, file_name: str, file_size: int, limit_bytes: int = 0, message: str = "") -> None:
self.limit_bytes = limit_bytes
limit_str = f" ({format_size(limit_bytes)})" if limit_bytes else ""
- super().__init__(
- file_name, file_size,
- message or f"File {file_name!r} ({format_size(file_size)}) exceeds platform limit{limit_str}",
- )
+ super().__init__(file_name, file_size,
+ message or f"File {file_name!r} ({format_size(file_size)}) exceeds platform limit{limit_str}")
@property
def limit_human(self) -> str:
return format_size(self.limit_bytes) if self.limit_bytes else "unknown"
-# ── Prepare-response shape ──
-
@dataclass
class _PreparePart:
index: int
@@ -97,27 +91,19 @@ def _parse_prepare_response(raw: Dict[str, Any]) -> _PrepareResult:
raw_parts = src.get("parts") or src.get("part_list") or []
if not isinstance(raw_parts, list) or not raw_parts:
raise ValueError(f"upload_prepare response missing parts: {str(raw)[:200]}")
- parts = [
- _PreparePart(
- index=int(p.get("part_index") or p.get("index") or 0),
- presigned_url=str(p.get("presigned_url") or p.get("url") or ""),
- block_size=int(p.get("block_size", 0)))
- for p in raw_parts
- if isinstance(p, dict)]
+ parts = [_PreparePart(index=int(p.get("part_index") or p.get("index") or 0),
+ presigned_url=str(p.get("presigned_url") or p.get("url") or ""),
+ block_size=int(p.get("block_size", 0)))
+ for p in raw_parts if isinstance(p, dict)]
return _PrepareResult(
- upload_id=upload_id,
- block_size=block_size,
- parts=parts,
+ upload_id=upload_id, block_size=block_size, parts=parts,
concurrency=int(src.get("concurrency", _DEFAULT_CONCURRENT_PARTS)) or _DEFAULT_CONCURRENT_PARTS,
retry_timeout=float(src.get("retry_timeout", 0.0) or 0.0))
def _api_path(chat_type: str, target_id: str, endpoint: str) -> str:
- base = "/v2/users" if chat_type == "c2c" else "/v2/groups"
- return f"{base}/{target_id}/{endpoint}"
-
+ return f"{'/v2/users' if chat_type == 'c2c' else '/v2/groups'}/{target_id}/{endpoint}"
-# ── Chunked upload driver ──
@dataclass
class _Job:
@@ -142,9 +128,8 @@ class ChunkedUploader:
circular import; must raise RuntimeError with the biz_code in the message);
``http_put`` is ``(url, data, headers) -> httpx-like response`` for COS PUTs."""
- def __init__(
- self, api_request: Callable[..., Awaitable[Dict[str, Any]]], http_put: Callable[..., Awaitable[Any]],
- log_tag: str = "QQBot") -> None:
+ def __init__(self, api_request: Callable[..., Awaitable[Dict[str, Any]]],
+ http_put: Callable[..., Awaitable[Any]], log_tag: str = "QQBot") -> None:
self._api_request = api_request
self._http_put = http_put
self._log_tag = log_tag
@@ -158,24 +143,18 @@ class ChunkedUploader:
Raises UploadDailyLimitExceededError (40093002), UploadFileTooLargeError, RuntimeError."""
if chat_type not in {"c2c", "group"}:
raise ValueError(f"ChunkedUploader: unsupported chat_type {chat_type!r}")
-
job = _Job(chat_type, target_id, file_path, Path(file_path).stat().st_size)
- logger.info(
- "[%s] Chunked upload start: file=%s size=%s type=%d",
- self._log_tag, file_name, format_size(job.file_size), file_type)
-
+ logger.info("[%s] Chunked upload start: file=%s size=%s type=%d", self._log_tag, file_name,
+ format_size(job.file_size), file_type)
# Hashing is blocking I/O → executor.
hashes = await asyncio.get_running_loop().run_in_executor(None, _compute_file_hashes, file_path, job.file_size)
prepare = await self._prepare(job, file_type, file_name, hashes)
max_concurrent = min(prepare.concurrency, _MAX_CONCURRENT_PARTS)
job.upload_id, job.block_size, job.total_parts = prepare.upload_id, prepare.block_size, len(prepare.parts)
- job.retry_timeout = min(
- prepare.retry_timeout if prepare.retry_timeout > 0 else _PART_FINISH_DEFAULT_TIMEOUT,
- _PART_FINISH_MAX_TIMEOUT)
- logger.info(
- "[%s] Prepared: upload_id=%s block_size=%s parts=%d concurrency=%d",
- self._log_tag, job.upload_id, format_size(job.block_size), job.total_parts, max_concurrent)
-
+ job.retry_timeout = min(prepare.retry_timeout if prepare.retry_timeout > 0 else _PART_FINISH_DEFAULT_TIMEOUT,
+ _PART_FINISH_MAX_TIMEOUT)
+ logger.info("[%s] Prepared: upload_id=%s block_size=%s parts=%d concurrency=%d", self._log_tag,
+ job.upload_id, format_size(job.block_size), job.total_parts, max_concurrent)
sem = asyncio.Semaphore(max(max_concurrent, 1))
async def _run(part: _PreparePart) -> None:
@@ -187,42 +166,34 @@ class ChunkedUploader:
return await self._complete(job)
async def _prepare(self, job: _Job, file_type: int, file_name: str, hashes: Dict[str, str]) -> _PrepareResult:
- body = {
- "file_type": file_type, "file_name": file_name, "file_size": job.file_size,
- "md5": hashes["md5"], "sha1": hashes["sha1"], "md5_10m": hashes["md5_10m"]}
+ body = {"file_type": file_type, "file_name": file_name, "file_size": job.file_size,
+ "md5": hashes["md5"], "sha1": hashes["sha1"], "md5_10m": hashes["md5_10m"]}
try:
raw = await self._post(job, "upload_prepare", body)
except RuntimeError as exc:
- err_msg = str(exc)
- if f"{_BIZ_CODE_DAILY_LIMIT}" in err_msg:
- raise UploadDailyLimitExceededError(file_name, job.file_size, err_msg) from exc
+ if f"{_BIZ_CODE_DAILY_LIMIT}" in str(exc):
+ raise UploadDailyLimitExceededError(file_name, job.file_size, str(exc)) from exc
raise
return _parse_prepare_response(raw)
async def _upload_one_part(self, job: _Job, part: _PreparePart) -> None:
"""PUT one part to COS, then call ``upload_part_finish``."""
part_index, total_parts = part.index, job.total_parts
- # Per-part block_size wins; fall back to the response-level value.
- actual_block_size = part.block_size if part.block_size > 0 else job.block_size
offset = (part_index - 1) * job.block_size
- length = min(actual_block_size, job.file_size - offset)
-
+ # Per-part block_size wins; fall back to the response-level value.
+ length = min(part.block_size if part.block_size > 0 else job.block_size, job.file_size - offset)
data = await asyncio.get_running_loop().run_in_executor(None, _read_file_chunk, job.file_path, offset, length)
md5_hex = hashlib.md5(data).hexdigest()
- logger.debug(
- "[%s] Part %d/%d: uploading %s (offset=%d md5=%s)",
- self._log_tag, part_index, total_parts, format_size(length), offset, md5_hex)
-
+ logger.debug("[%s] Part %d/%d: uploading %s (offset=%d md5=%s)", self._log_tag, part_index, total_parts,
+ format_size(length), offset, md5_hex)
await self._put_to_presigned_url(part.presigned_url, data, part_index, total_parts)
await self._part_finish_with_retry(job, part_index, length, md5_hex)
-
job.completed += 1
- logger.debug(
- "[%s] Part %d/%d done (%d/%d total)", self._log_tag, part_index, total_parts, job.completed, total_parts)
+ logger.debug("[%s] Part %d/%d done (%d/%d total)", self._log_tag, part_index, total_parts, job.completed,
+ total_parts)
- async def _with_retries(
- self, attempt_fn: Callable[[], Awaitable[Any]], *, max_retries: int, base_delay: float, label: str,
- failure_label: str) -> Any:
+ async def _with_retries(self, attempt_fn: Callable[[], Awaitable[Any]], *, max_retries: int, base_delay: float,
+ label: str, failure_label: str) -> Any:
"""Run *attempt_fn* up to ``max_retries + 1`` times with exponential backoff."""
last_exc: Exception | None = None
for attempt in range(max_retries + 1):
@@ -232,9 +203,8 @@ class ChunkedUploader:
last_exc = exc
if attempt < max_retries:
delay = base_delay * (2 ** attempt)
- logger.warning(
- "[%s] %s attempt %d failed, retry in %.1fs: %s",
- self._log_tag, label, attempt + 1, delay, exc)
+ logger.warning("[%s] %s attempt %d failed, retry in %.1fs: %s", self._log_tag, label, attempt + 1,
+ delay, exc)
await asyncio.sleep(delay)
raise RuntimeError(f"{failure_label} failed after {max_retries + 1} attempts: {last_exc}")
@@ -242,9 +212,8 @@ class ChunkedUploader:
"""PUT part data to a pre-signed COS URL with retry."""
async def _attempt() -> None:
- resp = await asyncio.wait_for(
- self._http_put(url, data=data, headers={"Content-Length": str(len(data))}),
- timeout=_PART_UPLOAD_TIMEOUT)
+ resp = await asyncio.wait_for(self._http_put(url, data=data, headers={"Content-Length": str(len(data))}),
+ timeout=_PART_UPLOAD_TIMEOUT)
status = getattr(resp, "status_code", 0)
if 200 <= status < 300:
logger.debug("[%s] PUT part %d/%d: %d OK", self._log_tag, part_index, total_parts, status)
@@ -255,16 +224,15 @@ class ChunkedUploader:
body_preview = ""
raise RuntimeError(f"COS PUT returned {status}: {body_preview}")
- await self._with_retries(
- _attempt, max_retries=_PART_UPLOAD_MAX_RETRIES, base_delay=1.0,
- label=f"PUT part {part_index}/{total_parts}", failure_label=f"Part {part_index}/{total_parts} upload")
+ await self._with_retries(_attempt, max_retries=_PART_UPLOAD_MAX_RETRIES, base_delay=1.0,
+ label=f"PUT part {part_index}/{total_parts}",
+ failure_label=f"Part {part_index}/{total_parts} upload")
async def _part_finish_with_retry(self, job: _Job, part_index: int, block_size: int, md5: str) -> None:
"""Call ``upload_part_finish``, retrying on biz_code 40093001 until ``job.retry_timeout``."""
body = {"upload_id": job.upload_id, "part_index": part_index, "block_size": block_size, "md5": md5}
loop = asyncio.get_running_loop()
- start = loop.time()
- attempt = 0
+ start, attempt = loop.time(), 0
while True:
try:
await self._post(job, "upload_part_finish", body)
@@ -274,25 +242,20 @@ class ChunkedUploader:
raise
elapsed = loop.time() - start
if elapsed >= job.retry_timeout:
- raise RuntimeError(
- f"upload_part_finish persistent retry timed out "
- f"after {job.retry_timeout:.0f}s ({attempt} retries): {exc}") from exc
+ raise RuntimeError(f"upload_part_finish persistent retry timed out after "
+ f"{job.retry_timeout:.0f}s ({attempt} retries): {exc}") from exc
attempt += 1
- logger.debug(
- "[%s] part_finish retryable error, attempt %d, elapsed=%.1fs: %s",
- self._log_tag, attempt, elapsed, exc)
+ logger.debug("[%s] part_finish retryable error, attempt %d, elapsed=%.1fs: %s", self._log_tag, attempt,
+ elapsed, exc)
await asyncio.sleep(_PART_FINISH_RETRY_INTERVAL)
async def _complete(self, job: _Job) -> Dict[str, Any]:
- """Call ``complete_upload`` with retry — the ``/files`` endpoint (same as the simple
- URL upload) selects the chunked-completion path when only ``upload_id`` is sent."""
+ """Call ``complete_upload`` with retry — the ``/files`` endpoint (same as the simple URL upload)
+ selects the chunked-completion path when only ``upload_id`` is sent."""
return await self._with_retries(
- lambda: self._post(job, "files", {"upload_id": job.upload_id}),
- max_retries=_COMPLETE_UPLOAD_MAX_RETRIES, base_delay=_COMPLETE_UPLOAD_BASE_DELAY,
- label="complete_upload", failure_label="complete_upload")
-
+ lambda: self._post(job, "files", {"upload_id": job.upload_id}), max_retries=_COMPLETE_UPLOAD_MAX_RETRIES,
+ base_delay=_COMPLETE_UPLOAD_BASE_DELAY, label="complete_upload", failure_label="complete_upload")
-# ── Helpers (module-level for testability) ──
def format_size(size_bytes: int) -> str:
"""Return a human-readable file size string (e.g. ``'12.3 MB'``)."""
@@ -309,32 +272,23 @@ def _read_file_chunk(file_path: str, offset: int, length: int) -> bytes:
with open(file_path, "rb") as fh:
fh.seek(offset)
data = fh.read(length)
- if len(data) != length:
- raise IOError(
- f"Short read from {file_path}: expected {length} bytes at "
- f"offset {offset}, got {len(data)} (file may be truncated)")
- return data
+ if len(data) != length:
+ raise IOError(f"Short read from {file_path}: expected {length} bytes at offset {offset}, got {len(data)} "
+ f"(file may be truncated)")
+ return data
def _compute_file_hashes(file_path: str, file_size: int) -> Dict[str, str]:
- """Compute md5, sha1, and md5_10m in a single pass."""
- md5 = hashlib.md5()
- sha1 = hashlib.sha1()
- md5_10m = hashlib.md5()
+ """Compute md5, sha1, and md5_10m in a single pass (for small files md5_10m is just the full md5)."""
+ md5, sha1, md5_10m = hashlib.md5(), hashlib.sha1(), hashlib.md5()
need_10m = file_size > _MD5_10M_SIZE
bytes_read = 0
with open(file_path, "rb") as fh:
while chunk := fh.read(65536):
md5.update(chunk)
sha1.update(chunk)
- if need_10m:
- remaining = _MD5_10M_SIZE - bytes_read
- if remaining > 0:
- md5_10m.update(chunk[:remaining])
+ if need_10m and (remaining := _MD5_10M_SIZE - bytes_read) > 0:
+ md5_10m.update(chunk[:remaining])
bytes_read += len(chunk)
full_md5 = md5.hexdigest()
- return {
- "md5": full_md5,
- "sha1": sha1.hexdigest(),
- # For small files the "10m" hash is just the full md5.
- "md5_10m": md5_10m.hexdigest() if need_10m else full_md5}
+ return {"md5": full_md5, "sha1": sha1.hexdigest(), "md5_10m": md5_10m.hexdigest() if need_10m else full_md5}
diff --git a/gateway/platforms/qqbot/keyboards.py b/gateway/platforms/qqbot/keyboards.py
index be608cb9b02..a6f205ce9df 100644
--- a/gateway/platforms/qqbot/keyboards.py
+++ b/gateway/platforms/qqbot/keyboards.py
@@ -1,11 +1,8 @@
-"""QQ Bot inline keyboards + approval / update-prompt helpers.
-
-A button click dispatches an ``INTERACTION_CREATE`` event carrying the button's
-``data``; the bot must ACK promptly via ``PUT /interactions/{id}`` or the user
-sees an error indicator. ``button_data`` formats: ``approve:<session_key>:<decision>``
-(decision = allow-once|allow-always|deny) and ``update_prompt:<answer>`` (y|n).
-Ported from WideLee's qqbot-agent-sdk v1.2.2 (authorship via Co-authored-by).
-"""
+"""QQ Bot inline keyboards + approval / update-prompt helpers. A button click dispatches an
+``INTERACTION_CREATE`` event carrying the button's ``data``; the bot must ACK promptly via
+``PUT /interactions/{id}`` or the user sees an error indicator. ``button_data`` formats:
+``approve:<session_key>:<decision>`` (allow-once|allow-always|deny) and ``update_prompt:<answer>`` (y|n).
+Ported from WideLee's qqbot-agent-sdk v1.2.2 (authorship via Co-authored-by)."""
from __future__ import annotations
@@ -16,13 +13,10 @@ from typing import Any, Dict, List, Optional
APPROVAL_BUTTON_PREFIX = "approve:"
UPDATE_PROMPT_PREFIX = "update_prompt:"
-# session_key may itself contain colons (e.g. agent:main:qqbot:c2c:OPENID),
-# so the session_key group is greedy but trails the decision.
+# session_key may itself contain colons (agent:main:qqbot:c2c:OPENID): greedy group, decision trails.
_APPROVAL_DATA_RE = re.compile(r"^approve:(.+):(allow-once|allow-always|deny)$")
_UPDATE_PROMPT_RE = re.compile(r"^update_prompt:(y|n)$")
-# ── Keyboard dataclasses ──
-
def _to_dict(value: Any) -> Any:
"""Serialize a dataclass tree in field-declaration order (the wire shape)."""
if is_dataclass(value):
@@ -31,8 +25,7 @@ def _to_dict(value: Any) -> Any:
class _Serializable:
- def to_dict(self) -> Dict[str, Any]:
- return _to_dict(self)
+ to_dict = _to_dict
@dataclass
@@ -84,8 +77,6 @@ class InlineKeyboard(_Serializable):
content: KeyboardContent = field(default_factory=KeyboardContent)
-# ── INTERACTION_CREATE parsing ──
-
def parse_approval_button_data(button_data: str) -> Optional[tuple[str, str]]:
"""Parse approval ``button_data`` into ``(session_key, decision)`` or ``None``."""
m = _APPROVAL_DATA_RE.match(button_data or "")
@@ -98,22 +89,18 @@ def parse_update_prompt_button_data(button_data: str) -> Optional[str]:
return m.group(1) if m else None
-# ── Keyboard builders ──
-
def _single_row_keyboard(group_id: str, *buttons: tuple) -> InlineKeyboard:
"""One row of callback buttons from ``(id, label, visited_label, data, style)`` tuples."""
row = KeyboardRow(buttons=[
- KeyboardButton(
- id=btn_id, group_id=group_id, action=KeyboardButtonAction(type=1, data=data),
- render_data=KeyboardButtonRenderData(label=label, visited_label=visited, style=style))
+ KeyboardButton(id=btn_id, group_id=group_id, action=KeyboardButtonAction(type=1, data=data),
+ render_data=KeyboardButtonRenderData(label=label, visited_label=visited, style=style))
for btn_id, label, visited, data, style in buttons])
return InlineKeyboard(content=KeyboardContent(rows=[row]))
def build_approval_keyboard(session_key: str, *, allow_permanent: bool = True) -> InlineKeyboard:
- """Build ``[✅ 允许一次] [⭐ 始终允许] [❌ 拒绝]`` (one group, so a click greys the rest).
- ⭐ is hidden when persistent scope is unavailable; *session_key* is embedded in
- ``button_data`` so the decision routes to the right approval."""
+ """Build ``[✅ 允许一次] [⭐ 始终允许] [❌ 拒绝]`` (one group, so a click greys the rest). ⭐ is hidden when
+ persistent scope is unavailable; *session_key* rides in ``button_data`` so the decision routes correctly."""
prefix = f"{APPROVAL_BUTTON_PREFIX}{session_key}"
buttons = [("allow", "✅ 允许一次", "已允许", f"{prefix}:allow-once", 1)]
if allow_permanent:
@@ -124,18 +111,14 @@ def build_approval_keyboard(session_key: str, *, allow_permanent: bool = True) -
def build_update_prompt_keyboard() -> InlineKeyboard:
"""Build a Yes/No keyboard for update confirmation prompts."""
- return _single_row_keyboard(
- "update_prompt",
- ("yes", "✓ 确认", "已确认", f"{UPDATE_PROMPT_PREFIX}y", 1),
- ("no", "✗ 取消", "已取消", f"{UPDATE_PROMPT_PREFIX}n", 0))
-
+ return _single_row_keyboard("update_prompt", ("yes", "✓ 确认", "已确认", f"{UPDATE_PROMPT_PREFIX}y", 1),
+ ("no", "✗ 取消", "已取消", f"{UPDATE_PROMPT_PREFIX}n", 0))
-# ── ApprovalRequest + text builder ──
@dataclass
class ApprovalRequest:
- """Approval-request display data. ``command_preview`` / ``cwd`` are set for exec
- approvals, ``tool_name`` for plugin approvals; ``severity`` is ``'critical' | 'info' | ''``."""
+ """Approval-request display data. ``command_preview`` / ``cwd`` are set for exec approvals, ``tool_name``
+ for plugin approvals; ``severity`` is ``'critical' | 'info' | ''``."""
session_key: str
title: str
description: str = ""
@@ -172,8 +155,6 @@ def build_approval_text(req: ApprovalRequest) -> str:
return "\n".join(lines)
-# ── INTERACTION_CREATE event shape ──
-
@dataclass
class InteractionEvent:
"""Parsed ``INTERACTION_CREATE`` payload (api-v2 event-emit docs)."""
@@ -207,9 +188,7 @@ def parse_interaction_event(raw: Dict[str, Any]) -> InteractionEvent:
return InteractionEvent(
id=str(raw.get("id", "")), type=int(data_raw.get("type", 0) or 0), chat_type=scene_code,
scene=_SCENE_NAMES.get(scene_code, ""), group_openid=str(raw.get("group_openid", "")),
- group_member_openid=str(raw.get("group_member_openid", "")),
- user_openid=str(raw.get("user_openid", "")), channel_id=str(raw.get("channel_id", "")),
- guild_id=str(raw.get("guild_id", "")), button_data=str(resolved.get("button_data", "")),
- button_id=str(resolved.get("button_id", "")),
- resolver_user_id=str(resolved.get("user_id", "")),
- )
+ group_member_openid=str(raw.get("group_member_openid", "")), user_openid=str(raw.get("user_openid", "")),
+ channel_id=str(raw.get("channel_id", "")), guild_id=str(raw.get("guild_id", "")),
+ button_data=str(resolved.get("button_data", "")), button_id=str(resolved.get("button_id", "")),
+ resolver_user_id=str(resolved.get("user_id", "")))
diff --git a/gateway/platforms/qqbot/onboard.py b/gateway/platforms/qqbot/onboard.py
index 0215d41b0bd..44a0b917b5c 100644
--- a/gateway/platforms/qqbot/onboard.py
+++ b/gateway/platforms/qqbot/onboard.py
@@ -1,6 +1,5 @@
-"""QQBot scan-to-configure (QR code onboard). Mirrors the Feishu pattern: synchronous
-HTTP + one entry-point ``qr_register()`` (create task → display QR → poll → decrypt
-credentials) against ``q.qq.com`` ``create_bind_task`` / ``poll_bind_result``."""
+"""QQBot scan-to-configure (QR code onboard). Mirrors the Feishu pattern: synchronous HTTP + one entry-point
+``qr_register()`` (create task → display QR → poll → decrypt) against ``q.qq.com`` bind-task endpoints."""
from __future__ import annotations
@@ -11,8 +10,7 @@ from typing import Optional, Tuple
from urllib.parse import quote
from .constants import (
- ONBOARD_API_TIMEOUT, ONBOARD_CREATE_PATH, ONBOARD_POLL_INTERVAL, ONBOARD_POLL_PATH, PORTAL_HOST, QR_URL_TEMPLATE,
-)
+ ONBOARD_API_TIMEOUT, ONBOARD_CREATE_PATH, ONBOARD_POLL_INTERVAL, ONBOARD_POLL_PATH, PORTAL_HOST, QR_URL_TEMPLATE)
from .crypto import decrypt_secret, generate_bind_key
from .utils import get_api_headers
@@ -71,7 +69,8 @@ def _create_bind_task(timeout: float = ONBOARD_API_TIMEOUT) -> Tuple[str, str]:
def _poll_bind_result(task_id: str, timeout: float = ONBOARD_API_TIMEOUT) -> Tuple[BindStatus, str, str, str]:
"""Poll *task_id*; returns ``(status, bot_appid, bot_encrypt_secret, user_openid)``."""
d = _portal_post(ONBOARD_POLL_PATH, {"task_id": task_id}, timeout, "poll_bind_result failed").get("data", {})
- return BindStatus(d.get("status", 0)), str(d.get("bot_appid", "")), d.get("bot_encrypt_secret", ""), d.get("user_openid", "")
+ return (BindStatus(d.get("status", 0)), str(d.get("bot_appid", "")), d.get("bot_encrypt_secret", ""),
+ d.get("user_openid", ""))
def build_connect_url(task_id: str) -> str:
@@ -86,14 +85,12 @@ def qr_register(timeout_seconds: int = 600) -> Optional[dict]:
"""Run the QR registration flow; returns ``{"app_id", "client_secret", "user_openid"}``
or None on failure / expiry / cancellation. Unexpected errors propagate."""
deadline = time.monotonic() + timeout_seconds
-
for refresh_count in range(_MAX_REFRESHES + 1):
try:
task_id, aes_key = _create_bind_task()
except Exception as exc:
logger.warning("[QQBot onboard] Failed to create bind task: %s", exc)
return None
-
url = build_connect_url(task_id)
print()
if _render_qr(url):
@@ -102,14 +99,12 @@ def qr_register(timeout_seconds: int = 600) -> Optional[dict]:
print(f" Open this URL in QQ on your phone:\n {url}")
print(" Tip: pip install qrcode to display a scannable QR code here")
print()
-
while time.monotonic() < deadline:
try:
status, app_id, encrypted_secret, user_openid = _poll_bind_result(task_id)
except Exception:
time.sleep(ONBOARD_POLL_INTERVAL)
continue
-
if status == BindStatus.COMPLETED:
client_secret = decrypt_secret(encrypted_secret, aes_key)
print()
@@ -117,17 +112,14 @@ def qr_register(timeout_seconds: int = 600) -> Optional[dict]:
if user_openid:
print(f" Scanner's OpenID: {user_openid}")
return {"app_id": app_id, "client_secret": client_secret, "user_openid": user_openid}
-
if status == BindStatus.EXPIRED:
if refresh_count >= _MAX_REFRESHES:
logger.warning("[QQBot onboard] QR code expired %d times — giving up", _MAX_REFRESHES)
return None
print(f"\n QR code expired, refreshing... ({refresh_count + 1}/{_MAX_REFRESHES})")
break # next for-loop iteration creates a new task
-
time.sleep(ONBOARD_POLL_INTERVAL)
else:
logger.warning("[QQBot onboard] Poll timed out after %ds", timeout_seconds)
return None
-
return None