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

This commit is contained in:
Davte 2020-08-23 14:03:42 +02:00
parent e29bb08b7b
commit 3e19ba4c81
Signed by: Davte
GPG Key ID: 209AE674A0007425
2 changed files with 10 additions and 11 deletions

View File

@ -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"

View File

@ -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: