add manual reminder, decorate remind() in reminder.py
This commit is contained in:
+13
-3
@@ -126,16 +126,26 @@ def command_me(message):
|
|||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['remind'])
|
@bot.message_handler(commands=['remind'])
|
||||||
def remind_manually():
|
def remind_manually(message):
|
||||||
"""Remind manually"""
|
"""Remind manually"""
|
||||||
reminder.remind()
|
args = message.text.split()
|
||||||
|
if len(args) > 1:
|
||||||
|
try:
|
||||||
|
today = dt.strptime(args[1], "%m-%d-%Y")
|
||||||
|
except ValueError as ve:
|
||||||
|
bot.send_message(message.chat.id, f"Не удалось распарсить дату!\n{ve}", parse_mode='HTML')
|
||||||
|
else:
|
||||||
|
reminder.remind(message.chat.id, today)
|
||||||
|
else:
|
||||||
|
bot.send_message(message.chat.id, f"<b>Справка</b>\nФормат даты: MM-DD-YYYY\n"
|
||||||
|
f"Пример: /remind 02-14-2022", parse_mode='HTML')
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['jobs'])
|
@bot.message_handler(commands=['jobs'])
|
||||||
def list_jobs(message):
|
def list_jobs(message):
|
||||||
"""List all jobs"""
|
"""List all jobs"""
|
||||||
if message.chat.id == ADMIN_ID:
|
if message.chat.id == ADMIN_ID:
|
||||||
text = reminder.get_jobs()
|
text = reminder.print_get_jobs()
|
||||||
bot.send_message(message.chat.id, text, parse_mode='HTML')
|
bot.send_message(message.chat.id, text, parse_mode='HTML')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+10
-11
@@ -1,4 +1,4 @@
|
|||||||
import schedule
|
from schedule import repeat, every, get_jobs, run_pending
|
||||||
import time
|
import time
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from datetime import datetime as dt
|
from datetime import datetime as dt
|
||||||
@@ -12,34 +12,33 @@ from .config import PYTHONCHATRU, bot
|
|||||||
|
|
||||||
def scheduler():
|
def scheduler():
|
||||||
while True:
|
while True:
|
||||||
schedule.run_pending()
|
run_pending()
|
||||||
time.sleep(120)
|
time.sleep(120)
|
||||||
|
|
||||||
|
|
||||||
def remind():
|
@repeat(every().day.at('06:00'))
|
||||||
|
def remind(chat_to_repeat=PYTHONCHATRU, today=dt.today()):
|
||||||
""" Remind holiday. """
|
""" Remind holiday. """
|
||||||
with open('holidays.csv', newline='', encoding='utf-8') as holidays_file:
|
with open('holidays.csv', newline='', encoding='utf-8') as holidays_file:
|
||||||
holidays = tuple(csv.reader(holidays_file))
|
holidays = tuple(csv.reader(holidays_file))[1:]
|
||||||
today = dt.today()
|
for entry in holidays:
|
||||||
for entry in holidays[1:]:
|
|
||||||
date, holiday, description = entry
|
date, holiday, description = entry
|
||||||
date = dt.strptime(date, "%m-%d-%Y")
|
date = dt.strptime(date, "%m-%d-%Y")
|
||||||
if today.month == date.month and today.day == date.day:
|
if today.month == date.month and today.day == date.day:
|
||||||
notification = f'🎉💻 Сегодня {date:%d.%m}, <b><u>{holiday.upper()}</u></b>!\
|
notification = f'🎉💻 Сегодня <b><u>{holiday.upper()}</u></b>!\
|
||||||
\n\n{description}.'
|
\n\n{description}.'
|
||||||
if date.year != 1000:
|
if date.year != 1000:
|
||||||
age = today.year - date.year
|
age = today.year - date.year
|
||||||
notification += f'\n<i>{age} годовщина</i>'
|
notification += f'\n<i>{age} годовщина</i>'
|
||||||
bot.send_message(PYTHONCHATRU, notification, parse_mode='HTML') # PYTHONCHATRU
|
bot.send_message(chat_to_repeat, notification, parse_mode='HTML')
|
||||||
|
|
||||||
|
|
||||||
def get_jobs():
|
def print_get_jobs():
|
||||||
all_jobs = schedule.get_jobs()
|
all_jobs = get_jobs()
|
||||||
text = f"├ <b>Jobs:</b> {str(len(all_jobs))}\
|
text = f"├ <b>Jobs:</b> {str(len(all_jobs))}\
|
||||||
\n├ {dt.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
\n├ {dt.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
||||||
text += '<i>' + str(all_jobs) + '</i>'
|
text += '<i>' + str(all_jobs) + '</i>'
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
schedule.every().day.at('06:00').do(remind)
|
|
||||||
Thread(target=scheduler).start()
|
Thread(target=scheduler).start()
|
||||||
|
|||||||
Reference in New Issue
Block a user