Handle unknown commands
This commit is contained in:
parent
631bda4465
commit
7ed430b264
@ -34,6 +34,7 @@ class Bot(TelegramBot):
|
|||||||
_maintenance_message = ("I am currently under maintenance!\n"
|
_maintenance_message = ("I am currently under maintenance!\n"
|
||||||
"Please retry later...")
|
"Please retry later...")
|
||||||
_authorization_denied_message = None
|
_authorization_denied_message = None
|
||||||
|
_unknown_command_message = None
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, token, hostname='', certificate=None, max_connections=40,
|
self, token, hostname='', certificate=None, max_connections=40,
|
||||||
@ -110,6 +111,8 @@ class Bot(TelegramBot):
|
|||||||
'passport_data': self.passport_data_message_handler
|
'passport_data': self.passport_data_message_handler
|
||||||
}
|
}
|
||||||
self.individual_text_message_handlers = dict()
|
self.individual_text_message_handlers = dict()
|
||||||
|
self.commands = dict()
|
||||||
|
self._unknown_command_message = None
|
||||||
self._under_maintenance = False
|
self._under_maintenance = False
|
||||||
self._allowed_during_maintenance = []
|
self._allowed_during_maintenance = []
|
||||||
self._maintenance_message = None
|
self._maintenance_message = None
|
||||||
@ -256,6 +259,16 @@ class Bot(TelegramBot):
|
|||||||
"""
|
"""
|
||||||
return self._default_keyboard
|
return self._default_keyboard
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unknown_command_message(self):
|
||||||
|
"""Message to be returned if user sends an unknown command.
|
||||||
|
|
||||||
|
If instance message is not set, class message is returned.
|
||||||
|
"""
|
||||||
|
if self._unknown_command_message:
|
||||||
|
return self._unknown_command_message
|
||||||
|
return self.__class__._unknown_command_message
|
||||||
|
|
||||||
async def message_router(self, update):
|
async def message_router(self, update):
|
||||||
"""Route Telegram `message` update to appropriate message handler."""
|
"""Route Telegram `message` update to appropriate message handler."""
|
||||||
for key, value in update.items():
|
for key, value in update.items():
|
||||||
@ -346,20 +359,19 @@ class Bot(TelegramBot):
|
|||||||
if user_id in self.individual_text_message_handlers:
|
if user_id in self.individual_text_message_handlers:
|
||||||
replier = self.individual_text_message_handlers[user_id]
|
replier = self.individual_text_message_handlers[user_id]
|
||||||
del self.individual_text_message_handlers[user_id]
|
del self.individual_text_message_handlers[user_id]
|
||||||
elif text.startswith('/'): # Command handler
|
elif text.startswith('/'): # Handle commands
|
||||||
# A command must always start with the ‘/’ symbol and may not be
|
# A command must always start with the ‘/’ symbol and may not be
|
||||||
# longer than 32 characters.
|
# longer than 32 characters.
|
||||||
# Commands can use latin letters, numbers and underscores.
|
# Commands can use latin letters, numbers and underscores.
|
||||||
print(text)
|
|
||||||
command = re.search(
|
command = re.search(
|
||||||
r"([A-z_1-9]){1,32}",
|
r"([A-z_1-9]){1,32}", # Command pattern (without leading `/`)
|
||||||
text
|
text
|
||||||
).group(0) # Get the first group characters matching pattern
|
).group(0) # Get the first group of characters matching pattern
|
||||||
if command in self.commands:
|
if command in self.commands:
|
||||||
replier = self.commands[command]['function']
|
replier = self.commands[command]['function']
|
||||||
elif update['chat']['id'] > 0:
|
elif update['chat']['id'] > 0:
|
||||||
replier = self.unknown_command_message
|
reply = self.unknown_command_message
|
||||||
else: # Check alias and text parsers
|
else: # Handle command aliases and text parsers
|
||||||
logging.info("#TODO alias and text parsers")
|
logging.info("#TODO alias and text parsers")
|
||||||
if replier:
|
if replier:
|
||||||
if asyncio.iscoroutinefunction(replier):
|
if asyncio.iscoroutinefunction(replier):
|
||||||
@ -748,6 +760,22 @@ class Bot(TelegramBot):
|
|||||||
"""
|
"""
|
||||||
self.authorization_function = authorization_function
|
self.authorization_function = authorization_function
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def set_class_unknown_command_message(cls, unknown_command_message):
|
||||||
|
"""Set class unknown command message.
|
||||||
|
|
||||||
|
It will be returned if user sends an unknown command in private chat.
|
||||||
|
"""
|
||||||
|
cls._unknown_command_message = unknown_command_message
|
||||||
|
|
||||||
|
def set_unknown_command_message(self, unknown_command_message):
|
||||||
|
"""Set instance unknown command message.
|
||||||
|
|
||||||
|
It will be returned if user sends an unknown command in private chat.
|
||||||
|
If instance message is None, default class message is used.
|
||||||
|
"""
|
||||||
|
self._unknown_command_message = unknown_command_message
|
||||||
|
|
||||||
def set_chat_id_getter(self, getter):
|
def set_chat_id_getter(self, getter):
|
||||||
"""Set chat_id getter.
|
"""Set chat_id getter.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user