Queer European MD passionate about IT
Browse Source

In text message handler, make available both lowered and original text and inspect condition parameters

Davte 3 years ago
parent
commit
59dde0d25e
2 changed files with 10 additions and 11 deletions
  1. 1 1
      davtelepot/__init__.py
  2. 9 10
      davtelepot/bot.py

+ 1 - 1
davtelepot/__init__.py

@@ -11,7 +11,7 @@ __author__ = "Davide Testa"
 __email__ = "davide@davte.it"
 __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
 __license__ = "GNU General Public License v3.0"
-__version__ = "2.6.12"
+__version__ = "2.6.13"
 __maintainer__ = "Davide Testa"
 __contact__ = "t.me/davte"
 

+ 9 - 10
davtelepot/bot.py

@@ -807,7 +807,8 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
     async def text_message_handler(self, update, user_record, language=None):
         """Handle `text` message update."""
         replier, reply = None, None
-        text = update['text'].lower()
+        text = update['text']
+        lowered_text = text.lower()
         user_id = update['from']['id'] if 'from' in update else None
         if user_id in self.individual_text_message_handlers:
             replier = self.individual_text_message_handlers[user_id]
@@ -818,7 +819,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
             # Commands can use latin letters, numbers and underscores.
             command = re.search(
                 r"([A-z_1-9]){1,32}",  # Command pattern (without leading `/`)
-                text
+                lowered_text
             ).group(0)  # Get the first group of characters matching pattern
             if command in self.commands:
                 replier = self.commands[command]['handler']
@@ -840,18 +841,16 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
         else:  # Handle command aliases and text parsers
             # Aliases are case insensitive: text and alias are both .lower()
             for alias, function in self.command_aliases.items():
-                if text.startswith(alias.lower()):
+                if lowered_text.startswith(alias.lower()):
                     replier = function
                     break
             # Text message update parsers
             for check_function, parser in self.text_message_parsers.items():
-                if (
-                    parser['argument'] == 'text'
-                    and check_function(text)
-                ) or (
-                    parser['argument'] == 'update'
-                    and check_function(update)
-                ):
+                if check_function(
+                        **{name: argument
+                           for name, argument in locals().items()
+                           if name in inspect.signature(
+                                check_function).parameters}):
                     replier = parser['handler']
                     break
         if replier: