async def callback_survey_handler(query: types.CallbackQuery): user = find_user(chat_id=query.message.chat.id) if not user: return _, command, question_number, choice_number = query.data.split(':') survey = Survey(**user.current_survey) raw_answers = user.answers questions = survey.questions if command == messages.COMMAND_SUBMIT: answers = list( map(lambda x: { 'answer_data': x[0], 'question_id': x[1].id }, zip(raw_answers, questions))) logging.debug(f"{answers}") # post all answers to the server for answer in answers: post_answer(**answer) await query.answer('submitted') await query.message.delete() else: logging.warning( f"There is no such command as {command}. query: {query.data}") await query.answer('')
async def cmd_start(message: types.Message): ''' Conversation's entry point. Send start message ''' logging.debug( f"start message. {message.from_user.id}/{message.from_user.username}") _, args = parse_command(message.text) if args: chat_id = message.chat.id pk = args[0] user = find_user(chat_id=chat_id) logging.debug(f"user: {user}") if not user: await message.answer(messages.NOT_REGISTERED) return try: survey = get_survey_or_error(user, pk) except (AccessError, DeadlineError) as e: await message.answer(e.__str__()) return if survey: # TODO: survey representation await start_survey(user, survey, message) # await message.answer(survey.__str__()) else: await message.answer(messages.HAVE_NO_PERMISSION) else: await message.answer(messages.ON_CMD_START)
async def process_email(message: types.Message, state: FSMContext): ''' Executes only if state is wait_for_email. Process email from user ''' email = message.text if not is_email_correct(email): await message.answer(messages.INVALID_EMAIL) return user = find_user(email=email) if user: await message.answer( f'user with username {user.username} already exists') return async with state.proxy() as data: data['email'] = email student = get_student(email) if not student: await message.answer("there is no such student email") return send_password(email, student['code']) await RegistrationStates.next() await message.answer(messages.SEND_CODE)
async def cmd_info(message: types.Message): logging.debug( f"info message. {message.from_user.id}/{message.from_user.username}") chat_id = message.from_user.id user = find_user(chat_id=chat_id) if not user: await message.answer('You are not registered') return else: await message.answer(f"email: {user.email}\nTrack: {Track(**user.track)}")
async def cmd_register(message: types.Message): logging.debug( f"register message. {message.from_user.id}/{message.from_user.username}" ) user = find_user(chat_id=message.from_user.id) if user: await message.answer(messages.REGISTERED) else: await RegistrationStates.wait_for_email.set() await message.answer(messages.SEND_EMAIL)
async def cmd_unregister(message: types.Message): logging.debug( f"unregister message. {message.from_user.id}/{message.from_user.username}" ) chat_id = message.from_user.id user = find_user(chat_id=chat_id) if not user: await message.answer('You are not registered') return else: if user.delete(): await message.answer('removed')
async def callback_answer_handler(query: types.CallbackQuery, state: FSMContext): ''' Callback for question button ''' user = find_user(chat_id=query.message.chat.id) if not user: return await state.finish() _, command, question_number, choice_number = query.data.split(':') if command == messages.COMMAND_SET: question_number = int(question_number) question = Survey(**user.current_survey).questions[question_number] # delete if choice_number in user.answers[question_number]: user.answers[question_number].remove(choice_number) # add/change else: if question.type == 0: user.answers[question_number] = [choice_number] elif question.type == 1: user.answers[question_number].append(choice_number) else: logging.warning( f"There is no such question type as {question.type}") user.commit() await show_question(user, question_number, query.message) elif command == messages.COMMAND_CHANGE: new_question: Union[Question, None] = await show_question( user, int(choice_number), query.message) if new_question: if new_question.type == 3: await QuestionsStates.wait_for_plain_text.set() else: pass await query.answer('')
async def process_plain_text(message: types.Message, state: FSMContext): user = find_user(chat_id=message.chat.id) if not user: return answer_text = message.text if len(answer_text) > config.PLAIN_TEXT_MAX_LENGTH: await message.answer(messages.PROCEES_PLAIN_TEXT_MAX_LENGTH % config.PLAIN_TEXT_MAX_LENGTH) return cur_number = user.current_question_number user.answers[cur_number] = [answer_text] user.commit() await message.answer(messages.PROCESS_PLAIN_TEXT_OK) message = await message.answer('_') await show_question(user, cur_number, message=message)