def pick_seminar_date_from_list(update: Update, context: CallbackContext): query = update.callback_query query.answer() user_id = query.message.chat.id user_assignments = read_data(user_id)['assignments'] seminar_index = query.data seminar_name = query.message.reply_markup.inline_keyboard[int( seminar_index)][0]['text'] context.user_data['user_seminar_choice'] = seminar_name if seminar_name not in user_assignments.keys(): query.edit_message_text( text=f'Данные по предмету "{seminar_name}" отсутствуют') update.callback_query.message.bot.delete_message( user_id, context.user_data['command_to_view_message_id']) return ConversationHandler.END else: seminar_dates_with_assignments = list( user_assignments[seminar_name].keys()) seminar_dates_with_assignments.sort( key=lambda d: datetime.datetime.strptime(d, "%d.%m.%Y")) keyboard = [[InlineKeyboardButton(date, callback_data=date)] for date in seminar_dates_with_assignments] query.edit_message_text(text="Выберите дату занятия.", reply_markup=InlineKeyboardMarkup(keyboard)) return ASSIGNMENT
def view_assignment(update: Update, context: CallbackContext): user_id = update.message.chat['id'] user_group = read_data(user_id)['group'] if user_group is not None: context.user_data['command_to_view_message_id'] = update.message[ 'message_id'] seminars, _, _ = get_group_seminars(user_group) keyboard = [[ InlineKeyboardButton(seminar_name, callback_data=str(seminar_index)) ] for seminar_index, seminar_name in enumerate(seminars)] update.message.reply_text("Выберите необходимый предмет", reply_markup=InlineKeyboardMarkup(keyboard)) return DATE else: update.message.reply_text( f'Необходимо ввести номер группы. (/group номер)', reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=False, resize_keyboard=True)) return ConversationHandler.END
def process_assignment_date(update: Update, context: CallbackContext): query = update.callback_query query.answer() assignment = context.user_data['assignment'] assignment.date = query.data if context.user_data['assignment_time_type'] == 'future_seminars': bot_reply_message = query.edit_message_text( text=f"Отправьте фотографию или текст!\n1) Используйте /add как подпись к фотографии " f"\n2) /add текст") else: user_id = query.message.chat['id'] user_assignments = read_data(user_id)['assignments'] seminar_data = user_assignments[assignment.seminar_name][assignment.date] keyboard = [[InlineKeyboardButton('Удалить запись', callback_data='delete')]] reply_markup = InlineKeyboardMarkup(keyboard) if 'photo_data' in seminar_data.keys(): if 'text_data' in seminar_data.keys(): bot_reply_message = update.callback_query.message.reply_photo(seminar_data['photo_data'], caption=f"'{assignment.seminar_name}' на {assignment.date}" f"\n\nТекст: {seminar_data['text_data']}" f"\n\nОтправьте фотографию или текст, чтобы заменить задание!" f"\n1) Используйте /add как подпись к фотографии " f"\n2) /add текст", reply_markup=reply_markup) else: bot_reply_message = update.callback_query.message.reply_photo(seminar_data['photo_data'], caption=f"'{assignment.seminar_name}' на {assignment.date}" f"\n\nОтправьте фотографию или текст, чтобы заменить задание!" f"\n1) Используйте /add как подпись к фотографии " f"\n2) /add текст", reply_markup=reply_markup) query.delete_message() else: bot_reply_message = query.edit_message_text(text=f"{assignment.seminar_name} на {assignment.date}:" f"\n{seminar_data['text_data']} " f"\n\nОтправьте фотографию или текст, чтобы заменить задание!" f"\n1) Используйте /add как подпись к фотографии " f"\n2) /add текст", reply_markup=reply_markup) context.user_data['bot_reply_message_id'] = bot_reply_message['message_id'] return ACTION
def choose_assignment_date(update: Update, context: CallbackContext): query = update.callback_query query.answer() user_id = query.message.chat['id'] user_data = read_data(user_id) user_utc_delta = user_data['utc_delta'] date = convert_utc_to_local_time(query.message.date, user_utc_delta) assignment = context.user_data['assignment'] assignment_time_type = query.data context.user_data['assignment_time_type'] = assignment_time_type seminar_weekdays = context.user_data['seminars_weekdays'][assignment.seminar_name] keyboard = None if assignment_time_type == "future_seminars": assignment.future_seminars_dates = generate_dates_of_future_seminars(date, seminar_weekdays) buttons = [InlineKeyboardButton(date, callback_data=date) for date in assignment.future_seminars_dates] keyboard = [buttons[k:k + 2] for k in range(0, len(buttons), 2)] elif assignment_time_type == "past_seminars": if assignment.seminar_name not in user_data['assignments'].keys(): query.edit_message_text(f'Нет добавленных заданий по "{assignment.seminar_name}"') query.message.bot.delete_message(user_id, context.user_data['command_to_add_mes_id_1']) return ConversationHandler.END else: dates_of_added_assignments = list(user_data['assignments'][assignment.seminar_name].keys()) buttons = [InlineKeyboardButton(date, callback_data=date) for date in dates_of_added_assignments] keyboard = [buttons[k:k + 2] for k in range(0, len(buttons), 2)] reply_markup = InlineKeyboardMarkup(keyboard) query.edit_message_text(text="Выберите дату занятия.", reply_markup=reply_markup) return DATE
def add_during_seminar(update: Update, context: CallbackContext): user_id = update.message.chat['id'] user_data = read_data(user_id) user_group = user_data['group'] user_utc_delta = user_data['utc_delta'] date = convert_utc_to_local_time(update.message.date, user_utc_delta) if user_group is not None: seminar_name, seminar_weekdays = get_current_seminar(user_group, date) if seminar_name is not None: future_seminars_dates = generate_dates_of_future_seminars(date, seminar_weekdays) next_seminar_date = future_seminars_dates[0] assignment = Assignment(seminar_name=seminar_name, date=next_seminar_date, future_seminars_dates=future_seminars_dates) assignment.parse_message(update.message) context.user_data['assignment'] = assignment keyboard = [ [InlineKeyboardButton(f"Добавить '{seminar_name}' на следующий семинар ({next_seminar_date})", callback_data="next_seminar")], [InlineKeyboardButton("Выбрать другую дату", callback_data='other_dates')], ] reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text("На какой день записать?", reply_markup=reply_markup) return PICK_DATE else: update.message.reply_text(f'Занятия сейчас нет. Попробуйте добавить вручную!', reply_markup=reply_markup_keyboard) else: update.message.reply_text(f'Необходимо ввести номер группы. (/group номер)', reply_markup=reply_markup_keyboard)
def send_assignment_to_user(update: Update, context: CallbackContext): query = update.callback_query query.answer() user_id = query.message.chat.id user_assignments = read_data(user_id)['assignments'] seminar_date = query.data seminar_name = context.user_data['user_seminar_choice'] if seminar_name in user_assignments.keys(): seminar_data = user_assignments[seminar_name][seminar_date] if 'photo_data' in seminar_data.keys(): if 'text_data' in seminar_data.keys(): update.callback_query.message.reply_photo( seminar_data['photo_data'], caption=f"'{seminar_name}' на {seminar_date}" f"\n\nТекст: {seminar_data['text_data']}") else: update.callback_query.message.reply_photo( seminar_data['photo_data'], caption=f"'{seminar_name}' на {seminar_date}") query.delete_message() else: query.edit_message_text( text= f"{seminar_name} на {seminar_date}:\n{seminar_data['text_data']}" ) else: query.edit_message_text( text=f'Данные по предмету "{seminar_name}" отсутствуют') query.message.bot.delete_message( user_id, context.user_data['command_to_view_message_id']) return ConversationHandler.END
def add_assignment(update: Update, context: CallbackContext): user_id = update.message.chat['id'] user_group = read_data(user_id)['group'] context.user_data['user_group'] = user_group if user_group is not None: context.user_data['command_to_add_mes_id_1'] = update.message.message_id seminars, _, seminars_weekdays = get_group_seminars(user_group) context.user_data['seminars_weekdays'] = seminars_weekdays keyboard = [[InlineKeyboardButton(seminar_name, callback_data=str(ind))] for ind, seminar_name in enumerate(seminars)] reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text(f'Выберите необходимый предмет', reply_markup=reply_markup) return FIELD else: update.message.reply_text(f'Необходимо ввести номер группы. (/group номер)', reply_markup=reply_markup_keyboard)
def view_assignment_for_specific_date(update: Update, context: CallbackContext): user_id = update.message.chat['id'] message = update.message.text date = message.split('/view')[1].strip() user_assignments = read_data(user_id)['assignments'] # delete user command /view date # delete the previous message with pages if the user used /view again try: update.message.bot.delete_message(user_id, update.message.message_id) if 'assignment_for_specific_date_id' in context.user_data.keys(): update.message.bot.delete_message( user_id, context.user_data['assignment_for_specific_date_id']) except BadRequest: pass assignments_for_the_date = [] for seminar_name in user_assignments.keys(): if date in user_assignments[seminar_name].keys(): assignment = Assignment(seminar_name=seminar_name, date=date, **user_assignments[seminar_name][date]) assignments_for_the_date.append({ 'data_type': assignment.to_string()[1], "text": assignment.to_string()[0], "photo": assignment.photo_data }) if len(assignments_for_the_date) != 0: paginator = InlineKeyboardSimplePaginator( page_count=len(assignments_for_the_date), data_pattern='assignment#{page}') if assignments_for_the_date[0]['data_type'] == "text": assignments_for_specific_date_message = update.message.reply_text( text=assignments_for_the_date[0]['text'], reply_markup=paginator.markup, parse_mode='Markdown') else: assignments_for_specific_date_message = update.message.reply_photo( photo=assignments_for_the_date[0]['photo'], caption=assignments_for_the_date[0]['text'], reply_markup=paginator.markup, parse_mode='Markdown') context.user_data[ 'assignments_for_the_date'] = assignments_for_the_date context.user_data[ 'assignment_for_specific_date_id'] = assignments_for_specific_date_message.message_id context.user_data[ 'previous_page_data_type'] = assignments_for_the_date[0][ 'data_type'] else: update.message.reply_text(f'Заданий на {date} не найдено!') update.message.bot.delete_message(user_id, update.message.message_id)