как получить и использовать текущее состояние в фильтре в aiogram 3
в данном фильтре мне нужно сравнивать текущее машинное состояние с состоянием из списка но почему то не один из операторов сравнения не отрабатывает хотя состояние совпадает
class IsIHAveThisCode(BaseFilter):
async def __call__(self, message: Message, state:FSMContext) -> bool:
print(await state.get_state())
if await state.get_state() == languages.PYTHON_HAVE_BEEN_GOT:
files = os.listdir(path='python_files')
max_file = len(files)
try:
for i in range(1, max_file):
if str(message) == str(i):
return True
else:
return False
except:
return False
elif await state.get_state() == languages.CSHARP_HAVE_BEEN_GOT:
files = os.listdir(path='c#_files')
max_file = len(files)
try:
for i in range(1, max_file):
if str(message) == str(i):
return True
else:
return False
except:
return False
elif await state.get_state() is None:
return False
Ответы (1 шт):
Автор решения: HAYL
→ Ссылка
всё я разобрался, кому нужно вот рабочий вариант
class IsIHAveThisCode(BaseFilter):
async def __call__(self, message: Message, state:FSMContext) -> bool:
if await state.get_state() == languages.PYTHON_HAVE_BEEN_GOT:
print(await state.get_state())
files = os.listdir(path='python_files')
max_file = len(files)
try:
for i in range(1, max_file + 1):
if str(message.text) == str(i):
return True
except:
return False
elif await state.get_state() == languages.CSHARP_HAVE_BEEN_GOT:
print(await state.get_state())
files = os.listdir(path='c#_files')
max_file = len(files)
try:
for i in range(1, max_file + 1):
if str(message.text) == str(i):
return True
except:
return False
elif await state.get_state() is None:
print(await state.get_state())
return False
ошибки: 1 функцию get_state у обьекта FSMcontext нужно вызывать с помощью await
2 в строке if str(message) == str(i): нужно обращаться не к message а к message.text 3 нужно убрать строку else: return False в цикле