Replies: 1 comment 5 replies
-
You can work directly with storage and set state for chats/users: aiogram/aiogram/contrib/fsm_storage/memory.py Lines 56 to 61 in cce29ba example: import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import StatesGroup, State
class Test(StatesGroup):
test = State()
async def on_startup(storage):
id_example = 999999999
await storage.set_state(user=id_example , chat=id_example , state=Test.test.state)
async def main():
bot = Bot("TOKEN")
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)
await on_startup(storage)
@dp.message_handler(state="*")
async def echo(msg: types.Message, state: FSMContext):
print(await state.get_state())
await msg.answer(f"Your state: {await state.get_state()}")
try:
await dp.start_polling()
finally:
await bot.session.close()
if __name__ == '__main__':
try:
asyncio.run(main())
except (KeyboardInterrupt, SystemExit):
pass Output:
|
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How do I initiate an FSM outside a message handler? E.g. in on_startup().
I would like to set the state for a user on startup but the error is:
ValueError: `user` or `chat` parameter is required but no one is provided!
And I can't even specify what user I want to set the state for.
Beta Was this translation helpful? Give feedback.
All reactions