Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Form_test(StatesGroup):
- first = State()
- second = State()
- @bot_dp.callback_query_handler(lambda c: c.data == 'test')
- async def start_test(callback_query: types.CallbackQuery):
- await callback_query.answer()
- user_id = callback_query.from_user.id
- r = requests.post(http_getusertoken, json={"password": "1488", "telegram_id": int(user_id)})
- TOKEN_USER = r.json()["access_token"]
- headers = {"Authorization": "Bearer " + TOKEN_USER}
- count = 0
- r = requests.get(http_getusertestlist, headers=headers)
- keyboard = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=False)
- if len(dp.values(r.json(), "/tests/*/value")) == 0:
- await bot.send_message(callback_query.message.chat.id, "Увы для вас пока нет тестов.")
- else:
- for i in dp.values(r.json(), "/tests/*/value"):
- row = types.KeyboardButton(str(count) + ". " + i)
- count += 1
- keyboard.row(row)
- await Form_test.first.set()
- await callback_query.message.answer("Выберите тест: ", reply_markup=keyboard)
- @bot_dp.message_handler(lambda message: message.text, state=Form_test.first)
- async def edit_name_save(message: types.Message, state: FSMContext):
- async with state.proxy() as data:
- data['name_test'] = message.text
- user_id = message.chat.id
- r = requests.post(http_getusertoken, json={"password": "1488", "telegram_id": int(user_id)})
- TOKEN_USER = r.json()["access_token"]
- headers = {"Authorization": "Bearer " + TOKEN_USER}
- a = str(message.text).split(".")
- r = requests.get(http_getusertestlist, headers=headers)
- z = dp.values(r.json(), "/tests/*/id") # id тестов
- global res_id
- res_id = z[int(a[0])]
- r = requests.get(htt_test + str(res_id), headers=headers)
- res = json.loads(r.text)
- global questions_list
- questions_list = [] # Все ответы списком
- count_questions = 0
- for i in res["questions"]:
- q = res["questions"][count_questions]
- questions_list.append(q)
- count_questions += 1
- print(res)
- print(questions_list)
- await Form_test.next()
- await message.answer("Выбранный тест: " + data["name_test"])
- @bot_dp.message_handler(state=Form_test.second)
- async def Galkov(message: types.Message, state: FSMContext):
- print("HYI!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
- index = 0
- user_answers = []
- user_id = message.chat.id
- r = requests.post(http_getusertoken, json={"password": "1488", "telegram_id": int(user_id)})
- TOKEN_USER = r.json()["access_token"]
- headers = {"Authorization": "Bearer " + TOKEN_USER}
- if index != len(questions_list): # 20
- markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
- count = 1
- await bot.send_message(message.chat.id, questions_list[index]['value'])
- for answer in questions_list[index]['answers']: # все ответы и id которые идут к вопросу
- itembtn = types.KeyboardButton(str(count)) # только ответы которые выводяться в сообщение кнопки
- markup.add(itembtn)
- msg = bot.send_message(message.chat.id, str(count) + ". " + answer["value"],
- reply_markup=markup) # (ascii_letters[count])
- count += 1
- if index != 0:
- user_answers.append(questions_list[index - 1]['answers'][int(message.text) - 1]['id'])
- index += 1
- if index <= len(questions_list):
- await message.answer("РАБОТАЕТ") # await handler(message)
- else:
- print(user_answers)
- requests.post(http_postanswer, json={"answers": user_answers},
- headers=headers)
- r = requests.post(http_results + str(res_id), headers=headers)
- print(r.json())
- for i in dp.values(r.json(), "/groups/*/interpretation"):
- await bot.send_message(message.chat.id, i)
- time.sleep(3)
- await send_welcome(message)
Add Comment
Please, Sign In to add comment