new notify

This commit is contained in:
Administrator 2023-03-30 00:53:29 +03:00
parent 7b28762c14
commit b11574c188
5 changed files with 104 additions and 5 deletions

View File

@ -54,7 +54,8 @@ def fetch_schedule_for_user(user: User):
),
"building": element['building'],
"lecturer": element['lecturer'],
"notified": False
"notified": False,
"notified_today": False
})
saved_ids.append(result.inserted_id)
else:

View File

@ -59,6 +59,40 @@ def process():
{"$set": {"next_daily_notify_time": user_model.next_daily_notify_time + datetime.timedelta(days=1)}}
)
for user in mongo.users_collection.find({"first_lesson_notify": {"$ne": None}, "hse_id": {"$ne": None}}):
time_now = now(UserSchema().load(user))
for lesson in mongo.lessons_collection.find({
"hse_user_id": user["hse_id"],
"begin": {"$lte": time_now + datetime.timedelta(minutes=user["first_lesson_notify"])},
"notified_today": {"$ne": True}
}).sort("begin"):
ans = f"📅 {lesson['begin'].strftime('%d.%m')}\n"
ans += f"📚 {lesson['discipline']}\n"
ans += f"🏢 {lesson['building']}, {lesson['auditorium']}\n"
ans += f"🕑 {lesson['begin'].strftime('%H:%M')} - {lesson['end'].strftime('%H:%M')}\n"
ans += f"🧑‍🏫 {(lesson['lecturer'] or 'Неизвестно')}\n"
if lesson.get('link', None):
ans += f"🔗 {lesson['link']}"
try:
mess = "Пары начутся через "
if user['first_lesson_notify'] == 30:
mess += "30 минут"
elif user['first_lesson_notify'] == 60:
mess += "1 час"
elif user['first_lesson_notify'] == 4 * 60:
mess += "4 часа"
else:
mess += "12 часов"
mess += "!\n\nТвоя первая пара:\n\n" + ans
bot.send_message(
user["chat_id"],
mess
)
except ApiTelegramException:
pass
mongo.lessons_collection.update_many({"begin": {"$gte": time_now.date(), "$lt": (time_now + datetime.timedelta(days=1)).date()}}, {"$set": {"notified_today": True}})
break
def notify():
while True:

View File

@ -4,7 +4,7 @@ from daemons.bot import bot
from daemons.fetch import fetch_schedule_for_user
from helpers import get_next_daily_notify_time
from helpers.keyboards import main_keyboard, notify_keyboard, yes_no_keyboard, again_keyboard, groups_keyboard, \
no_daily_notify, student_or_teacher_keyboard, campus_keyboard, daily_notify_type
no_daily_notify, student_or_teacher_keyboard, campus_keyboard, daily_notify_type, notify_type, first_lesson_notify
from helpers.models import UserSchema, User
from helpers.mongo import mongo
from helpers.ruz import ruz
@ -188,10 +188,10 @@ class Answer(BaseAnswer):
elif message.text == "Уведомления о парах":
bot.send_message(
user.chat_id,
"Выбери за сколько минут мне нужно напомнить тебе о предстоящей паре",
reply_markup=notify_keyboard()
"Я умею уведомлять о каждой паре и о первой паре. Что хочешь настроить?",
reply_markup=notify_type()
)
self.set_state(user, "wait_for_notify")
self.set_state(user, "notify_type")
return
elif message.text == "Ежедневные уведомления":
bot.send_message(
@ -213,6 +213,51 @@ class Answer(BaseAnswer):
reply_markup=main_keyboard()
)
def handle_state_notify_type(self, message: Message, user: User):
text = message.text
if text == "О каждой паре":
bot.send_message(
user.chat_id,
"Выбери за сколько минут мне нужно напомнить тебе о предстоящей паре",
reply_markup=notify_keyboard()
)
self.set_state(user, "wait_for_notify")
elif text == "О первой паре":
bot.send_message(
user.chat_id,
"Выбери за сколько минут мне нужно напоминать тебе о первой паре",
reply_markup=first_lesson_notify()
)
self.set_state(user, "wait_for_first_notify")
elif text == "Назад":
self.set_state(user, "ready")
bot.send_message(
user.chat_id,
text,
reply_markup=main_keyboard()
)
else:
bot.send_message(user.chat_id, "Используй кнопки!", reply_markup=notify_type())
def handle_state_wait_for_first_notify(self, message: Message, user: User):
text = message.text
if text == "30 минут":
time_notify = 30
elif text == "1 час":
time_notify = 60
elif text == "4 часа":
time_notify = 4 * 60
elif text == "12 часов":
time_notify = 12 * 60
elif text == "Не уведомлять":
time_notify = None
else:
bot.send_message(user.chat_id, "Используй кнопки!", reply_markup=first_lesson_notify())
return
mongo.users_collection.update_one({"chat_id": user.chat_id}, {"$set": {"first_lesson_notify": time_notify}})
self.set_state(user, "ready")
bot.send_message(user.chat_id, "Запомнил!", reply_markup=main_keyboard())
def handle_state_wait_for_daily_notify_type(self, message: Message, user: User):
text = message.text
if text == "Текущий день":

View File

@ -70,3 +70,21 @@ def daily_notify_type():
kb.row("Следующий день")
kb.row("Не уведомлять")
return kb
def notify_type():
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("О каждой паре")
kb.row("О первой паре")
kb.row("Назад")
return kb
def first_lesson_notify():
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("30 минут")
kb.row("1 час")
kb.row("4 часа")
kb.row("12 часов")
kb.row("Не уведомлять")
return kb

View File

@ -19,6 +19,7 @@ class User:
is_teacher: bool = False
campus: str = "Москва"
daily_notify_today: bool = True
first_lesson_notify: Optional[float] = None
class Meta:
unknown = EXCLUDE