Queer European MD passionate about IT
瀏覽代碼

`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.
Davte 5 年之前
父節點
當前提交
41f578cf21
共有 1 個文件被更改,包括 12 次插入4 次删除
  1. 12 4
      davtelepot/bot.py

+ 12 - 4
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,