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",
"chatId": -1002184916546
}
},
{
"name": "Варениковская сп - погода",
"filter": {
"type": "contain",
"value": "ЦГМС"
},
"action": {
"type": "forward",
"chatId": -1002184916546
}
}
]
}

View File

@ -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 "<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)
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):

View File

@ -1 +1 @@
0.1.1
0.1.2