Repository navigation
fix(telegram): recover when a group becomes a supergroup - #319
Hansen1018 wants to merge 1 commit into
Conversation
TNT-Likely
left a comment
There was a problem hiding this comment.
@Hansen1018 感谢提交!这个问题场景很真实,分支改动也非常干净。Code review 时发现 2 处逻辑缺陷,麻烦修复后再合:
-
迁移成功后 Onboarding 校验指纹会失配(
src/modules/administration/api/channels.py)identity = channel_fingerprint(channel)是在_persist_telegram_migration(db, channel, notifier)之前计算的;迁移把新chat_id写回channel.config后,record_check(db, "channel", channel.id, identity, True)写入的仍然是旧chat_id的指纹。- 后续 onboarding 调用
verified()比对最新channel_fingerprint(channel)时会判定为False(未验证)。建议在_persist_telegram_migration返回新chat_id后重新计算identity,并补充一条验证/api/onboarding中verified == True的回归断言。
-
重发新
chat_id失败时,“请把 chat_id 改为 ...”的提示在真实场景不会命中(src/platform/notifications/notifier.py)- 当前在
allow_chat_migration=False的第二次请求里是通过if migrate_to:拼装提示的;但第二次请求发往的已经是新的超级群chat_id,真实 Telegram 返回 403/400(如 bot 被移出群或被拉黑)时不会再携带parameters.migrate_to_chat_id(单测里是因为 mock 在第二次 403 响应里也额外塞了migrate_to_chat_id才通过的)。 - 建议在第一次触发重试的调用外层捕获
RuntimeError,把;请把 chat_id 改为 {new_chat_id}附加到重试异常信息中,并同步把单测里第二次 mock 响应的parameters.migrate_to_chat_id去掉以还原真实 Telegram 响应。
- 当前在
另外,修复完成后麻烦顺带用仓内的 .agents/skills/panwatch-local-delivery 跑一轮隔离验收,并将脱敏后的离线测试报告 ZIP 附到 PR 正文里,方便我们直接核对合并。谢谢!
0ad09d1 to
a210ced
Compare
PanWatch contribution checkThe mechanical PR requirements are satisfied. Product acceptance, evidence quality and code review remain maintainer decisions. Scope: 2 production files, 106 added + deleted production lines. Tests, Markdown docs and known lockfiles are excluded. 本检查只核对材料与改动规模,不代表产品或实现已经验收。首次提示后有14天整改窗口;第7天提醒,逾期仍未达标自动关闭。维护者可用 pr:hold 暂停;关闭后仍可重开或提交拆分版本。 |
|
Rebased onto the latest
Validation re-run on the new base: The three failures are the pre-existing, date-dependent ones in For reference, the CI status of this pull request after the rebase:
That failure was addressed by #316 (keep the review API cases inside the 90-day window), which was closed on 2026-12-01. If it is still wanted, it can be reopened — the fix only replaces the pinned date with a date relative to today, so the cases keep asserting the same window behaviour and stop expiring. Until then the The Telegram behaviour itself was verified against the live Bot API on the previous base and is unchanged by the rebase: a group that was upgraded to a supergroup answers the old |
|
@Hansen1018 看到你已经把分支 rebase 到最新 提醒一下:刚才复查
麻烦把这 2 处一并修复,并跑一轮隔离验收附上报告后再通知我们合入,谢谢! |
a210ced to
715345f
Compare
Telegram invalidates the chat id when a group is upgraded to a supergroup: sending to the old id fails with "Bad Request: group chat was upgraded to a supergroup chat" and the new id is only returned in the error payload. The channel test then reported a bare "test notification failed", so the real cause was invisible. Retry once with the migrate_to_chat_id Telegram provides, report the new id when the retry fails as well, and store the migrated id in the channel configuration so the user does not have to change it by hand. Telegram now always uses PanWatch's own sender instead of Apprise: only our own request can read the Telegram error payload, so the migration and the real failure reason are reachable. Through Apprise a failing channel only ever reported "notification failed". Two follow-up defects in that path are fixed here as well: - The channel test recorded its verification result with the fingerprint of the channel as it was before the migration, so onboarding compared a new fingerprint (the chat id changed) and reported the channel as unverified. The fingerprint is recomputed after the migrated chat id is stored. - The retry already addresses the new supergroup, so a failing retry no longer carries migrate_to_chat_id in the Telegram response and the "please change chat_id" hint was never produced. The retry error is caught in the call that triggered it and the new id is appended there.
715345f to
7d7a44b
Compare
|
Both review findings are fixed, and the isolated acceptance run surfaced a third one that the unit tests could not see — thank you for the review, it made the change much stronger. 1. Onboarding fingerprint after the migration — fixed
migrated_chat_id = _persist_telegram_migration(db, channel, notifier)
if migrated_chat_id:
# 指纹包含渠道配置:迁移后 chat_id 变了,必须用新指纹记录校验结果,
# 否则 onboarding 的 verified() 会拿旧指纹比对,误判为“未验证”。
identity = channel_fingerprint(channel)Regression test added: the onboarding endpoint has to report 2. The "please change chat_id" hint — fixedThe retry now runs inside the call that triggered it, and that call catches the retry error and appends the new id: try:
return await self._send_telegram(
{**config, "chat_id": new_chat_id}, title, content,
allow_chat_migration=False,
)
except Exception as exc:
# 重发请求发往的已经是新的超级群,真实响应不再带 migrate_to_chat_id,
# 所以在这里补上新 ID。
raise RuntimeError(f"{exc};请把 chat_id 改为 {new_chat_id}") from excThe unit test now mocks the second response without 3. Found by the isolated acceptance run: Telegram never used our senderRunning the real flow in an isolated instance ( Apprise does not expose the Telegram response, so neither the migration nor the real reason can be read from it. Telegram therefore always uses PanWatch's own client now: if channel_type == "telegram":
...
# Telegram 一律走自定义实现:只有自己发请求才能读到 Telegram 原始错误,
# 从而处理群组升级为超级群后的 chat_id 迁移,并把真实失败原因告知用户。
# 走 Apprise 时只会得到“发送失败”,迁移与排障都无从谈起。
return NoneCovered by a new test ( Isolated acceptance (real Telegram Bot API)Private QA copy of the workspace — own The message was really delivered to the authorized group; the server log shows the migration and both successful sends: Test report
Identities: source Branch stateRebased onto the current Ready for another look — thanks for catching the two defects. |
Background
A Telegram channel that used to work stopped delivering, and the channel test only reported "测试通知发送失败" — the real reason was discarded.
The cause is a Telegram platform change, not a configuration mistake: when a group is upgraded to a supergroup, the old
chat_idis invalidated. Telegram answers the old id with{"ok": false, "error_code": 400, "description": "Bad Request: group chat was upgraded to a supergroup chat", "parameters": {"migrate_to_chat_id": -1006031292438}}and the new id is only delivered inside the error payload. Two problems follow:
chat_id, so the channel cannot be repaired without reading the server logs.Reproduced with a real bot and a real group:
Changes
src/platform/notifications/notifier.py— when the Telegram API returnsparameters.migrate_to_chat_id, retry the send once with that id, as Telegram asks clients to do. The mapping is recorded inNotifierManager.telegram_migrations. If the retry fails as well, the error names the new id so the configuration can be fixed.src/platform/notifications/notifier.py— Telegram is now always sent by PanWatch's own client instead of Apprise. Only our own request can read the Telegram error payload, so the migration and the real failure reason are reachable; through Apprise a failing channel only ever reported "notification failed".src/modules/administration/api/channels.py— the channel test reports the underlying reason instead of a bare failure, and writes the migratedchat_idback into the channel configuration, telling the user which id is now in use. The verification fingerprint is recomputed after the write, so onboarding still reports the channel as verified.tests/test_telegram_delivery.py,tests/test_onboarding.py— regression tests for the retry, for the actionable error, for the configuration update and for the onboarding verdict.Scope
One outcome: a channel keeps working after its Telegram group is upgraded to a supergroup, and a failing test tells the user what to fix.
For a working channel the visible difference is the rendering: the title is sent as a bold line, the body is escaped and truncated at 3900 characters. The proxy option, the sanitising, the quiet-hours policy and the message limit are unchanged.
Excluded: no change to the other notification channels, to the notification policy, to the group/topic support, or to any API field.
Validation
Tested commit:
7d7a44bCommands and results:
The full suite is green on the current
main: the date-dependenttests/test_evaluations_api.pyfailures that made every pull request red were fixed by #322, which is merged.Isolated acceptance (real Telegram Bot API)
Run in a private QA copy of the workspace (own
DATA_DIR, own frontend build,127.0.0.1only, the running instance untouched) with the repository skill.agents/skills/panwatch-local-delivery:The message was really delivered to the authorized group; the server log shows the migration and both successful sends:
Tested source
7d7a44b; source content hash6f376157ac179bc2594a657b77bf53f91a5637cce9096bf43a4e024babae7b18; frontend build hashf8866f517e9e3fbe0a3ed1e2c972611bdfb5fd8e0117152e77c51f6017834fa0; QA run1df50909-0319-4479-bf00-44973417eb4d.Test report
Report attachment pending: the REST API exposes no endpoint for uploading a ZIP to a pull request and no browser file-picker tool is available in this session, so the sanitized report could not be uploaded here. The complete package is kept locally and its content is reproduced in the review comment.
panwatch-pr319-acceptance-report.zip(8,025 bytes)43f4b5b59aa92594e5cd7a07aa98e6cc331f1c78d1ab46a0c0cdfcbbfa6737efreport/index.html(offline, self-contained),report/OPEN.md,report/evidence/*.json,report/manifest.json,report/SHA256SUMSsha256sum -c SHA256SUMSpasses for every file, and no token or credential is present in the packageAcceptance evidence
Steps:
chat_id.Expected:
Observed:
Evidence: the new regression tests, the isolated acceptance run quoted above, and the sanitized report package described in the Validation section.
Boundaries and risks
Follow-up
None.