Birthdays notification made

This commit is contained in:
Profitroll 2022-10-25 14:18:51 +02:00
parent 2f24739cbb
commit 9ea575b2cb
6 changed files with 47 additions and 2 deletions

View File

@ -65,6 +65,6 @@ After all of that you're good to go! Happy using :)
* [x] Get applications .json
* [ ] Get application by id and user_id
* [x] Age as a DD.MM.YYYY
* [ ] Notify about upcoming birthdays
* [x] Notify about upcoming birthdays
* [x] Change the application data
* [x] Check if user is already in group

View File

@ -2,6 +2,8 @@
"locale": "uk",
"owner": 0,
"age_allowed": 0,
"birthdays_notify": true,
"birthdays_time": "09:00",
"admin_group": 0,
"destination_group": 0,
"admins": [],

View File

@ -52,6 +52,7 @@
"sus_refused_by": "❌ **Доступ заборонено**\nАдмін **{0}** заборонив `{1}` доступ до спільноти не за персональним посиланням.",
"reapply_forbidden": "❌ **Дія неможлива**\nТвоя минула анкета ще не була схвалена або відхилена.",
"reapply_in_progress": "❌ **Дія неможлива**\nТи прямо зараз вже заповнюєш анкету. Якщо в ній є помилка - після заповнення просто натисни **{0}** та почни знову.",
"birthday": "У користувача **{0}** (@{1}) сьогодні день народження! Виповнилось {2} років",
"question_titles": {
"question1": "Ім'я/звертання:",
"question2": "День народження:",

23
main.py
View File

@ -1,8 +1,11 @@
from datetime import datetime
from threading import Thread
from dateutil.relativedelta import relativedelta
from time import time
from os import getpid, path
from modules.birthdays import check_birthdays
from modules.utils import *
from schedule import run_pending, every
from pyrogram.client import Client
from pyrogram import filters
@ -558,6 +561,26 @@ if __name__ == "__main__":
# I did compare performance, almost no difference and it's much more useful this way. Change my mind.
app.start() # type: ignore
if configGet("birthdays_notify"):
every().day.at(configGet("birthdays_time")).do(check_birthdays, app)
# Background tasks checker
def background_task():
try:
while True:
try:
run_pending()
#print('Checked')
time.sleep(1)
except:
pass
except KeyboardInterrupt:
print('\nShutting down')
killProc(pid)
t = Thread(target=background_task)
t.start()
app.send_message(configGet("owner"), f"Starting up with pid `{pid}`") # type: ignore
# Registering user commands

18
modules/birthdays.py Normal file
View File

@ -0,0 +1,18 @@
from datetime import datetime
from os import fsdecode, listdir, sep
from modules.utils import configGet, jsonLoad, locale
from dateutil.relativedelta import relativedelta
def check_birthdays(app):
for user_file in listdir(f"{configGet('data', 'locations')}{sep}users{sep}"):
filename = fsdecode(f"{configGet('data', 'locations')}{sep}users{sep}{user_file}")
if filename.endswith(".json"):
user = jsonLoad(filename)
if isinstance(user["application"]["2"], str):
try:
if ".".join([((user["application"]["2"]).split("."))[0], ((user["application"]["2"]).split("."))[1]]) == datetime.now().strftime("%d.%m"):
tg_user = app.get_users(int(user_file.replace(".json", "")))
app.send_message( configGet("admin_group"), locale("birthday", "message").format(str(tg_user.first_name), str(tg_user.username), str(relativedelta(datetime.now(), datetime.strptime(user["application"]["2"], '%d.%m.%Y')).years)) )
except AttributeError:
continue

View File

@ -1,4 +1,5 @@
pyrogram>=2.0.59
tgcrypto>=1.2.4
ujson>=5.5.0
psutil>=5.9.2
psutil>=5.9.2
schedule