0

I'm creating a chat bot that gives you an example in math in Python using the Aiogram library. It works like this, when you write the /start command, it offers to choose 3 modes: multiplication, division and all at once (1,2,3). Then it offers how many digits you need. Then he asks you an example, for example 121 * 121. If you answer incorrectly, for example 15 641, then you lost, if 14 641, you won. So far I have only inserted the first mode. I have the second and third ready, but I will insert them at the very last moment.

When I tested the bot, there was a problem with the wait_count state (number of digits). In the console, he does not write me any errors, only a warning:

/home/user/Documents/library/projects/python/trener/main.py:31: FSMStorageWarning: You haven’t set any storage yet so no states and no data will be saved.
You can connect Memory Storage for debug purposes or non-essential data.
await state.update_data(type=int(sms.text))
/home/user/Documents/library/projects/python/trener/main.py:33: FSMStorageWarning: You haven’t set any storage yet so no states and no data will be saved.
You can connect Memory Storage for debug purposes or non-essential data.
await state.set_state(number.wait_count.state)

Despite the warning, I switched to the bot. And when I had already reached the wait_count state, after I entered 2, he reset the state to me and asked the same question again. I tried to delete lines 40-45 and leave 43 (temporarily), but it still doesn't come out (I thought it just can't take the information out of the state). I also tried adding MemoryStorage (as in the warning), then it didn't respond at all. Help me, please. Here is the code:

import random
from aiogram import Bot,Dispatcher,executor,types
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup

bot = Bot("...")
dp = Dispatcher(bot=bot)

def umn(ras):
    n1=random.randint(int("1"+"0"*(ras-1)),int("10"+"0"*(ras-1)))
    n2=random.randint(int("1"+"0"*(ras-1)),int("10"+"0"*(ras-1)))
    inp = str(n1)+"*"+str(n2)
    rest = n1*n2
    return inp,rest
class number(StatesGroup):
    wait_type = State()
    wait_count = State()
    wait_answer = State()
@dp.message_handler(commands=['start'])
async def start(mes:types.Message,state:FSMContext):
    await bot.send_message(mes.from_user.id,"""Welcome to the Mission Impossible math game. Here you can train your brains for multiplication, addition, subtraction. Select the mode:
Enter 1 for multiplication.
Enter 2 for addition - subtraction.
Enter 3 for the unrealistically difficult mode all in the mix""")
    await state.set_state(number.wait_type.state)
@dp.message_handler()
async def type(mes: types.Message,state: FSMContext):
    if mes.text=="":
        await mes.answer("The field is empty, try again:")
        return
    await state.update_data(type=int(mes.text))
    await mes.answer("How many digits do you need for the first time? ")
    await state.set_state(number.wait_count.state)
@dp.message_handler()
async def count(mes: types.Message,state: FSMContext):
    if mes.text=="":
        await mes.answer("The field is empty, try again")
        return
    await state.update_data(count=int(mes.text))
    global inp
    user_type = state.get_data()["type"]
    if user_type == 1:
        inp = umn(mes.text)
    else:
        inp= "Sorry"
    await mes.answer(inp[0])
    await state.set_state(number.wait_answer.state)
@dp.message_handler()
async def answer(mes: types.Message,state: FSMContext):
    if mes.text=="":
        await mes.answer("The field is empty, try again")
        return
    if int(mes.text)==inp[1]:
        await mes.answer("T")
    else:
        await mes.answer("F")
    await state.finish()
if __name__ == '__main__':
    executor.start_polling(dp)

1 Answers1

0

To use states, you need to add the following line to your code

from aiogram.contrib.fsm_storage.memory import MemoryStorage # Aiogram 2.x

....

storage = MemoryStorage()
bot = Bot(token="TOKEN", parse_mode='HTML')
dp = Dispatcher(bot, storage=storage)

....

official example is here