extract links, delete incoming msg, add sender to reply
This commit is contained in:
33
bot/bot.py
33
bot/bot.py
@ -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():
|
||||
|
||||
Reference in New Issue
Block a user