From f591c8d54a0b5eaedb534a892364be65090d7413 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Tue, 26 Sep 2023 13:00:17 +0300 Subject: [PATCH] core: implement adaptive delays during message processing and improve the control panel --- main.py | 71 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 4dbfc79..ec6ccda 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,9 @@ import traceback # global variables STOP_REQUESTED = False - +DELAY_AFTER_RESPONSE = 3 +DELAY_AFTER_MESSAGE = 0.1 +DELAY_AFTER_IDLE = 1.0 # some functions that increase readability of the code def readfile(filename): @@ -59,7 +61,7 @@ class ModuleV1: 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()") + print(f"[ERROR] module v1: traceback:\n{traceback.format_exc()}") return "", None @@ -166,12 +168,13 @@ def queue_processor(): try: if len(message_queue) > 0: msg = message_queue[0] + del message_queue[0] print("[DEBUG] queue_processor: {}".format(msg)) # debug # check for control commands - if msg["chat"]["id"] == 575246355: + if msg.chat.id == 575246355: if msg["text"][0] == "$": - command = msg["text"][1:].split(" ") + command = msg["text"][1:].split() if len(command) >= 2 and command[0] == "module": if command[1] == "reload": @@ -185,7 +188,49 @@ def queue_processor(): del mcu.modules[:] mcu.reload_modules() - del message_queue[0] + elif (2 <= len(command) <= 3) and command[0] == "delay": + l = len(command) + + if command[1] == "response": + if l == 3: + try: + new_value = float(command[2]) + global DELAY_AFTER_RESPONSE + DELAY_AFTER_RESPONSE = new_value + updater.bot.send_message(msg.chat.id, f"Set DELAY_AFTER_RESPONSE to {command[2]}") + except: + print(f"[WARN]: Cannot set DELAY_AFTER_RESPONSE to non-float value of {command[2]}") + updater.bot.send_message(msg.chat.id, f"[WARN]: Cannot set DELAY_AFTER_RESPONSE to non-float value of {command[2]}") + elif l == 2: + print(f"[INFO]: DELAY_AFTER_RESPONSE = {DELAY_AFTER_RESPONSE}") + updater.bot.send_message(msg.chat.id, f"[INFO]: DELAY_AFTER_RESPONSE = {DELAY_AFTER_RESPONSE}") + + elif command[1] == "message": + if l == 3: + try: + new_value = float(command[2]) + global DELAY_AFTER_MESSAGE + DELAY_AFTER_MESSAGE = new_value + updater.bot.send_message(msg.chat.id, f"Set DELAY_AFTER_MESSAGE to {command[2]}") + except: + print("[WARN]: Cannot set DELAY_AFTER_MESSAGE to non-float value of {command[2]}") + updater.bot.send_message(msg.chat.id, f"[WARN]: Cannot set DELAY_AFTER_MESSAGE to non-float value of {command[2]}") + elif l == 2: + print(f"[INFO]: DELAY_AFTER_MESSAGE = {DELAY_AFTER_MESSAGE}") + updater.bot.send_message(msg.chat.id, f"[INFO]: DELAY_AFTER_MESSAGE = {DELAY_AFTER_MESSAGE}") + + elif command[1] == "idle": + if l == 3: + try: + new_value = float(command[2]) + global DELAY_AFTER_IDLE + DELAY_AFTER_IDLE = new_value + except: + print("[WARN]: Cannot set DELAY_AFTER_IDLE to non-float value of {command[2]}") + updater.bot.send_message(msg.chat.id, f"[WARN]: Cannot set DELAY_AFTER_MESSAGE to non-float value of {command[2]}") + elif l == 2: + print(f"[INFO]: DELAY_AFTER_IDLE = {DELAY_AFTER_IDLE}") + updater.bot.send_message(msg.chat.id, f"[INFO]: DELAY_AFTER_IDLE = {DELAY_AFTER_IDLE}") continue # modules are used in here @@ -199,6 +244,7 @@ def queue_processor(): 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}") + time.sleep(DELAY_AFTER_RESPONSE) break elif formatting in ["Markdown", "MarkdownV2", "HTML"]: @@ -206,25 +252,20 @@ def queue_processor(): disable_web_page_preview=True, parse_mode=formatting) print(f"Responded using module {mod.path} ({mod.alias}) with text (using {formatting}): {response}") + time.sleep(DELAY_AFTER_RESPONSE) break - del message_queue[0] - - time.sleep(0.1) + time.sleep(DELAY_AFTER_MESSAGE) else: if STOP_REQUESTED: break else: - time.sleep(1) + time.sleep(DELAY_AFTER_IDLE) except Exception as e: print(f"[ERROR] queue_processor: current message queue: {message_queue}") print(f"[ERROR] Traceback:\n{traceback.format_exc()}") - print(f"[ERROR] queue_processor: error while processing message ({e}), trying to delete it...") - try: - del message_queue[0] - print("[INFO] queue_processor: deleted broken message from the queue") - except: - print("[WARN] queue_processor: message seems absent, whatever") + print(f"[ERROR] queue_processor: error while processing message ({e})...") + print("[INFO] queue_processor: skipped broken message") print("[INFO] queue_processor thread stops successfully")