From 553125721f3b0cbbdf4309a3fa405872e8dfa076 Mon Sep 17 00:00:00 2001 From: afolivieri Date: Tue, 23 Apr 2024 13:59:15 +0300 Subject: [PATCH] added message before the forwarded one with the keyword matched and the context of the match --- streaming_overseer.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/streaming_overseer.py b/streaming_overseer.py index 4c91468..ecabe56 100644 --- a/streaming_overseer.py +++ b/streaming_overseer.py @@ -93,9 +93,16 @@ async def main(): @client.on(events.NewMessage(chats=channels)) async def handler(event): message_content = event.message.message if event.message else "" - for pattern in word_patterns.values(): - if regex.search(pattern, message_content): + for word, pattern in word_patterns.items(): + match = regex.search(pattern, message_content) + if match: + start_pos = max(match.start() - 20, 0) + end_pos = min(match.end() + 20, len(message_content)) + context = message_content[start_pos:end_pos] + await client.send_message(channel_id, f"Keyword Match: {word}\nContext: {context}") + await asyncio.sleep(0.1) await event.message.forward_to(channel_id) + await asyncio.sleep(0.1) print(f'Forwarded Message: {message_content}') break