From 41f578cf21cc86fd0fd06e362d82de28b6e05ca7 Mon Sep 17 00:00:00 2001 From: Davte Date: Thu, 18 Jul 2019 09:56:40 +0200 Subject: [PATCH] `reply` general method defined: calls proper output method Call reply with an update and some keyword arguments, it will use the proper method according to kwargs to send the output. --- davtelepot/bot.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/davtelepot/bot.py b/davtelepot/bot.py index 0f0c9f4..b443370 100644 --- a/davtelepot/bot.py +++ b/davtelepot/bot.py @@ -643,10 +643,7 @@ class Bot(TelegramBot, ObjectWithDatabase): if type(reply) is str: reply = dict(text=reply) try: - if 'text' in reply: - return await self.send_message(update=update, **reply) - if 'photo' in reply: - return await self.send_photo(update=update, **reply) + return await self.reply(update=update, **reply) except Exception as e: logging.error( f"Failed to handle text message:\n{e}", @@ -901,6 +898,17 @@ class Bot(TelegramBot, ObjectWithDatabase): yield (prefix + text_chunk + suffix), is_last return + async def reply(self, update=None, *args, **kwargs): + """Reply to `update` with proper method according to `kwargs`.""" + method = None + if 'text' in kwargs: + method = self.send_message + elif 'photo' in kwargs: + method = self.send_photo + if method is not None: + return await method(update=update, *args, **kwargs) + raise Exception("Unsopported keyword arguments for `Bot().reply`.") + async def send_message(self, chat_id=None, text=None, parse_mode='HTML', disable_web_page_preview=None,