def select_months_att_handler(call_back): bot_msg = bot.edit_message_text( text="{0}\U00002026".format(choice(loading_text["schedule"])), chat_id=call_back.message.chat.id, message_id=call_back.message.message_id ) json_attestation = func.get_json_attestation(call_back.message.chat.id) answers = [] is_full_place = func.is_full_place(call_back.message.chat.id) if call_back.message.text == "Выбери месяц:": schedule_variations = [(True, True, False), (False, True, False)] else: schedule_variations = [(True, False, True), (False, False, True)] for personal, session, only_resit in schedule_variations: answers = func.create_session_answers(json_attestation, call_back.data, call_back.message.chat.id, is_full_place, personal, session, only_resit) if answers: break if not answers: answers.append("<i>Нет событий</i>") try: bot.edit_message_text(text=answers[0], chat_id=call_back.message.chat.id, message_id=bot_msg.message_id, parse_mode="HTML") except ApiException: func.send_long_message(bot, answers[0], call_back.message.chat.id) finally: for answer in answers[1:]: func.send_long_message(bot, answer, call_back.message.chat.id)
def week_day_schedule_handler(call_back): bot_msg = bot.edit_message_text(text="{0}\U00002026".format( choice(loading_text["schedule"])), chat_id=call_back.message.chat.id, message_id=call_back.message.message_id) is_next_week = False iso_day_date = list((datetime.today() + server_timedelta).isocalendar()) if iso_day_date[2] == 7: iso_day_date[1] += 1 if call_back.data == "Следующее": iso_day_date[1] += 1 is_next_week = True iso_day_date[2] = week_day_number[week_day_titles[ call_back.message.text.split(": ")[-1]]] day_date = func.date_from_iso(iso_day_date) json_day = func.get_json_day_data(call_back.message.chat.id, day_date, next_week=is_next_week) full_place = func.is_full_place(call_back.message.chat.id) answer = func.create_schedule_answer(json_day, full_place, call_back.message.chat.id) try: bot.edit_message_text(text=answer, chat_id=call_back.message.chat.id, message_id=bot_msg.message_id, parse_mode="HTML") except ApiException: func.send_long_message(bot, answer, call_back.message.chat.id)
def now_lesson_handler(message): bot.send_chat_action(message.chat.id, "typing") today = datetime.today() + server_timedelta json_day = func.get_json_day_data(message.chat.id, today.date()) full_place = func.is_full_place(message.chat.id) answer = func.create_schedule_answer(json_day, full_place, message.chat.id) if "Выходной" in answer: func.send_long_message(bot, answer, message.chat.id) else: lessons = answer.strip().split("\n\n")[1:] for lesson in lessons: times = [] for st in lesson.split("\n")[0].split(" ")[-1].split( emoji["en_dash"]): times.append(func.string_to_time(st)) if times[0] <= today.time() <= times[1]: answer = "{0} <b>Пара:</b>\n{1}".format(emoji["books"], lesson) func.send_long_message(bot, answer, message.chat.id) return elif today.time() <= times[0] and today.time() <= times[1]: answer = "{0} <b>Перерыв</b>\n\nСледующая:\n{1}".format( emoji["couch_and_lamp"], lesson) func.send_long_message(bot, answer, message.chat.id) return answer = "{0} Пары уже закончились".format(emoji["sleep"]) func.send_long_message(bot, answer, message.chat.id)
def schedule_for_interval(message): bot.send_chat_action(message.chat.id, "typing") from_date, to_date = func.text_to_interval(message.text.lower()) json_data = func.get_json_interval_data(message.chat.id, from_date=from_date, to_date=to_date + timedelta(days=1)) is_send = False full_place = func.is_full_place(message.chat.id) if len(json_data["Days"]) > 10: answer = "{0} Превышен интервал в <b>10 дней</b>".format( emoji["warning"]) bot.send_message(text=answer, chat_id=message.chat.id, parse_mode="HTML") return elif len(json_data["Days"]): for day_info in json_data["Days"]: answer = func.create_schedule_answer(day_info, full_place, message.chat.id) if "Выходной" in answer: continue func.send_long_message(bot, answer, message.chat.id) is_send = True if not is_send or not len(json_data["Days"]): answer = "{0} С <i>{1}</i> по <i>{2}</i> занятий нет".format( emoji["sleep"], func.datetime_to_string(from_date), func.datetime_to_string(to_date)) bot.send_message(text=answer, chat_id=message.chat.id, parse_mode="HTML")
def today_schedule_handler(message): bot.send_chat_action(message.chat.id, "typing") for_date = datetime.today().date() + server_timedelta if message.text.capitalize() == "Завтра": for_date += timedelta(days=1) json_day = func.get_json_day_data(message.chat.id, for_date) full_place = func.is_full_place(message.chat.id) answer = func.create_schedule_answer(json_day, full_place, message.chat.id) func.send_long_message(bot, answer, message.chat.id)
def schedule_for_weekday(message): bot.send_chat_action(message.chat.id, "typing") message.text = message.text.title() if message.text in week_day_titles.values(): week_day = message.text else: week_day = week_day_titles[message.text] day_date = func.get_day_date_by_weekday_title(week_day) json_day = func.get_json_day_data(message.chat.id, day_date) full_place = func.is_full_place(message.chat.id) answer = func.create_schedule_answer(json_day, full_place, message.chat.id) func.send_long_message(bot, answer, message.chat.id)
def schedule_for_day(message): bot.send_chat_action(message.chat.id, "typing") day = func.text_to_date(message.text.lower()) json_week = func.get_json_week_data(message.chat.id, for_day=day) json_day = func.get_json_day_data(message.chat.id, day_date=day, json_week_data=json_week) full_place = func.is_full_place(message.chat.id) answer = func.create_schedule_answer(json_day, full_place, user_id=message.chat.id, personal=True) func.send_long_message(bot, answer, message.chat.id)
def place_handler(message): bot.send_chat_action(message.chat.id, "typing") answer = "Здесь ты можешь выбрать отображаемый формат адреса\nСейчас: " place_keyboard = InlineKeyboardMarkup(True) if func.is_full_place(message.chat.id): answer += "<b>Полностью</b>" place_keyboard.row(*[ InlineKeyboardButton(text=name, callback_data="Аудитория") for name in ["Только аудитория"] ]) else: answer += "<b>Только аудитория</b>" place_keyboard.row(*[ InlineKeyboardButton(text=name, callback_data="Полностью") for name in ["Полностью"] ]) bot.send_message(message.chat.id, answer, parse_mode="HTML", reply_markup=place_keyboard)
def schedule_sender(): sql_con = get_connection() cursor = sql_con.cursor() cursor.execute("""SELECT id FROM user_data WHERE sending = 1""") data = cursor.fetchall() cursor.close() sql_con.close() send_cnt = 0 err_cnt = 0 tomorrow_moscow_datetime = \ datetime.today() + timedelta(days=1) + server_timedelta tomorrow_moscow_date = tomorrow_moscow_datetime.date() for user_data in data: user_id = user_data[0] json_day = get_json_day_data(user_id, tomorrow_moscow_date) full_place = is_full_place(user_id) answer = create_schedule_answer(json_day, full_place, user_id) if "Выходной" in answer: continue try: answer = "Расписание на завтра:\n\n" + answer send_long_message(bot, answer, user_id) send_cnt += 1 except Exception as err: print(err) print(user_id) err_cnt += 1 continue return send_cnt, err_cnt
def inline_query_weekday_schedule_handler(inline_query): user_id = inline_query.from_user.id week_day = inline_query.query.title() day_date = func.get_day_date_by_weekday_title(week_day) json_day = func.get_json_day_data(user_id, day_date) full_place = func.is_full_place(user_id) answer = func.create_schedule_answer(json_day, full_place, user_id) group_info = func.get_current_group(user_id) week_num = (datetime.today() + server_timedelta).isocalendar()[1] r = InlineQueryResultArticle( id="{0}_{1}_{2}_{3}".format(user_id, group_info[0], week_num, week_day_number[week_day]), title=answer.split("\n\n")[0], input_message_content=InputTextMessageContent( answer, parse_mode="HTML" ), description=group_info[1] ) bot.answer_inline_query(inline_query.id, [r], cache_time=1, is_personal=True)
def all_week_schedule_handler(call_back): user_id = call_back.message.chat.id bot_msg = bot.edit_message_text(text="{0}\U00002026".format( choice(loading_text["schedule"])), chat_id=call_back.message.chat.id, message_id=call_back.message.message_id) if call_back.data == "Текущее": json_week = func.get_json_week_data(user_id) else: json_week = func.get_json_week_data(user_id, next_week=True) inline_answer = json_week["WeekDisplayText"] bot.answer_callback_query(call_back.id, inline_answer, cache_time=1) is_send = False if len(json_week["Days"]): for day in json_week["Days"]: full_place = func.is_full_place(call_back.message.chat.id) answer = func.create_schedule_answer(day, full_place, call_back.message.chat.id) if "Выходной" in answer: continue if json_week["Days"].index(day) == 0 or not is_send: try: bot.edit_message_text(text=answer, chat_id=user_id, message_id=bot_msg.message_id, parse_mode="HTML") except ApiException: func.send_long_message(bot, answer, user_id) else: func.send_long_message(bot, answer, user_id) is_send = True if not is_send or not len(json_week["Days"]): answer = "{0} Выходная неделя".format(emoji["sleep"]) bot.edit_message_text(text=answer, chat_id=user_id, message_id=bot_msg.message_id)