Interface BotChatStateService


public interface BotChatStateService
Service interface for managing per-chat conversational state. Implementations persist and retrieve a string-based state token for each chat, enabling multi-step conversation flows that are conditionally activated via BotChatState.

Enum-friendly overloads are provided so callers can pass enum constants directly and receive IDE autocompletion, instead of writing raw strings:

// Instead of: chatStateService.setState(chatId, "WAITING_FOR_NAME")
chatStateService.setState(chatId, MyState.WAITING_FOR_NAME);

// Instead of: MyState.valueOf(chatStateService.getState(chatId))
MyState state = chatStateService.getStateAs(chatId, MyState.class);
Since:
0.0.1
Author:
Islom Mirsaburov
  • Method Summary

    Modifier and Type
    Method
    Description
    getState(Long chatId)
    Retrieves the current state for the specified chat.
    default <T extends Enum<T>>
    T
    getStateAs(Long chatId, Class<T> type)
    Retrieves the current state for the specified chat and parses it as an enum constant of the given type.
    default void
    setState(Long chatId, Enum<?> state)
    Stores the enum constant's Enum.name() as the state for the specified chat.
    void
    setState(Long chatId, String state)
    Stores or updates the state for the specified chat.
  • Method Details

    • getState

      String getState(Long chatId)
      Retrieves the current state for the specified chat.
      Parameters:
      chatId - the unique Telegram chat identifier; must not be null
      Returns:
      the current state string for the chat, or null if no state has been set
    • setState

      void setState(Long chatId, String state)
      Stores or updates the state for the specified chat.
      Parameters:
      chatId - the unique Telegram chat identifier; must not be null
      state - the new state value to persist; may be null to clear the state
    • setState

      default void setState(Long chatId, Enum<?> state)
      Stores the enum constant's Enum.name() as the state for the specified chat. This overload enables IDE autocompletion when callers work with enum-defined states.
      Parameters:
      chatId - the unique Telegram chat identifier; must not be null
      state - the enum constant whose name is persisted; must not be null
    • getStateAs

      default <T extends Enum<T>> T getStateAs(Long chatId, Class<T> type)
      Retrieves the current state for the specified chat and parses it as an enum constant of the given type. Returns null if no state is set.
      MyState current = chatStateService.getStateAs(chatId, MyState.class);
      
      Type Parameters:
      T - the enum type
      Parameters:
      chatId - the unique Telegram chat identifier; must not be null
      type - the enum class to parse the stored string into; must not be null
      Returns:
      the matching enum constant, or null if no state has been set
      Throws:
      IllegalArgumentException - if the stored string does not match any constant of type