ARTICLE AD BOX
I have a Telegram bot built with Spring Boot. It uses a router with a ConversationContext map to manage user state.
When a message comes i check the context:
if (context == null) { context = createNewContext(chatId, message); contexts.put(chatId, context); chatService.registerTelegramChat(chatId); }This works fine for most commands and even for plain text messages — the chat gets registered.
However, for commands like /skip and /cancel (when they are the first command from the user), the chat is NOT registered.
Logs show that context is null:
IS CONTEXT NULL true
But then I get an exception:
NoSuchFirstStateException: No such first state for command type: /skip
So it seems like createNewContext() fails before registerTelegramChat() is called.
My understanding is that /skip and /cancel are not "first" commands, so they don't have an initial state.
Questions:
1. What is the correct way to handle commands like /skip and /cancel when there is no existing context?
2. Should chat registration be decoupled from context initialization?
3. Is it better to assign a default state (like UNKNOWN or IDLE) for non-initial commands?
Architecture details:
- Spring Boot
- Custom router with CommandHandler registry
- ConversationState enum with "first" states
Any best practices for handling conversation state machines in Telegram bots would be appreciated.
