ruz-bot/daemons/notify.py
2022-10-25 17:42:13 +03:00

65 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import datetime
from time import sleep
from telebot.apihelper import ApiTelegramException
from daemons.bot import bot
from helpers import now, get_next_daily_notify_time
from helpers.models import UserSchema
from helpers.mongo import mongo
from helpers.ruz import ruz
def process():
for user in mongo.users_collection.find({"notify_minutes": {"$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["notify_minutes"])},
"notified": False
}):
ans = ""
ans += f"Дисциплина: {lesson['discipline']}\n"
ans += f"Аудитория: {lesson['building']}, {lesson['auditorium']}\n"
ans += f"Начало: {lesson['begin'].strftime('%H:%M')}\n"
ans += f"Конец: {lesson['end'].strftime('%H:%M')}\n"
ans += f"Преподаватель: {(lesson['lecturer'] or 'Неизвестно')}\n"
try:
bot.send_message(
user["chat_id"],
"Уведомляю о занятиях!\n" + ans
)
except ApiTelegramException:
pass
mongo.lessons_collection.update_one({"_id": lesson['_id']}, {"$set": {"notified": True}})
for user in mongo.users_collection.find({"next_daily_notify_time": {"$lte": time_now}}):
user_model = UserSchema().load(user)
if time_now.weekday() != 6:
lessons = mongo.get_today_lessons(user_model)
if len(lessons) == 0:
text = "Сегодня у тебя нет пар."
else:
text = ruz.schedule_builder(lessons)
try:
bot.send_message(
user["chat_id"],
"Уведомляю о занятиях! Твое расписание на сегодня:\n" + text
)
except:
pass
mongo.users_collection.update_one(
{"chat_id": user["chat_id"]},
{"$set": {"next_daily_notify_time": user_model.next_daily_notify_time + datetime.timedelta(days=1)}}
)
def notify():
while True:
print("notify start")
begin = datetime.datetime.now()
process()
end = datetime.datetime.now()
print('notify finished')
print("time elapsed", (end - begin).total_seconds())
sleep(60)