fixed bug with breaking of config reading

This commit is contained in:
bvn13 2024-07-11 22:38:30 +03:00
parent b3aeae64e9
commit d2a5c97035
3 changed files with 36 additions and 5 deletions

View File

@ -10,6 +10,17 @@
"type": "forward", "type": "forward",
"chatId": -1002184916546 "chatId": -1002184916546
} }
},
{
"name": "Варениковская сп - погода",
"filter": {
"type": "contain",
"value": "ЦГМС"
},
"action": {
"type": "forward",
"chatId": -1002184916546
}
} }
] ]
} }

View File

@ -60,7 +60,7 @@ class Bot():
return return
chat_id = event.chat_id chat_id = event.chat_id
message = event.message message = event.message
logger.debug("Received message in chat {0}: {1}".format(chat_id, message.text if not message == None else "<No Text>")) logger.info("Received message in chat {0}, id {1}: {2}".format(chat_id, message.id, message.text if not message == None else "<No Text>"))
await self.__process_message(event, chat_id, message) await self.__process_message(event, chat_id, message)
def start(self) -> None: def start(self) -> None:
@ -80,47 +80,67 @@ class Bot():
async def __process_message(self, event, chat_id, message): async def __process_message(self, event, chat_id, message):
configs = self.__config.get_on_message_for_chat_id(chat_id) configs = self.__config.get_on_message_for_chat_id(chat_id)
if configs == None: if configs == None:
logger.info("(chat_id, msg_id)=({0}, {1}): Config is None".format(chat_id, message.id))
return return
if not isinstance(configs, list): if not isinstance(configs, list):
logger.info("(chat_id, msg_id)=({0}, {1}): Config is not list".format(chat_id, message.id))
return return
i = 0
for config in configs: for config in configs:
i = i + 1
filter = config['filter'] if 'filter' in config else None filter = config['filter'] if 'filter' in config else None
action = config['action'] if 'action' in config else None action = config['action'] if 'action' in config else None
action_type = action['type'] if 'type' in action else None action_type = action['type'] if 'type' in action else None
if action == None or action_type == 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: 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): 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': 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 destination = action['chatId'] if 'chatId' in action else None
if destination == 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) 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: async def __needs_skip(self, filter: dict, message) -> bool:
filter_type = filter['type'] if 'type' in filter else None filter_type = filter['type'] if 'type' in filter else None
filter_value = filter['value'] if 'value' in filter else None filter_value = filter['value'] if 'value' in filter else None
if filter_type == None or filter_value == None: if filter_type == None or filter_value == None:
logger.info("msg_id={0}: Filter is bad".format(message.id))
return True return True
text = str(message.text) text = str(message.text)
if text == None: if text == None:
logger.info("msg_id={0}: Text is None".format(message.id))
if not message.reply_to == None: 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) reply = await self.__get_reply(message.chat_id, message)
return await self.__needs_skip(filter, reply) return await self.__needs_skip(filter, reply)
return True return True
if filter_type == 'contain': if filter_type == 'contain':
logger.info("msg_id={0}: Filter type is 'contain'".format(message.id))
if text.find(filter_value) == -1: 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: 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) reply = await self.__get_reply(message.chat_id, message)
return await self.__needs_skip(filter, reply) return await self.__needs_skip(filter, reply)
return True return True
elif filter_type == 'regexp': elif filter_type == 'regexp':
logger.info("msg_id={0}: Filter type is 'regexp'".format(message.id))
if not re.match(filter_value, text): 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: 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) reply = await self.__get_reply(message.chat_id, message)
return await self.__needs_skip(filter, reply) return await self.__needs_skip(filter, reply)
return True return True
logger.info("msg_id={0}: Message to be processed".format(message.id))
return False return False
async def __get_reply(self, chat_id, message): async def __get_reply(self, chat_id, message):

View File

@ -1 +1 @@
0.1.1 0.1.2