Queer European MD passionate about IT
Преглед на файлове

Provide a chat_id getter and a method to change it.

Davte преди 5 години
родител
ревизия
783040bcd0
променени са 1 файла, в които са добавени 17 реда и са изтрити 0 реда
  1. 17 0
      davtelepot/bot.py

+ 17 - 0
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.