From 3aacd0dceffee6b0a783d378d9f396e47c2c0bc2 Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Fri, 7 Feb 2025 01:03:49 +0300 Subject: [PATCH] Refactor channel username and ID extraction logic --- post_parser.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/post_parser.py b/post_parser.py index 5145b7a..91185bb 100644 --- a/post_parser.py +++ b/post_parser.py @@ -534,13 +534,16 @@ class PostParser: """Extract channel username or ID from message""" chat = message.chat if hasattr(message, 'chat') else message if not chat: return None - if isinstance(chat.id, int) and str(chat.id).startswith('-100'): return str(chat.id) # Return full ID for channels with numeric ID + + # First try to get username if hasattr(chat, 'usernames') and chat.usernames: # Check many usernames active_usernames = [u.username for u in chat.usernames if u.active] if active_usernames: return active_usernames[0] if hasattr(chat, 'username') and chat.username: return chat.username # Check single username - if isinstance(chat.id, int) and str(chat.id).startswith('-100'): return str(chat.id) # Return full ID if no username found + + # Return numeric ID only if no username found + if isinstance(chat.id, int) and str(chat.id).startswith('-100'): return str(chat.id) logger.error(f"channel_username_error: no username or valid ID found for chat") return None