From 472da17a7ddf3ddb8d9c568c7333aa76a7f6268a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 1 Apr 2019 11:20:13 +0200 Subject: [PATCH] Add error handler to TelegramBot --- royalnet/bots/telegram.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index fabae1f5..cdd91cb4 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -24,11 +24,13 @@ class TelegramBot: master_server_secret: str, commands: typing.List[typing.Type[Command]], database_uri: str, - missing_command: Command = NullCommand): + missing_command: Command = NullCommand, + error_command: Command = NullCommand): self.bot: telegram.Bot = telegram.Bot(api_key) self.should_run: bool = False self.offset: int = -100 self.missing_command = missing_command + self.error_command = error_command self.network: RoyalnetLink = RoyalnetLink(master_server_uri, master_server_secret, "telegram", todo) # Generate commands self.commands = {} @@ -90,7 +92,10 @@ class TelegramBot: # Skip inexistent commands command = self.missing_command # Call the command - return await self.Call(message.chat, command, *parameters, update=update).run() + try: + return await self.Call(message.chat, command, *parameters, update=update).run() + except Exception as exc: + return await self.Call(message.chat, self.error_command, *parameters, update=update).run() async def handle_net_request(self, message: Message): pass