extract links, delete incoming msg, add sender to reply
This commit is contained in:
34
bot/bot.py
34
bot/bot.py
@ -7,13 +7,15 @@ import re
|
||||
from urllib.parse import urlparse, urlunparse
|
||||
import random
|
||||
|
||||
HAS_LINK_RE = r'(https?:\/\/[^\s]+|www\.[^\s]+)'
|
||||
from msgprocessor import TrackerRemovalMsgProcessor, TrackerRemovalProcessorMessage
|
||||
|
||||
HAS_LINK_RE = r"(https?:\/\/[^\s]+|www\.[^\s]+)"
|
||||
|
||||
bot = AsyncTeleBot(TOKEN)
|
||||
|
||||
|
||||
def extract_links(text: str):
|
||||
url_pattern = r'(https?://[^\s]+|www\.[^\s]+)'
|
||||
url_pattern = r"(https?://[^\s]+|www\.[^\s]+)"
|
||||
links = re.findall(url_pattern, text)
|
||||
return links
|
||||
|
||||
@ -45,10 +47,30 @@ 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
|
||||
|
||||
tracker_removal_result = TrackerRemovalMsgProcessor(
|
||||
TrackerRemovalProcessorMessage(fromUser=msg.from_user, text=msg.text)
|
||||
).process()
|
||||
|
||||
if not tracker_removal_result.needsToReply:
|
||||
return
|
||||
|
||||
try:
|
||||
await bot.delete_message(msg.chat.id, msg.id, timeout=5)
|
||||
except Exception as e:
|
||||
await bot.reply_to(
|
||||
message=msg.id,
|
||||
text="Uoghhhh, i am not an admin here? I can't cleanup this tracking(",
|
||||
)
|
||||
print(e, flush=True) # todo: логгер
|
||||
return
|
||||
|
||||
await bot.send_message(msg.chat.id, tracker_removal_result.text, parse_mode="html")
|
||||
|
||||
|
||||
async def main():
|
||||
|
||||
Reference in New Issue
Block a user