@lannyyyy

Проблемы с работой бота. Что ему не нравится?) На что жалуется?

ERROR:asyncio:Task exception was never retrieved
future: exception=IndexError('list index out of range')>
Traceback (most recent call last):
File "C:\Users\god\Downloads\MASH-bot-main\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 418, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "C:\Users\god\Downloads\MASH-bot-main\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 236, in process_updates
return await asyncio.gather(*tasks)
File "C:\Users\god\Downloads\MASH-bot-main\venv\lib\site-packages\aiogram\dispatcher\handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\god\Downloads\MASH-bot-main\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 257, in process_update
return await self.message_handlers.notify(update.message)
File "C:\Users\god\Downloads\MASH-bot-main\venv\lib\site-packages\aiogram\dispatcher\handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\god\Downloads\MASH-bot-main\#1\main.py", line 30, in test
type_of_test, test_id = link_to_test[0]
IndexError: list index out of range
ERROR:asyncio:Task exception was never retrieved
future: exception=IndexError('list index out of range')>
Traceback (most recent call last):
File "C:\Users\god\Downloads\MASH-bot-main\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 418, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "C:\Users\god\Downloads\MASH-bot-main\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 236, in process_updates
return await asyncio.gather(*tasks)
File "C:\Users\god\Downloads\MASH-bot-main\venv\lib\site-packages\aiogram\dispatcher\handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\god\Downloads\MASH-bot-main\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 257, in process_update
return await self.message_handlers.notify(update.message)
File "C:\Users\god\Downloads\MASH-bot-main\venv\lib\site-packages\aiogram\dispatcher\handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\god\Downloads\MASH-bot-main\#1\main.py", line 30, in test
type_of_test, test_id = link_to_test[0]
IndexError: list index out of range

main.py

logging.basicConfig(level=logging.INFO)

bot = Bot(token=options.token)
bot.parse_mode = 'html'
dp = Dispatcher(bot)

def get_answers(type_of_test, test_id):
url = 'https://uchebnik.mos.ru/exam/rest/secure/testplaye...'
payload = json.dumps(
{'test_type': 'training_test', 'generation_context_type': type_of_test, 'generation_by_id': test_id})
headers = {'Content-type': 'application/json', 'Cookie': options.cookie}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json()

@dp.message_handler()
async def test(message: types.Message):
link_to_test = re.findall(r'(selftest/spec|training_spec|test_by_binding)/([\w]+)', message.text)

type_of_test, test_id = link_to_test[0]
type_of_test = 'homework' if type_of_test == 'test_by_binding' else 'spec'

response = get_answers(type_of_test, test_id)
if 'error' in response:
return await message.answer("ERROR")

await message.answer(f'Номер теста {test_id}\nРешаем.. :P')

print(response)

for task in response['training_tasks']:
test_task = task['test_task']
question = test_task['question_elements'][0]['text']
type_of_question = test_task['answer']['type']
right_answer = test_task['answer']['right_answer']
if type_of_question == 'answer/number':
right_answer = right_answer['number']
elif type_of_question == 'answer/string':
right_answer = right_answer['string']
elif type_of_question == 'answer/single':
variants = test_task['answer']['options']
for i in variants:
if i['id'] in right_answer['id']:
right_answer = i['text']
break
else:
continue
await message.answer(f'{question}\n\nОТВЕТ: {right_answer}')

if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)

message.handler.py

import re
import requests
import json
import options

def get_answers(type_of_test, test_id):
url = 'https://uchebnik.mos.ru/exam/rest/secure/testplaye...'
payload = json.dumps(
{'test_type': 'training_test', 'generation_context_type': type_of_test, 'generation_by_id': test_id})
headers = {'Content-type': 'application/json', 'Cookie': options.cookie}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json()

async def answers(message_text, send):
link_to_test = re.findall(r'(selftest/spec|training_spec|test_by_binding)/([\w]+)', message_text)

type_of_test, test_id = link_to_test[0]
type_of_test = 'homework' if type_of_test == 'test_by_binding' else 'spec'

response = get_answers(type_of_test, test_id)
if 'error' in response:
return await send("ERROR")

await send(f'Номер теста {test_id}\nРешаем.. :P')

print(response)

for task in response['training_tasks']:
test_task = task['test_task']
question = test_task['question_elements'][0]['text']
type_of_question = test_task['answer']['type']
right_answer = test_task['answer']['right_answer']
if type_of_question == 'answer/number':
right_answer = right_answer['number']
elif type_of_question == 'answer/string':
right_answer = right_answer['string']
elif type_of_question == 'answer/single':
variants = test_task['answer']['options']
for i in variants:
if i['id'] in right_answer['id']:
right_answer = i['text']
break
else:
continue
await send(f'{question}\n\nОТВЕТ: {right_answer}')

async def unknown(send):
await send("Не понял")

async def handler(message_text, send):
if re.findall(r'(selftest/spec|training_spec|test_by_binding)/([\w]+)', message_text):
await answers(message_text, send)
else:
await unknown(send)

Заранее спасибо!
  • Вопрос задан
  • 165 просмотров
Решения вопроса 1
@Everything_is_bad
type_of_test, test_id = link_to_test[0]
IndexError: list index out of range
тебя явно указали проблему
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 2
Rsa97
@Rsa97
Для правильного вопроса надо знать половину ответа
В link_to_test нет элемента с индексом 0.
Ваш К.О.
Ответ написан
Комментировать
Eleksius
@Eleksius
Python (tkinter), c#
IndexError: list index out of range


Это значит, что вы пытаетесь вернуть элемент из списка с индексом, который больше самого списка.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы