stage
This commit is contained in:
102
bot/bot.py
102
bot/bot.py
@ -1,42 +1,18 @@
|
||||
from telebot.async_telebot import AsyncTeleBot
|
||||
from telebot.types import Message
|
||||
from telebot.types import Message, InputFile
|
||||
import asyncio
|
||||
from secret import TOKEN
|
||||
import textbook
|
||||
import re
|
||||
from urllib.parse import urlparse, urlunparse
|
||||
import random
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from db.engine import engine
|
||||
from db import models
|
||||
from msgprocessor import TrackerRemovalMsgProcessor, TrackerRemovalProcessorMessage
|
||||
|
||||
HAS_LINK_RE = r"(https?:\/\/[^\s]+|www\.[^\s]+)"
|
||||
from femboygenerator import FemboyGeneratorMsgProcessor, FemboyGeneratorMessage
|
||||
|
||||
bot = AsyncTeleBot(TOKEN)
|
||||
|
||||
|
||||
def extract_links(text: str):
|
||||
url_pattern = r"(https?://[^\s]+|www\.[^\s]+)"
|
||||
links = re.findall(url_pattern, text)
|
||||
return links
|
||||
|
||||
|
||||
def process_text(text: str) -> str | None:
|
||||
links = extract_links(text)
|
||||
updated_links = []
|
||||
for link in links:
|
||||
if any(keyword in link for keyword in ("youtu.be", "youtube.com")):
|
||||
parsed_link = urlparse(link)
|
||||
if "si=" in parsed_link.query:
|
||||
query_arr = parsed_link.query.split("&")
|
||||
new_query_arr = [ele for ele in query_arr if "si=" not in ele]
|
||||
new_query = "&".join(new_query_arr)
|
||||
updated_link = parsed_link._replace(query=new_query)
|
||||
updated_links.append(urlunparse(updated_link))
|
||||
if updated_links:
|
||||
return random.choice(textbook.reactions).format(link="\n".join(updated_links))
|
||||
return None
|
||||
|
||||
|
||||
@bot.message_handler(commands=["start"])
|
||||
async def start(msg: Message):
|
||||
if msg.chat.type == "private":
|
||||
@ -57,20 +33,66 @@ async def got_message(msg: Message):
|
||||
TrackerRemovalProcessorMessage(fromUser=msg.from_user, text=msg.text)
|
||||
).process()
|
||||
|
||||
if not tracker_removal_result.needsToReply:
|
||||
return
|
||||
femboy_generator_result = FemboyGeneratorMsgProcessor(msg=msg).process()
|
||||
|
||||
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(",
|
||||
if tracker_removal_result.needsToReply:
|
||||
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: логгер
|
||||
|
||||
await bot.send_message(
|
||||
msg.chat.id, tracker_removal_result.text, parse_mode="html"
|
||||
)
|
||||
print(e, flush=True) # todo: логгер
|
||||
return
|
||||
|
||||
await bot.send_message(msg.chat.id, tracker_removal_result.text, parse_mode="html")
|
||||
if femboy_generator_result.send:
|
||||
with Session(engine) as db:
|
||||
upload = (
|
||||
db.query(models.TelegramUpload)
|
||||
.filter(
|
||||
models.TelegramUpload.path == femboy_generator_result.media_path
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if upload:
|
||||
if femboy_generator_result.media_type == "gif":
|
||||
await bot.send_animation(
|
||||
reply_to_message_id=femboy_generator_result.message.id,
|
||||
chat_id=femboy_generator_result.message.id,
|
||||
animation=upload.file_id,
|
||||
)
|
||||
elif femboy_generator_result.media_type == "pic":
|
||||
await bot.send_photo(
|
||||
reply_to_message_id=femboy_generator_result.message.id,
|
||||
chat_id=femboy_generator_result.message.id,
|
||||
photo=upload.file_id,
|
||||
)
|
||||
else:
|
||||
if femboy_generator_result.media_type == "gif":
|
||||
reply_msg = await bot.send_animation(
|
||||
reply_to_message_id=femboy_generator_result.message.id,
|
||||
chat_id=femboy_generator_result.message.id,
|
||||
animation=InputFile(femboy_generator_result.media_path),
|
||||
)
|
||||
elif femboy_generator_result.media_type == "pic":
|
||||
await bot.send_photo(
|
||||
reply_to_message_id=femboy_generator_result.message.id,
|
||||
chat_id=femboy_generator_result.message.id,
|
||||
photo=InputFile(femboy_generator_result.media_path),
|
||||
)
|
||||
file_id = reply_msg.animation.file_id
|
||||
new_upload = models.TelegramUpload(
|
||||
path=femboy_generator_result.media_path,
|
||||
file_id=file_id,
|
||||
type=femboy_generator_result.media_type,
|
||||
)
|
||||
db.add(new_upload)
|
||||
db.commit()
|
||||
|
||||
|
||||
async def main():
|
||||
|
||||
Reference in New Issue
Block a user