From b68d9326946c23c9c3723e5fb421e506dc6395e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C5=82gorzata=20Kufel?= Date: Fri, 25 Apr 2025 13:18:08 +0200 Subject: [PATCH] first version of praniebot, fixes required --- .idea/misc.xml | 3 ++ main.py | 122 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/.idea/misc.xml b/.idea/misc.xml index 30f3709..1e5e126 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/main.py b/main.py index e69de29..bb59d3e 100644 --- a/main.py +++ b/main.py @@ -0,0 +1,122 @@ +import re +from datetime import datetime, timedelta +import pytz +from apscheduler.schedulers.asyncio import AsyncIOScheduler +from telegram import Update, User +from telegram.ext import Application, CommandHandler, ContextTypes + +TOKEN = "7769574344:AAH_90MxtZt-BpJraJbV5-qkD-z2x-LY3F4" # wiem, że zahardkodowane, ale w dupie mam elo +reminders = {} + +# TODO: fix max delay 24h +class CustomContext(ContextTypes.DEFAULT_TYPE): + def __init__(self, application, chat_id=None, user_id=None): + super().__init__(application=application) + self.chat_id = chat_id + self.user_id = user_id + if 'scheduler' not in application.bot_data: + application.bot_data['scheduler'] = AsyncIOScheduler(timezone="Europe/Warsaw") + application.bot_data['scheduler'].start() + self.scheduler = application.bot_data['scheduler'] + + +async def schedule_reminder(context: CustomContext, chat_id: int, user: User, delay: timedelta): + max_delay = timedelta(hours=24) + if delay > max_delay: + await context.bot.send_message( + chat_id=chat_id, + text="maksymalnie 24 degeneracie ty", + parse_mode="HTML" + ) + return + + now = datetime.now(pytz.timezone("Europe/Warsaw")) + reminder_time = now + delay + job_id = f"pranie_{chat_id}_{reminder_time.timestamp()}" + + async def send_reminder(): + await context.bot.send_message( + chat_id=chat_id, + text=f"🔔 PRANIE!!! PRANIE KURWA ROZWIEŚ TEJ @{user.username}", + parse_mode="HTML" + ) + + context.scheduler.add_job( + send_reminder, + trigger="date", + run_date=reminder_time, + id=job_id, + replace_existing=True, + ) + + if chat_id not in reminders: + reminders[chat_id] = [] + reminders[chat_id].append((job_id, reminder_time.strftime("%H:%M"), user.id)) + + +async def pranie(update: Update, context: CustomContext): + chat_id = update.effective_chat.id + user = update.effective_user + username = user.first_name + + text = " ".join(context.args) + + if not text: + await update.message.reply_text("usage: /pranie HH:MM, /pranie Xmin/Xh") + return + + if re.match(r"^\d{1,2}:\d{2}$", text): + hour, minute = map(int, text.split(":")) + now = datetime.now(pytz.timezone("Europe/Warsaw")) + reminder_time = now.replace(hour=hour, minute=minute, second=0, microsecond=0) + if reminder_time < now: + reminder_time += timedelta(days=1) + delay = reminder_time - now + elif re.match(r"^\d+\s*(m|min|minutes?|h|hours?)$", text): + num, unit = re.findall(r"(\d+)\s*(\w+)", text)[0] + if "h" in unit: + delay = timedelta(hours=int(num)) + else: + delay = timedelta(minutes=int(num)) + else: + await update.message.reply_text("zły format, /pranie HH:MM albo /pranie [X min/hours]") + return + + await schedule_reminder(context, chat_id, user, delay) + await update.message.reply_text(f"przypominaja nastawiona, {username}") + +async def praniehelp(update: Update, context: CustomContext): + help_text = ( + "🧺 *PRANIEBOT - komendy:*\n" + "/pranie HH:MM - przypominajka na godzinę\n" + " _example: /pranie 14:30_\n\n" + "/pranie Xmin/Xh - przypominajka po czasie\n" + " _example: /pranie 30min_\n" + " _example: /pranie 2h_\n\n" + "/praniehelp - niestety nie rozwiesi prania" + ) + await update.message.reply_text(help_text, parse_mode="Markdown") + + +async def start(update: Update): + await update.message.reply_text( + "/pranie HH:MM albo /pranie Xmin/Xh do ustawienia przypominajki\n" + "/praniehelp niestety nie rozwiesi prania" + ) + + +def main(): + context_types = ContextTypes(context=CustomContext) + application = Application.builder().token(TOKEN).context_types(context_types).build() + + application.add_handler(CommandHandler("start", start)) + application.add_handler(CommandHandler("pranie", pranie)) + application.add_handler(CommandHandler("praniehelp", praniehelp)) + + print("zapierdala") + + application.run_polling() + + +if __name__ == "__main__": + main()