From 1a9a2d7fe81b32fddd6ec5c1eaf167caded3f528 Mon Sep 17 00:00:00 2001 From: Nish Date: Wed, 8 Apr 2026 12:05:24 +0530 Subject: [PATCH] fix(gateway/telegram): fall back to chat.id when from_user is None in DMs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `message.from_user` is None — which can happen for forwarded messages, anonymous admin mode in groups, or certain Telegram client edge cases — `_build_message_event` set `source.user_id` to None. This caused: 1. `_is_user_authorized()` to early-return False (`if not user_id: return False`) 2. The access check never compared against `TELEGRAM_ALLOWED_USERS` even when the user actually was in the allowlist 3. The pairing flow fired and generated a code for `user_id=None` 4. The pairing approval saved an entry under the literal string key "null" 5. The user was effectively locked out because their real user_id never matched the "null" key on subsequent messages For DMs (`chat_type == "dm"`), Telegram guarantees `chat.id == user.id` — they are the same numeric ID for private chats. Falling back to `chat.id` when `from_user` is None for DMs restores the expected access-control behavior without weakening it (group/channel chats correctly stay None). Also adds a parallel `user_name` fallback to `chat.full_name` so the display name still works in the same edge case. --- gateway/platforms/telegram.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gateway/platforms/telegram.py b/gateway/platforms/telegram.py index 8df05268..f7161405 100644 --- a/gateway/platforms/telegram.py +++ b/gateway/platforms/telegram.py @@ -2926,8 +2926,8 @@ class TelegramAdapter(BasePlatformAdapter): chat_id=str(chat.id), chat_name=chat.title or (chat.full_name if hasattr(chat, "full_name") else None), chat_type=chat_type, - user_id=str(user.id) if user else None, - user_name=user.full_name if user else None, + user_id=str(user.id) if user else (str(chat.id) if chat_type == "dm" else None), + user_name=user.full_name if user else (chat.full_name if hasattr(chat, "full_name") and chat_type == "dm" else None), thread_id=thread_id_str, chat_topic=chat_topic, )