[英]Why doesn't the function in the Aiogram FSM work?
我需要幫助,我想讓我的功能在狀態機中工作。 當我運行代碼時,它會進入“輸入檢查:”的時刻。 我進入,沒有任何反應。 沒有狀態機,一切正常,我希望函數接受消息文本,然后處理它並顯示結果。 提前致謝!
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram import types, Dispatcher
from create_bot import dp, bot
from func import function # this function itself
from keyboard import func_kb
async def start_command(message : types.Message):
await message.answer("Greetings! Select the function you need.", reply_markup=func_kb)
class Form(StatesGroup):
seturl = State()
@dp.message_handler(commands=['Test'])
async def test(message: types.Message):
await message.reply('Enter to check:')
await Form.seturl.set()
@dp.message_handler(state=Form.seturl)
async def monet(message : types.Message, state: FSMContext):
get_result = function(message.text) #get value from function
if get_result == "true":
await message.answer("Yes")
else:
await message.answer('Not')
await state.finish()
def register_handlers(dp : Dispatcher):
dp.register_message_handler(start_command, commands=['start'])
我嘗試在本地運行您的代碼,它對我有用。 我可以假設您沒有初始化處理程序或做錯了。 要么function
不返回任何內容,要么返回但不返回布爾值。
而且我還注意到你在 if 語句中寫了“true”,如果你的function
返回return True or False
,那么這很可能是一個錯誤。 你需要這樣寫:
if get_result:
await message.answer("Yes")
else:
await message.answer('Not')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.