def question_endpoint(update: Update, context: CallbackContext) -> int: db = next(get_db()) telegram_id = update.effective_chat.username curr_question_number = context.user_data['question_number'] card_name = context.user_data['card_name'] if curr_question_number < len(context.user_data['question_list']): curr_question = context.user_data['question_list'][ curr_question_number] update.message.reply_text( f"Вопрос {curr_question_number+1}: {curr_question}") update.message.reply_text(f"Введи ответ:") return ANSWERING_QUESTIONS else: update.message.reply_text(f"Ты закончил карточку {card_name}!") update.message.reply_text( f"Результат: {curr_question_number - len(context.user_data['error_question_list'])}/{curr_question_number}" ) if len(context.user_data['error_question_list']) == 0: update.message.reply_text(f"Поздравляю, ошибок нет!") else: update.message.reply_text(f"Ошибки:") for q, e_a, c_a in zip(context.user_data['error_question_list'], context.user_data['error_answer_list'], context.user_data['correct_answer_list']): update.message.reply_text(f"Вопрос: {q}") update.message.reply_text(f"Ваш ответ: {e_a}") update.message.reply_text(f"Правильный ответ: {c_a}") update.message.reply_text(f"---") context.user_data.clear() cards_name_list = list_card_names(db, telegram_id) return waiting_select_card_endpoint(update, context, cards_name_list)
def select_question(update: Update, context: CallbackContext) -> int: db = next(get_db()) telegram_id = update.effective_chat.username card_name = context.user_data['card_name'] user_answer = update.message.text card_name_list = list_card_names(db, telegram_id) if user_answer == 'Назад': return waiting_select_card_endpoint(update, context, card_name_list) card_questions_list = list_card_questions(db, telegram_id, card_name) if user_answer in card_questions_list: question = user_answer item = crud.get_item_for_user_card(db=db, telegram_id=telegram_id, card_name=card_name, question=question) print_question_and_answer(update, item) return waiting_select_question_endpoint(update, context, card_questions_list) else: update.message.reply_text( f"Вопроса '{user_answer}' в карточке {card_name} нет!") return waiting_select_question_endpoint(update, context, card_questions_list)
def start_play(update: Update, context: CallbackContext) -> int: db = next(get_db()) telegram_id = update.effective_chat.username is_already_registered = register(update, context) cards_name_list = list_card_names(db, telegram_id) if cards_name_list: return waiting_select_card_endpoint(update, context, cards_name_list) else: return no_cards_endpoint(update, context)
def card_delete_endpoint(update: Update, context: CallbackContext): db = next(get_db()) telegram_id = update.effective_chat.username card_names_list = list_card_names(db, telegram_id) card_names_kbd = make_card_names_kbd(card_names_list) card_names = ReplyKeyboardMarkup(card_names_kbd, one_time_keyboard=True) update.message.reply_text( "!!!УДАЛЕНИЕ КАРТОЧКИ!!!", reply_markup=card_names, ) return WAITING_DELETE_DECISION
def select_card(update: Update, context: CallbackContext) -> int: db = next(get_db()) telegram_id = update.effective_chat.username card_name = update.message.text cards_name_list = list_card_names(db, telegram_id) if card_name in cards_name_list: update.message.reply_text(f"Карточка с именем {card_name}:") context.user_data['card_name'] = card_name card_questions_list = list_card_questions(db, telegram_id, card_name) return waiting_select_question_endpoint(update, context, card_questions_list) else: update.message.reply_text(f"Карточки с именем {card_name} нет!") return waiting_select_card_endpoint(update, context, cards_name_list)
def changing_item(update: Update, context: CallbackContext) -> int: db = next(get_db()) telegram_id = update.effective_chat.username card_name = context.user_data['card_name'] question = context.user_data['question'] user_answer = update.message.text card_questions_list = list_card_questions(db, telegram_id, card_name) if user_answer == 'Назад': return waiting_select_question_endpoint(update, context, card_questions_list) if user_answer == 'Вопрос': return waiting_change_question_endpoint(update, context) if user_answer == 'Ответ': return waiting_change_answer_endpoint(update, context)
def change_answer(update: Update, context: CallbackContext): db = next(get_db()) telegram_id = update.effective_chat.username card_name = context.user_data['card_name'] question = context.user_data['question'] new_answer = update.message.text crud.delete_item(db, telegram_id, card_name, question) new_item = schemas.ItemBase(question=question, answer=new_answer) item = crud.create_card_item(db, new_item, telegram_id, card_name) update.message.reply_text("Ответ изменен!!!") print_question_and_answer(update, item) card_questions_list = list_card_questions(db, telegram_id, card_name) return waiting_select_question_endpoint(update, context, card_questions_list)
def answering(update: Update, context: CallbackContext) -> int: db = next(get_db()) telegram_id = update.effective_chat.username card_name = context.user_data['card_name'] curr_question_number = context.user_data['question_number'] question = context.user_data['question_list'][curr_question_number] answer = update.message.text correct_answer = crud.get_item_for_user_card(db, telegram_id, card_name, question).answer if answer != correct_answer: context.user_data['error_question_list'].append(question) context.user_data['error_answer_list'].append(answer) context.user_data['correct_answer_list'].append(correct_answer) context.user_data['question_number'] += 1 return question_endpoint(update, context)
def start_delete(update: Update, context: CallbackContext) -> int: db = next(get_db()) telegram_id = update.effective_chat.username db_user = crud.get_user_by_telegram_id(db, telegram_id=telegram_id) if not db_user: update.message.reply_text("Ты новенький. Зарегистрировал тебя!)") user = schemas.UserCreate(telegram_id=telegram_id) crud.create_user(db=db, user=user) return no_cards_endpoint(update, context) else: update.message.reply_text("Снова здарова") card_names_list = list_card_names(db, telegram_id) if not card_names_list: return no_cards_endpoint(update, context) else: return card_delete_endpoint(update, context)
def creating_answer(update: Update, context: CallbackContext): db = next(get_db()) telegram_id = update.effective_chat.username card_name = context.user_data['card_name'] answer = update.message.text if not answer: update.message.reply_text(f"Ответ не может быть пустым!") update.message.reply_text(f'Введите ответ:') return CREATING_ANSWER else: update.message.reply_text(f"Сохраняю ответ: {answer}") context.user_data['card_item']['answer'] = answer item = schemas.ItemBase(**context.user_data['card_item']) crud.create_card_item(db=db, item=item, telegram_id=telegram_id, card_name=card_name) context.user_data['card_item'] = dict() return item_creation_decision(update, context)
def new_card_name_process(update: Update, context: CallbackContext) -> int: db = next(get_db()) telegram_id = update.effective_chat.username card_name = update.message.text result = crud.user_card_exists(db=db, telegram_id=telegram_id, card_name=card_name) if result: update.message.reply_text(f"Карточка {card_name} уже есть!") update.message.reply_text(f'Введите уникальное имя катрочки:') return TYPING_NEW_CARD_NAME else: update.message.reply_text(f"Создаю катрочку с именем: {card_name}") context.user_data['card_name'] = card_name context.user_data['card_item'] = dict() card = schemas.CardBase(card_name=card_name) crud.create_user_card(db=db, card=card, telegram_id=telegram_id) update.message.reply_text(f'Введите вопрос:') return CREATING_QUESTION
def creating_question(update: Update, context: CallbackContext): db = next(get_db()) telegram_id = update.effective_chat.username card_name = context.user_data['card_name'] question = update.message.text if not question: update.message.reply_text(f"Вопрос не может быть пустым!") update.message.reply_text(f'Введите вопрос:') return CREATING_QUESTION query = crud.user_card_question_exists(db=db, telegram_id=telegram_id, card_name=card_name, question=question) if query: update.message.reply_text(f"Вопрос {question} уже есть в катрочке {card_name}!") update.message.reply_text(f'Введите вопрос:') return CREATING_QUESTION else: update.message.reply_text(f"Сохраняю вопрос: {question}") context.user_data['card_item']['question'] = question update.message.reply_text(f'Введите ответ:') return CREATING_ANSWER
def change_question(update: Update, context: CallbackContext): db = next(get_db()) telegram_id = update.effective_chat.username card_name = context.user_data['card_name'] question = context.user_data['question'] new_question = update.message.text card_questions_list = list_card_questions(db, telegram_id, card_name) if new_question not in card_questions_list: answer = crud.get_item_for_user_card(db, telegram_id, card_name, question).answer crud.delete_item(db, telegram_id, card_name, question) new_item = schemas.ItemBase(question=new_question, answer=answer) item = crud.create_card_item(db, new_item, telegram_id, card_name) update.message.reply_text("Вопрос изменен!!!") print_question_and_answer(update, item) card_questions_list = list_card_questions(db, telegram_id, card_name) return waiting_select_question_endpoint(update, context, card_questions_list) else: update.message.reply_text(f"Ткой вопрос уже есть в карточке {card_name}!") return waiting_select_question_endpoint(update, context, card_questions_list)
def start_new(update: Update, context: CallbackContext) -> int: db = next(get_db()) telegram_id = update.effective_chat.username db_user = crud.get_user_by_telegram_id(db, telegram_id=telegram_id) if not db_user: update.message.reply_text("Ты новенький. Зарегистрировал тебя!)") user = schemas.UserCreate(telegram_id=telegram_id) crud.create_user(db=db, user=user) else: update.message.reply_text("Снова здарова") card_list = list_card_names(db=db, telegram_id=telegram_id) if card_list: card_list_msg = '\t- ' + '\n\t- '.join(card_list) update.message.reply_text(f"Уже созданные тобой карточки:\n{card_list_msg}") else: update.message.reply_text(f"Ты пока не создал ни одной карточки(") update.message.reply_text( "Меню создания карточки:", reply_markup=start_creation, ) return CARD_CREATION_CHOICE
def delete_card(update: Update, context: CallbackContext): db = next(get_db()) telegram_id = update.effective_chat.username card_name = update.message.text card_exists_query = crud.user_card_exists(db=db, telegram_id=telegram_id, card_name=card_name) if not card_exists_query: update.message.reply_text(f"Карточки {card_name} нет!") return card_delete_endpoint(update, context) else: update.message.reply_text(f"Удаляю {card_name}:") crud.delete_card(db=db, card_name=card_name, telegram_id=telegram_id) if not list_card_names(db=db, telegram_id=telegram_id): return no_cards_endpoint(update, context) update.message.reply_text( "Продолжить удаление?", reply_markup=end_deleting_items, ) return WAITING_CONTINUE