-
Notifications
You must be signed in to change notification settings - Fork 0
Sending automatically messages #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import aiogram.types as types | ||
|
||
# bot/fallbacks.py | ||
from bot.handlers import * | ||
from states import DialogStates | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# bot/scheduler.py | ||
from datetime import time | ||
from aiogram import Bot | ||
from apscheduler.schedulers.asyncio import AsyncIOScheduler | ||
from apscheduler.triggers.cron import CronTrigger | ||
from messages import send_morning_message, send_evening_message | ||
from service.user import UserService | ||
|
||
scheduler: AsyncIOScheduler = AsyncIOScheduler() | ||
|
||
def initialize_scheduler(): | ||
scheduler.start() | ||
return scheduler | ||
|
||
def update_user_job(user_id: int, when: time, bot: Bot, job_type: str): | ||
job_id = f"{job_type}_message_{user_id}" | ||
|
||
if scheduler.get_job(job_id): | ||
scheduler.remove_job(job_id) | ||
|
||
scheduler.add_job( | ||
send_morning_message if job_type == "wake" else send_evening_message, | ||
trigger=CronTrigger(hour=when.hour, minute=when.minute), | ||
args=[bot, user_id], | ||
id=job_id, | ||
replace_existing=True | ||
) | ||
|
||
print(f"[INFO] - User with id: {user_id} updated job, {job_id}") | ||
|
||
async def schedule_all_users_jobs(bot: Bot): | ||
users = await UserService.get_all_users() | ||
for user in users: | ||
if user["wake_time"]: | ||
update_user_job(user["id"], user["wake_time"], bot, job_type="wake") | ||
if user["sleep_time"]: | ||
update_user_job(user["id"], user["sleep_time"], bot, job_type="sleep") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# bot/utills.py | ||
from datetime import datetime, timedelta | ||
|
||
def format_date(dt: datetime) -> str: | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,7 @@ | ||||||||||||||||||||||||||||
from typing import Any | ||||||||||||||||||||||||||||
from aiogram import types, Bot | ||||||||||||||||||||||||||||
from service.user import UserService | ||||||||||||||||||||||||||||
from service.routine import RoutineService | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
MESSAGES: Any = { | ||||||||||||||||||||||||||||
"UKRANIAN": { | ||||||||||||||||||||||||||||
|
@@ -96,6 +99,8 @@ | |||||||||||||||||||||||||||
"ROUTINE_TIME": "⏰ Прокидаєшся о {}, лягаєш спати о {}, загальний час дня: {}. Чудовий план! 😊", | ||||||||||||||||||||||||||||
"TIMER_INVALID": "❌ Неправильний формат часу (потрібно 10:00). Спробуй ще раз! 😌", | ||||||||||||||||||||||||||||
"IDEA_EXIST": "⚠️ Ідея з такою назвою вже є. Придумай нову, ти ж креативний! 😊", | ||||||||||||||||||||||||||||
"SEND_MORNING_MSG": "Доброго ранку, {}", | ||||||||||||||||||||||||||||
"SEND_EVENING_MSG": "Доброго вечора, {}", | ||||||||||||||||||||||||||||
"LANGUAGE_ASK": ( | ||||||||||||||||||||||||||||
"🌐 Яку мову обереш, друже? \n" | ||||||||||||||||||||||||||||
"Тисни кнопку нижче, і поїхали! 😄" | ||||||||||||||||||||||||||||
|
@@ -199,6 +204,8 @@ | |||||||||||||||||||||||||||
"TIMER_INVALID": "❌ Wrong time format (use 10:00). Try again! 😌", | ||||||||||||||||||||||||||||
"ROUTINE_TIME": "⏰ Wake up at {}, sleep at {}, total day time: {}. Great plan! 😊", | ||||||||||||||||||||||||||||
"IDEA_EXIST": "⚠️ An idea with that name already exists. Got another creative one? 😊", | ||||||||||||||||||||||||||||
"SEND_MORNING_MSG": "Good morning, {}!", | ||||||||||||||||||||||||||||
"SEND_EVENING_MSG": "Good evening, {}!", | ||||||||||||||||||||||||||||
"LANGUAGE_ASK": ( | ||||||||||||||||||||||||||||
"🌐 What language would you like, friend? \n" | ||||||||||||||||||||||||||||
"Pick one below, and let’s roll! 😄" | ||||||||||||||||||||||||||||
|
@@ -209,10 +216,10 @@ | |||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Buttons | ||||||||||||||||||||||||||||
BUTTON_SETTINGS = "⚙️ Settings" | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The BUTTON_SETTINGS variable is being redefined. The original definition on line 222 is being moved, but this creates inconsistency in the code structure. Consider maintaining the original order or using a single definition. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||
BUTTON_ADD_TASK: str = "📝 Add a Task" | ||||||||||||||||||||||||||||
BUTTON_IDEA: str = "💾 Save an Idea" | ||||||||||||||||||||||||||||
BUTTON_MYDAY: str = "📅 My Day" | ||||||||||||||||||||||||||||
BUTTON_SETTINGS: str = "⚙️ Settings" | ||||||||||||||||||||||||||||
BUTTON_HELP: str = "❓ Help" | ||||||||||||||||||||||||||||
BUTTON_UA_LANG: str = "🇺🇦 Українська" | ||||||||||||||||||||||||||||
BUTTON_EN_LANG: str = "🇬🇧 English" | ||||||||||||||||||||||||||||
|
@@ -284,4 +291,38 @@ def generate_daily_stats_message(language: str, created_ideas: int, completed_ta | |||||||||||||||||||||||||||
f"📝 *Tasks added*: {created_tasks}\n\n" | ||||||||||||||||||||||||||||
"🔄 Updates every day at 00:00.\n\n" | ||||||||||||||||||||||||||||
"You’re absolutely crushing it! Keep shining! 🌟" | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
async def send_morning_message(bot: Bot, user_id: int): | ||||||||||||||||||||||||||||
language = await UserService.get_user_language(user_id) or "ENGLISH" | ||||||||||||||||||||||||||||
morning_routine = await RoutineService.get_user_routines(user_id, routine_type="morning") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
print(f"[INFO] - Sending morning routine to user with id, {user_id}") | ||||||||||||||||||||||||||||
if not morning_routine: | ||||||||||||||||||||||||||||
await bot.send_message( | ||||||||||||||||||||||||||||
user_id, | ||||||||||||||||||||||||||||
MESSAGES[language]['SEND_MORNING_MSG'].format("👤") + '\n' + MESSAGES[language]['NO_MORNING_ROUTINE'] | ||||||||||||||||||||||||||||
Comment on lines
+299
to
+304
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The hardcoded "👤" emoji should be replaced with a more meaningful placeholder or the actual username, as suggested by the message template parameter structure.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
dividers: str = "\n" + ("-" * int(len(MESSAGES[language]['MORNING_ROUTINE_SHOW']) * 1.65)) | ||||||||||||||||||||||||||||
formatted_routine_items = "\n".join( | ||||||||||||||||||||||||||||
f"# {idx}. {routine['routine_name']}" | ||||||||||||||||||||||||||||
for idx, routine in enumerate(morning_routine, start=1) | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
formatted_morning_routine = ( | ||||||||||||||||||||||||||||
MESSAGES[language]['MORNING_ROUTINE_SHOW'] + | ||||||||||||||||||||||||||||
dividers + | ||||||||||||||||||||||||||||
"\n" + | ||||||||||||||||||||||||||||
formatted_routine_items | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
await bot.send_message(user_id, formatted_morning_routine) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
async def send_evening_message(bot: Bot, user_id: int): | ||||||||||||||||||||||||||||
language = await UserService.get_user_language(user_id) or "ENGLISH" | ||||||||||||||||||||||||||||
print(f"[INFO] - Sending evening routine to user with id, {user_id}") | ||||||||||||||||||||||||||||
await bot.send_message( | ||||||||||||||||||||||||||||
user_id, | ||||||||||||||||||||||||||||
MESSAGES[language]['SEND_EVENING_MSG'].format("👤") | ||||||||||||||||||||||||||||
Comment on lines
+325
to
+327
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the morning message, the hardcoded "👤" emoji should be replaced with a more meaningful placeholder or the actual username.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import is redundant since line 6 already imports everything from messages with 'from messages import *'. Remove this duplicate import.
Copilot uses AI. Check for mistakes.