From 1663acd1a1d3fc3b03be2943ded0c8ff5f1c4598 Mon Sep 17 00:00:00 2001 From: codeLzq <1293680370@qq.com> Date: Mon, 2 Feb 2026 14:36:24 +0800 Subject: [PATCH] feat: enhance sender ID handling in Telegram channel - Update sender ID construction to prioritize user ID while maintaining username for allowlist compatibility. - Improve allowlist checking in BaseChannel to support sender IDs with multiple parts separated by '|'. --- nanobot/channels/base.py | 9 ++++++++- nanobot/channels/telegram.py | 6 ++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/nanobot/channels/base.py b/nanobot/channels/base.py index d83367c..8f16399 100644 --- a/nanobot/channels/base.py +++ b/nanobot/channels/base.py @@ -72,7 +72,14 @@ class BaseChannel(ABC): if not allow_list: return True - return str(sender_id) in allow_list + sender_str = str(sender_id) + if sender_str in allow_list: + return True + if "|" in sender_str: + for part in sender_str.split("|"): + if part and part in allow_list: + return True + return False async def _handle_message( self, diff --git a/nanobot/channels/telegram.py b/nanobot/channels/telegram.py index 840c250..ac2dba4 100644 --- a/nanobot/channels/telegram.py +++ b/nanobot/channels/telegram.py @@ -199,8 +199,10 @@ class TelegramChannel(BaseChannel): user = update.effective_user chat_id = message.chat_id - # Get sender identifier (prefer username, fallback to user_id) - sender_id = str(user.username or user.id) + # Use stable numeric ID, but keep username for allowlist compatibility + sender_id = str(user.id) + if user.username: + sender_id = f"{sender_id}|{user.username}" # Store chat_id for replies self._chat_ids[sender_id] = chat_id