From d2a5c97035704be46548e45f9d2958846f6c31fa Mon Sep 17 00:00:00 2001 From: bvn13 Date: Thu, 11 Jul 2024 22:38:30 +0300 Subject: [PATCH] fixed bug with breaking of config reading --- config/newsmaker.json | 11 +++++++++++ src/bot/bot.py | 28 ++++++++++++++++++++++++---- version | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/config/newsmaker.json b/config/newsmaker.json index 1ccb3eb..898947e 100644 --- a/config/newsmaker.json +++ b/config/newsmaker.json @@ -10,6 +10,17 @@ "type": "forward", "chatId": -1002184916546 } + }, + { + "name": "Варениковская сп - погода", + "filter": { + "type": "contain", + "value": "ЦГМС" + }, + "action": { + "type": "forward", + "chatId": -1002184916546 + } } ] } diff --git a/src/bot/bot.py b/src/bot/bot.py index 7b5bc47..2b1fda4 100644 --- a/src/bot/bot.py +++ b/src/bot/bot.py @@ -60,7 +60,7 @@ class Bot(): return chat_id = event.chat_id message = event.message - logger.debug("Received message in chat {0}: {1}".format(chat_id, message.text if not message == None else "")) + logger.info("Received message in chat {0}, id {1}: {2}".format(chat_id, message.id, message.text if not message == None else "")) await self.__process_message(event, chat_id, message) def start(self) -> None: @@ -80,47 +80,67 @@ class Bot(): async def __process_message(self, event, chat_id, message): configs = self.__config.get_on_message_for_chat_id(chat_id) if configs == None: + logger.info("(chat_id, msg_id)=({0}, {1}): Config is None".format(chat_id, message.id)) return if not isinstance(configs, list): + logger.info("(chat_id, msg_id)=({0}, {1}): Config is not list".format(chat_id, message.id)) return + i = 0 for config in configs: + i = i + 1 filter = config['filter'] if 'filter' in config else None action = config['action'] if 'action' in config else None action_type = action['type'] if 'type' in action else None if action == None or action_type == None: - return + logger.info("(chat_id, msg_id, cfg_id)=({0}, {1}, {2}): Action is bad".format(chat_id, message.id, i)) + continue if not filter == None: + logger.info("(chat_id, msg_id, cfg_id)=({0}, {1}, {2}): Filter is set".format(chat_id, message.id, i)) if await self.__needs_skip(filter, message): - return + logger.info("(chat_id, msg_id, cfg_id)=({0}, {1}, {2}): Skipping".format(chat_id, message.id, i)) + continue if action_type == 'forward': + logger.info("(chat_id, msg_id, cfg_id)=({0}, {1}, {2}): Action type is 'forward'".format(chat_id, message.id, i)) destination = action['chatId'] if 'chatId' in action else None if destination == None: - return + logger.info("(chat_id, msg_id, cfg_id)=({0}, {1}, {2}): Destination is None".format(chat_id, message.id, i)) + continue await event.forward_to(destination) + logger.info("(chat_id, msg_id, cfg_id)=({0}, {1}, {2}): Forwarded".format(chat_id, message.id, i)) async def __needs_skip(self, filter: dict, message) -> bool: filter_type = filter['type'] if 'type' in filter else None filter_value = filter['value'] if 'value' in filter else None if filter_type == None or filter_value == None: + logger.info("msg_id={0}: Filter is bad".format(message.id)) return True text = str(message.text) if text == None: + logger.info("msg_id={0}: Text is None".format(message.id)) if not message.reply_to == None: + logger.info("msg_id={0}: Reply is present".format(message.id)) reply = await self.__get_reply(message.chat_id, message) return await self.__needs_skip(filter, reply) return True if filter_type == 'contain': + logger.info("msg_id={0}: Filter type is 'contain'".format(message.id)) if text.find(filter_value) == -1: + logger.info("msg_id={0}: Text '{1}' not found".format(message.id, filter_value)) if not message.reply_to == None: + logger.info("msg_id={0}: Reply is present".format(message.id)) reply = await self.__get_reply(message.chat_id, message) return await self.__needs_skip(filter, reply) return True elif filter_type == 'regexp': + logger.info("msg_id={0}: Filter type is 'regexp'".format(message.id)) if not re.match(filter_value, text): + logger.info("msg_id={0}: Text '{1}' not found".format(message.id, filter_value)) if not message.reply_to == None: + logger.info("msg_id={0}: Reply is present".format(message.id)) reply = await self.__get_reply(message.chat_id, message) return await self.__needs_skip(filter, reply) return True + logger.info("msg_id={0}: Message to be processed".format(message.id)) return False async def __get_reply(self, chat_id, message): diff --git a/version b/version index 6da28dd..8294c18 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.1.1 \ No newline at end of file +0.1.2 \ No newline at end of file