extract links, delete incoming msg, add sender to reply

This commit is contained in:
Ghytro
2024-11-07 15:49:07 +03:00
parent d453168457
commit 7e78811724
4 changed files with 162 additions and 4 deletions

View File

@ -5,6 +5,10 @@ from secret import TOKEN
import textbook
import re
from urllib.parse import urlparse, urlunparse
from msgprocessor import (
TrackerRemovalMsgProcessor,
TrackerRemovalProcessorMessage
)
import random
HAS_LINK_RE = r'(https?:\/\/[^\s]+|www\.[^\s]+)'
@ -45,10 +49,31 @@ async def start(msg: Message):
@bot.message_handler(func=lambda message: True)
async def got_message(msg: Message):
if re.match(string=msg.text, pattern=HAS_LINK_RE):
fixed_reply = process_text(msg.text)
if fixed_reply:
await bot.reply_to(msg, fixed_reply)
# god i love nones as fuck
if msg.text is None:
return
if msg.from_user is None:
return
if msg.from_user.username is None:
return
tracker_removal_result = TrackerRemovalMsgProcessor(
TrackerRemovalProcessorMessage(
fromUsername=msg.from_user.username,
text=msg.text
)
).process()
if not tracker_removal_result.needsToReply:
return
try:
await bot.delete_message(msg.chat.id, msg.id, 5)
except Exception as e:
print(e) # todo: логгер
return
await bot.send_message(msg.chat.id, tracker_removal_result.text)
async def main():