Platitude

Untitled

Jun 8th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.10 KB | None | 0 0
  1. class Form_test(StatesGroup):
  2.     first = State()
  3.     second = State()
  4.  
  5.  
  6. @bot_dp.callback_query_handler(lambda c: c.data == 'test')
  7. async def start_test(callback_query: types.CallbackQuery):
  8.     await callback_query.answer()
  9.     user_id = callback_query.from_user.id
  10.     r = requests.post(http_getusertoken, json={"password": "1488", "telegram_id": int(user_id)})
  11.     TOKEN_USER = r.json()["access_token"]
  12.     headers = {"Authorization": "Bearer " + TOKEN_USER}
  13.     count = 0
  14.     r = requests.get(http_getusertestlist, headers=headers)
  15.     keyboard = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=False)
  16.     if len(dp.values(r.json(), "/tests/*/value")) == 0:
  17.         await bot.send_message(callback_query.message.chat.id, "Увы для вас пока нет тестов.")
  18.     else:
  19.         for i in dp.values(r.json(), "/tests/*/value"):
  20.             row = types.KeyboardButton(str(count) + ". " + i)
  21.             count += 1
  22.             keyboard.row(row)
  23.         await Form_test.first.set()
  24.         await callback_query.message.answer("Выберите тест: ", reply_markup=keyboard)
  25.  
  26.  
  27. @bot_dp.message_handler(lambda message: message.text, state=Form_test.first)
  28. async def edit_name_save(message: types.Message, state: FSMContext):
  29.     async with state.proxy() as data:
  30.         data['name_test'] = message.text
  31.     user_id = message.chat.id
  32.     r = requests.post(http_getusertoken, json={"password": "1488", "telegram_id": int(user_id)})
  33.     TOKEN_USER = r.json()["access_token"]
  34.     headers = {"Authorization": "Bearer " + TOKEN_USER}
  35.     a = str(message.text).split(".")
  36.     r = requests.get(http_getusertestlist, headers=headers)
  37.     z = dp.values(r.json(), "/tests/*/id")  # id тестов
  38.     global res_id
  39.     res_id = z[int(a[0])]
  40.     r = requests.get(htt_test + str(res_id), headers=headers)
  41.     res = json.loads(r.text)
  42.     global questions_list
  43.     questions_list = []  # Все ответы списком
  44.     count_questions = 0
  45.     for i in res["questions"]:
  46.         q = res["questions"][count_questions]
  47.         questions_list.append(q)
  48.         count_questions += 1
  49.     print(res)
  50.     print(questions_list)
  51.     await Form_test.next()
  52.     await message.answer("Выбранный тест: " + data["name_test"])
  53.  
  54.  
  55. @bot_dp.message_handler(state=Form_test.second)
  56. async def Galkov(message: types.Message, state: FSMContext):
  57.     print("HYI!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  58.     index = 0
  59.     user_answers = []
  60.     user_id = message.chat.id
  61.     r = requests.post(http_getusertoken, json={"password": "1488", "telegram_id": int(user_id)})
  62.     TOKEN_USER = r.json()["access_token"]
  63.     headers = {"Authorization": "Bearer " + TOKEN_USER}
  64.     if index != len(questions_list):  # 20
  65.         markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
  66.         count = 1
  67.         await bot.send_message(message.chat.id, questions_list[index]['value'])
  68.         for answer in questions_list[index]['answers']:  # все ответы и id которые идут к вопросу
  69.             itembtn = types.KeyboardButton(str(count))  # только ответы которые выводяться в сообщение кнопки
  70.             markup.add(itembtn)
  71.             msg = bot.send_message(message.chat.id, str(count) + ". " + answer["value"],
  72.                                    reply_markup=markup)  # (ascii_letters[count])
  73.             count += 1
  74.  
  75.     if index != 0:
  76.         user_answers.append(questions_list[index - 1]['answers'][int(message.text) - 1]['id'])
  77.     index += 1
  78.  
  79.     if index <= len(questions_list):
  80.         await message.answer("РАБОТАЕТ")  # await handler(message)
  81.     else:
  82.         print(user_answers)
  83.  
  84.         requests.post(http_postanswer, json={"answers": user_answers},
  85.                       headers=headers)
  86.  
  87.         r = requests.post(http_results + str(res_id), headers=headers)
  88.         print(r.json())
  89.         for i in dp.values(r.json(), "/groups/*/interpretation"):
  90.             await bot.send_message(message.chat.id, i)
  91.             time.sleep(3)
  92.             await send_welcome(message)
Add Comment
Please, Sign In to add comment