/
Esgalnor
/
TG_Bot
Обзор
Документация
Войти
/
Esgalnor
/
TG_Bot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
patch
show_recs.py
180 строк
5 KB
Esgalnor
upload files
18 авг 2025, 17:25
18 авг 2025, 17:25
4bf5fb4
Код
Авторство
О чём код?
import datetime from loader import bot from database.user_pw import users from database.calendar_pw import my_calendar from states.states import MasterStates from config.config import MASTER_ID #from utils.my_calendar import calendar_1, cal_1 from utils.my_calendar import calendar_for_show, cal_for_show from utils.json_funcs import to_obj from telebot.types import Message, ReplyKeyboardRemove, CallbackQuery, InlineKeyboardMarkup, InlineKeyboardButton @bot.message_handler(state="*", commands=["show_recs"]) def show_recs(message: Message): id = message.from_user.id if id == MASTER_ID: # del_old_rec() keyboard = InlineKeyboardMarkup(row_width=2) row = list() row.append(InlineKeyboardButton(text="Сегодня", callback_data="today")) row.append(InlineKeyboardButton(text="Завтра", callback_data="tomorrow")) row.append(InlineKeyboardButton(text="Выбрать день", callback_data="choice")) keyboard.row(*row) bot.send_message( id, text="Записи за какой день показать?", reply_markup=keyboard ) bot.set_state(id, MasterStates.show_rec) users.set_temp(id) @bot.callback_query_handler(func=lambda call: call.data.startswith("today"), state=MasterStates.show_rec) def today(call: CallbackQuery): id = call.from_user.id date = datetime.date.today().strftime("%d.%m.%Y") send_data(date, id, call) @bot.callback_query_handler(func=lambda call: call.data.startswith("tomorrow"), state=MasterStates.show_rec) def tomorrow(call: CallbackQuery): id = call.from_user.id date = datetime.date.today() delta = datetime.timedelta(days=1) date += delta date = date.strftime("%d.%m.%Y") send_data(date, id, call) @bot.callback_query_handler(func=lambda call: call.data.startswith("choice"), state=MasterStates.show_rec) def choice(call: CallbackQuery): id = call.from_user.id now = datetime.datetime.now() # Get the current date bot.edit_message_text( text="Выберите дату", chat_id=id, message_id=call.message.message_id, reply_markup=cal_for_show.create_calendar( name=calendar_for_show.prefix, # Specify the NAME of your calendar year=now.year, month=now.month, ), ) @bot.callback_query_handler(func=lambda call: call.data.startswith(calendar_for_show.prefix), state=MasterStates.show_rec) def callback_inline(call: CallbackQuery): """ Processing inline callback requests for a calendar :param call: :return: """ # At this point, we are sure that this calendar is ours. So we cut the line by the separator of our calendar name, action, year, month, day = call.data.split(calendar_for_show.sep) # Processing the calendar. Get either the date or None if the buttons are of a different type id = call.from_user.id try: date = cal_for_show.calendar_query_handler( bot=bot, call=call, name=name, action=action, year=year, month=month, day=day ) except: bot.send_message(id, "К сожалению на эти даты записи пока нет.") bot.delete_state(id) return choice_date = date.strftime('%d.%m.%Y') data = get_rec(choice_date) if data == '': data = 'Записей на {} нет'.format(choice_date) bot.send_message( id, choice_date + '\n' + data ) bot.delete_state(id) users.set_temp(id) def send_data(date: str, id, call: CallbackQuery): data = get_rec(date) if data == '': data = 'Записей на {} нет'.format(date) bot.edit_message_text( data, id, call.message.message_id ) def get_all_rec_day(day: str): return list(map(int, my_calendar.get_users_on_rec(day).split())) def get_time_rec(id): rec = to_obj(users.get_curr_rec(id)) if rec: for date in rec.keys(): curr_rec = date break else: return '' return curr_rec.split()[1] def get_duration(id): rec = to_obj(users.get_curr_rec(id)) if rec: for date in rec.values(): curr_rec = date break else: return '' return sum(to_obj(curr_rec).values()) def get_rec(day): text = [] for user in get_all_rec_day(day): text.append("{}, {}, {} мин." "".format(user, get_time_rec(user), get_duration(user))) return '\n'.join(text) def del_old_rec(): now = datetime.datetime.now() users_list = list() date = now while True: delta = datetime.timedelta(days=1) date -= delta try: users_list.append(my_calendar.get_users_on_rec(date.strftime('%d.%m.%Y')).split()) except: break for user_day in users_list: for user in user_day: users.del_rec(str(user)) if __name__ == '__main__': # print(get_duration(5222312325)) ...