From 783040bcd00a2699cc38bca6cd80bad7b6440b20 Mon Sep 17 00:00:00 2001 From: Davte Date: Mon, 1 Jul 2019 20:20:12 +0200 Subject: [PATCH] Provide a chat_id getter and a method to change it. --- davtelepot/bot.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/davtelepot/bot.py b/davtelepot/bot.py index fefb69c..8e3ec18 100644 --- a/davtelepot/bot.py +++ b/davtelepot/bot.py @@ -112,6 +112,14 @@ class Bot(TelegramBot): self._under_maintenance = False self._allowed_during_maintenance = [] self._maintenance_message = None + # Default chat_id getter: same chat as update + self.get_chat_id = lambda update: ( + update['message']['chat']['id'] + if 'message' in update and 'chat' in update['message'] + else update['chat']['id'] + if 'chat' in update + else None + ) # Message to be returned if user is not allowed to call method self._authorization_denied_message = None # Default authorization function (always return True) @@ -664,6 +672,15 @@ class Bot(TelegramBot): """ self.authorization_function = authorization_function + def set_chat_id_getter(self, getter): + """Set chat_id getter. + + It must be a function that takes an update and returns the proper + chat_id. + """ + assert callable(getter), "Chat id getter must be a function!" + self.get_chat_id = getter + @staticmethod def get_identifier_from_update_or_user_id(user_id=None, update=None): """Get telegram id of user given an update.