From be351807037a8014450f5d1160159f75c296a905 Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 23 Sep 2024 18:54:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- key_management.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/key_management.py b/key_management.py index 9d1da1b6..dec49dd7 100644 --- a/key_management.py +++ b/key_management.py @@ -250,3 +250,25 @@ async def renew_expired_keys(): await asyncio.sleep(3600) + +async def notify_expiring_keys(): + while True: + current_time = datetime.utcnow() + threshold_time = int((current_time + timedelta(hours=10)).timestamp() * 1000) + + conn = await asyncpg.connect(DATABASE_URL) + try: + # Получаем пользователей с истекающими ключами + expiring_keys = await conn.fetch('SELECT tg_id, expiry_time FROM keys WHERE expiry_time <= $1', threshold_time) + + for record in expiring_keys: + tg_id = record['tg_id'] + expiry_time = record['expiry_time'] + + # Отправляем уведомление пользователю + await bot.send_message(tg_id, f"🔔 Ваш ключ истекает через 10 часов. Пожалуйста, продлите его, чтобы избежать отключения.") + + finally: + await conn.close() + + await asyncio.sleep(3600) # Проверяем каждый час \ No newline at end of file