fix(telegram): normalize reply sender_id to self_id for bot replies#8237
Open
woleigedouer wants to merge 3 commits into
Open
fix(telegram): normalize reply sender_id to self_id for bot replies#8237woleigedouer wants to merge 3 commits into
woleigedouer wants to merge 3 commits into
Conversation
Telegram adapter stored Reply.sender_id as numeric user ID while self_id is the bot username, causing WakingCheckStage to never match replies to bot messages in groups.
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In the tests,
_find_reply_componentusesnext(...)without a default, which will raise an opaqueStopIterationif theReplycomponent is missing; consider asserting or using a safer helper that fails with a clearer message. - When normalizing
reply_sender_id, you assign eitherstr(reply_abm.sender.user_id)ormessage.self_id, which may represent different identifier formats (numeric ID vs username); it would be helpful to make the intended invariant forsender_idexplicit in code (e.g., via a short comment) to avoid future confusion.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In the tests, `_find_reply_component` uses `next(...)` without a default, which will raise an opaque `StopIteration` if the `Reply` component is missing; consider asserting or using a safer helper that fails with a clearer message.
- When normalizing `reply_sender_id`, you assign either `str(reply_abm.sender.user_id)` or `message.self_id`, which may represent different identifier formats (numeric ID vs username); it would be helpful to make the intended invariant for `sender_id` explicit in code (e.g., via a short comment) to avoid future confusion.
## Individual Comments
### Comment 1
<location path="tests/test_telegram_adapter.py" line_range="145-150" />
<code_context>
+ reply_to_message=reply_to_message,
+ )
+
+ result = await adapter.convert_message(update, _build_context())
+
+ assert result is not None
+ reply = _find_reply_component(result)
+ assert reply.sender_id == "87654321"
+ assert reply.qq == 87654321
+
+
</code_context>
<issue_to_address>
**suggestion (testing):** Consider asserting more of the Reply payload (id and chain) to ensure reply metadata is preserved
The tests currently verify `sender_id` and `qq`, but not that reply metadata like `id` and `chain` still match `reply_to_message`. Please also assert something like `reply.id == reply_to_message.message_id` and add a minimal check on `reply.chain` (e.g., non-empty or contains the original text) to ensure normalization doesn’t inadvertently alter these fields.
```suggestion
result = await adapter.convert_message(update, _build_context())
assert result is not None
reply = _find_reply_component(result)
assert reply.sender_id == "87654321"
assert reply.qq == 87654321
# reply metadata should be preserved
assert reply.id == reply_to_message.message_id
assert reply.chain
# ensure the reply chain still carries the original reply text
if getattr(reply_to_message, "text", None):
assert any(
getattr(component, "text", None) == reply_to_message.text
for component in reply.chain
)
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the Telegram adapter to normalize the sender ID when a message is a reply to the bot, ensuring it matches the bot's self_id. Corresponding unit tests were added to verify this behavior for both bot and user replies. Feedback was provided to remove a redundant string conversion for the sender ID to improve code readability.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Telegram adapter stored
Reply.sender_idas numeric user ID whileself_idis the bot username, causingWakingCheckStageto never match replies to bot messages in groups.When the
/wake prefix is removed, replying to a bot message in a Telegram group should still wake the bot through the genericReplywake check. Before this change, Telegram replies only worked in some text-message cases because of the existing/@botfallback path, not becauseReply.sender_idmatchedevent.get_self_id().Modifications / 改动点
Reply.sender_idtomessage.self_idonly when the replied message was sent by the bot.message.self_idas the Telegram bot username to avoid affecting@bot_usernamewake behavior./@botfallback for compatibility.Screenshots or Test Results / 运行截图或测试结果
Manual verification:
/fromwake_prefix./is removed./back makes the old fallback path work again, confirming the previous behavior depended on the/@bottext fallback instead of the genericReplywake check.Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Normalize Telegram reply metadata so replies to bot messages use the bot self ID while preserving existing behavior for user replies.
Bug Fixes:
Tests: