Skip to content

Commit

Permalink
make extract_bot_id return None in case validation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
coder2020official committed Jul 30, 2024
1 parent 6108e35 commit 30ebe75
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions telebot/async_telebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion telebot/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])


Expand Down

0 comments on commit 30ebe75

Please sign in to comment.