def weekday_select_handler(message): """ Handler for schedule request of specific weekday """ log(permanent.MODULE_NAME, message) # check user is configured if not controller.get_user(message.chat.id).is_configured: bot.send_message(message.chat.id, permanent.MESSAGE_USER_NOT_CONFIGURED, reply_markup=main_markup) return # remove star symbol if needed weekday = message.text[:2] # force check message is weekday due to some bug if weekday not in permanent.TEXT_DAYS_OF_WEEK: return # get list of lessons for specified user and day schedule = controller.get_day_lessons( message.from_user.id, day=permanent.TEXT_DAYS_OF_WEEK.index(weekday)) elective_shedule = get_day_elective_lessons( message.from_user.id, day=permanent.TEXT_DAYS_OF_WEEK.index(weekday)) # convert lessons to understandable string output reply = "Core Courses:\n" reply += permanent.MESSAGE_FREE_DAY if not schedule else \ permanent.HEADER_SEPARATOR.join(str(lesson) for lesson in schedule) reply += '\nElectives:\n' reply += (MESSAGE_FREE_DAY_ELECTIVE if not elective_shedule else permanent.HEADER_SEPARATOR.join( str(lesson) for lesson in elective_shedule)) bot.send_message(message.chat.id, reply, reply_markup=main_markup)
def schedule_command_handler(message): """ Register module's commands """ log(permanent.MODULE_NAME, message) if '/friend' in message.text: # both '/friend' and '/friend @alias' are supported if message.text == "/friend": msg = bot.send_message(message.chat.id, permanent.REQUEST_ALIAS) bot.register_next_step_handler(msg, process_friend_request_step) elif len(message.text) > 8: alias = message.text[8:].strip() send_friend_schedule(message, alias) elif message.text == '/configure_schedule': # Register user if he is not registered if not controller.get_user(message.from_user.id): controller.register_user(message.from_user.id, message.from_user.username) # set configured to false and remove his groups controller.set_user_configured(message.from_user.id, False) options = telebot.types.ReplyKeyboardMarkup(True, False) # add buttons to choose course options.add(*list(permanent.REGISTERED_COURSES.keys())) msg = bot.send_message(message.chat.id, permanent.REQUEST_COURSE, reply_markup=options) bot.register_next_step_handler(msg, process_course_step)
def garbage_message_handler(message): """ Handler for any other unknown messages Defined when all other modules already defined their handlers """ log(permanent.MODULE_NAME, message) # show main buttons if unknown input sent bot.send_message(message.chat.id, permanent.MESSAGE_ERROR, reply_markup=main_markup) alias = get_user(message.from_user.id).alias if ' ' in message.text: for admin in SUPERADMIN_LIST: bot.send_message( admin, f"{permanent.MESSAGE_UNKNOWN} {str(alias)}:\n{message.text}" )
def get_relevant_reminders(session): """ Function is called in fixed amount of minutes before each lesson (e.g. 10 minutes) Returns list of tuples with user ids and lessons. Each user in tuple must be reminded about his lesson :param session: sqlalchemy session from decorator :return: [(int, Lesson)] """ users = session.query(User).all() need_remind = [] for user in users: if not schedule_controller.get_user(user.id).is_configured: continue next_lesson = schedule_controller.get_next_lesson(user.id) if next_lesson and abs(next_lesson.minutes_until_start - permanent.REMIND_WHEN_LEFT_MINUTES) <= 1: need_remind.append((user.id, next_lesson)) return need_remind
def main_buttons_handler(message): """ Handler for processing three main buttons requests """ log(permanent.MODULE_NAME, message) # check user if configured user = controller.get_user(message.chat.id) if not user or not user.is_configured: bot.send_message(message.chat.id, permanent.MESSAGE_USER_NOT_CONFIGURED, reply_markup=main_markup) return # update alias if it was changed controller.set_user_alias(message.from_user.id, message.from_user.username) if message.text == permanent.TEXT_BUTTON_NOW: send_current_schedule(message.chat.id, message.from_user.id) elif message.text == permanent.TEXT_BUTTON_DAY: markup = telebot.types.ReplyKeyboardMarkup(True) buttons = list() day_of_week = datetime.today().weekday() # make list of weekdays and add star for today for i, day in enumerate(permanent.TEXT_DAYS_OF_WEEK): buttons.append( telebot.types.KeyboardButton( day if day_of_week != i else day + "⭐")) markup.add(*buttons) bot.send_message(message.chat.id, permanent.REQUEST_WEEKDAY, reply_markup=markup) elif message.text == permanent.TEXT_BUTTON_WEEK: bot.send_message(message.chat.id, permanent.MESSAGE_FULL_WEEK, reply_markup=main_markup)