From f16ed8235c430e1391aed541ac6cd5cd0b5a2d95 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 24 Dec 2018 17:32:34 +0100 Subject: [PATCH] Add new is_bot method to check if the logged-in user is a bot --- telethon/client/auth.py | 2 ++ telethon/client/telegrambaseclient.py | 1 + telethon/client/users.py | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/telethon/client/auth.py b/telethon/client/auth.py index f310f5d3..94062d03 100644 --- a/telethon/client/auth.py +++ b/telethon/client/auth.py @@ -351,6 +351,7 @@ class AuthMethods(MessageParseMethods, UserMethods): Returns the input user parameter. """ + self._bot = bool(user.bot) self._self_input_peer = utils.get_input_peer(user, allow_self=False) self._authorized = True @@ -413,6 +414,7 @@ class AuthMethods(MessageParseMethods, UserMethods): except errors.RPCError: return False + self._bot = None self._self_input_peer = None self._authorized = False self._state = types.updates.State(0, 0, datetime.datetime.now(), 0, 0) diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index 0f111f9a..d048c492 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -289,6 +289,7 @@ class TelegramBaseClient(abc.ABC): # Sometimes we need to know who we are, cache the self peer self._self_input_peer = None + self._bot = None # endregion diff --git a/telethon/client/users.py b/telethon/client/users.py index 18c9c9d1..42581f98 100644 --- a/telethon/client/users.py +++ b/telethon/client/users.py @@ -110,6 +110,7 @@ class UserMethods(TelegramBaseClient): me = (await self( functions.users.GetUsersRequest([types.InputUserSelf()])))[0] + self._bot = me.bot if not self._self_input_peer: self._self_input_peer = utils.get_input_peer( me, allow_self=False @@ -119,6 +120,15 @@ class UserMethods(TelegramBaseClient): except errors.UnauthorizedError: return None + async def is_bot(self): + """ + Return ``True`` if the signed-in user is a bot, ``False`` otherwise. + """ + if self._bot is None: + self._bot = (await self.get_me()).bot + + return self._bot + async def is_user_authorized(self): """ Returns ``True`` if the user is authorized.