ruz-bot/daemons/notify.py
2022-10-21 15:16:21 +03:00

39 lines
1.4 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
import zoneinfo
from time import sleep
from daemons.bot import bot
from helpers.mongo import mongo
def process():
for user in mongo.users_collection.find({"notify_minutes": {"$ne": None}, "hse_id": {"$ne": None}}):
zone = zoneinfo.ZoneInfo("Europe/Moscow")
now = datetime.datetime.now(zone)
for lesson in mongo.lessons_collection.find({
"hse_user_id": user["hse_id"],
"begin": {"$lte": now + datetime.timedelta(minutes=5)},
"notified": False
}):
ans = ""
ans += "Аудитория: " + lesson["building"] + ", " + lesson["auditorium"] + "\n"
ans += "Начало: " + lesson["begin"].strftime("%H:%M") + "\n"
ans += "Конец: " + lesson["end"].strftime("%H:%M") + "\n"
ans += "Преподаватель: " + lesson["lecturer"] + "\n"
bot.send_message(
user["chat_id"],
"Уведомляю о занятиях!\n" + ans
)
mongo.lessons_collection.update_one({"_id": lesson['_id']}, {"$set": {"notified": True}})
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 * 2)