From 30ebe756ac98010380a83114d84525ae968f0086 Mon Sep 17 00:00:00 2001 From: _run Date: Tue, 30 Jul 2024 15:48:26 +0500 Subject: [PATCH] make extract_bot_id return None in case validation fails --- telebot/__init__.py | 4 ++-- telebot/async_telebot.py | 4 ++-- telebot/util.py | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 74c08a4b8..fc7f3cdc3 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -191,11 +191,11 @@ def __init__( self.allow_sending_without_reply = allow_sending_without_reply self.webhook_listener = None self._user = None - self.bot_id: int = None if validate_token: util.validate_token(self.token) - self.bot_id = util.extract_bot_id(self.token) # subject to change in future, unspecified + + self.bot_id: Union[int, None] = util.extract_bot_id(self.token) # subject to change in future, unspecified # logs-related if colorful_logs: diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 5302000ff..ec782ff91 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -189,11 +189,11 @@ def __init__(self, token: str, parse_mode: Optional[str]=None, offset: Optional[ self.middlewares = [] self._user = None # set during polling - self.bot_id: int = None if validate_token: util.validate_token(self.token) - self.bot_id: int = util.extract_bot_id(self.token) # subject to change, unspecified + + self.bot_id: Union[int, None] = util.extract_bot_id(self.token) # subject to change, unspecified @property diff --git a/telebot/util.py b/telebot/util.py index c8ef526c5..0448893e1 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -698,7 +698,11 @@ def validate_token(token) -> bool: return True -def extract_bot_id(token) -> str: +def extract_bot_id(token) -> Union[int, None]: + try: + validate_token(token) + except ValueError: + return None return int(token.split(':')[0])