add support for parse_mode selection in all module APIs and update modules to respect these changes

This commit is contained in:
2023-09-06 17:22:26 +03:00
parent 2b9ac41ced
commit 0497cbf9b7
5 changed files with 91 additions and 45 deletions
+17 -15
View File
@@ -40,6 +40,7 @@ class ModuleV1:
# set environmental variables
def set_env(self):
self.RESPONSE = ""
self.FORMAT = ""
def set_predefine(self):
try:
@@ -55,11 +56,11 @@ class ModuleV1:
self.MESSAGE = msg
try:
exec(self.code)
return self.RESPONSE
return self.RESPONSE, self.FORMAT
except Exception as e:
print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"")
print(f"[ERROR] module v1: traceback:\ntraceback.format_exc()")
return ""
return "", None
class ModuleV2:
@@ -78,7 +79,7 @@ class ModuleV2:
except Exception as e:
print(f"[ERROR] module v2: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"")
print(f"[ERROR] module v2: traceback:\ntraceback.format_exc()")
return None
return None, None
# module control unit
@@ -190,21 +191,22 @@ def queue_processor():
# modules are used in here
for mod in mcu.modules:
if mod.enabled:
if mod.version == 1 or mod.version == 2:
response = mod.process(msg)
if mod.version in [1, 2]:
response, formatting = mod.process(msg)
if response:
'''
# protecting output
symbols_to_escape = ['[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!']
for symbol in symbols_to_escape:
response = response.replace(symbol, f"\\{symbol}")
'''
if formatting == None:
updater.bot.send_message(chat_id=msg.chat.id, text=response,
disable_web_page_preview=True)
print(f"Responded using module {mod.path} ({mod.alias}) with text: {response}")
break
updater.bot.send_message(chat_id=msg.chat.id, text=response,
disable_web_page_preview=True)
print(f"Responded using module {mod.path} ({mod.alias}) with text: {response}")
break
elif formatting in ["Markdown", "MarkdownV2", "HTML"]:
updater.bot.send_message(chat_id=msg.chat.id, text=response,
disable_web_page_preview=True,
parse_mode=formatting)
print(f"Responded using module {mod.path} ({mod.alias}) with text (using {formatting}): {response}")
break
del message_queue[0]