Wizard is a special type of Dispatcher that can be used to simplify implementing step-by-step scenes.

Type Parameters

  • State extends object

Hierarchy (View Summary)

Constructors

Properties

child: never

Create a new child dispatcher.

for: never

Create a new dispatcher and bind it to the client.

scene: never

Create a new scene dispatcher

Accessors

  • get parent(): null | Dispatcher<any>
  • Get parent dispatcher if current dispatcher is a child. Otherwise, return null

    Returns null | Dispatcher<any>

  • get sceneName(): undefined | string
  • For scene dispatchers, name of the scene

    Returns undefined | string

  • get totalSteps(): number
  • Get the total number of registered steps

    Returns number

Methods

  • Add a child dispatcher.

    Child dispatchers are called when dispatching updates just like normal, except they can be controlled externally. Additionally, child dispatcher have their own independent handler grouping that does not interfere with parent's, including StopPropagation (i.e. returning StopPropagation will still call children. To entirely stop, use StopChildrenPropagation)

    Note that child dispatchers share the same TelegramClient and storage binding as the parent, don't bind them manually.

    Parameters

    Returns void

  • Add a dispatcher as a scene with a non-scoped state.

    Scoped storage for a scene means that the scene will have its own storage, that is only available within the scene and does not interfere with global state. Non-scoped, on the other hand, is the same state as the one used for the root dispatcher

    Parameters

    • scene: Dispatcher<State & WizardInternalState>

      Dispatcher representing the scene

    • scoped: false

      Whether to use scoped FSM storage for the scene

    Returns void

  • Add a dispatcher as a scene with a scoped state

    Scoped storage for a scene means that the scene will have its own storage, that is only available within the scene and does not interfere with global state. Non-scoped, on the other hand, is the same state as the one used for the root dispatcher

    Parameters

    • scene: Dispatcher<any>

      Dispatcher representing the scene

    • Optionalscoped: true

      Whether to use scoped FSM storage for the scene (defaults to true)

    Returns void

  • Add an update handler to a given handlers group

    Parameters

    • handler: UpdateHandler

      Update handler

    • group: number = 0

      Handler group index

    Returns void

  • Bind the dispatcher to the client. Called by the constructor automatically if client was passed.

    Dispatcher also uses bound client to throw errors

    Parameters

    Returns void

  • Create a clone of this dispatcher, that has the same handlers, but is not bound to a client or to a parent dispatcher.

    Custom Storage and key delegate are copied too.

    By default, child dispatchers (and scenes) are ignored, since that requires cloning every single one of them recursively and then binding them back.

    Parameters

    • children: boolean = false

      Whether to also clone children and scenes

    Returns Dispatcher<State & WizardInternalState>

  • Destroy the dispatcher and all its children.

    When destroying, all the registered handlers are removed, and the underlying storage is freed.

    Returns Promise<void>

  • Process a raw update with this dispatcher. Calling this method without bound client will not work.

    Under the hood asynchronously calls dispatchRawUpdateNow with error handler set to client's one.

    Parameters

    Returns void

  • Process a raw update right now in the current stack.

    Unlike dispatchRawUpdate, this does not schedule the update to be dispatched, but dispatches it immediately, and after awaiting this method you can be certain that the update was fully processed by all the registered handlers, including children.

    Parameters

    Returns Promise<boolean>

    Whether the update was handled

  • Process an update with this dispatcher. Calling this method without bound client will not work.

    Under the hood asynchronously calls dispatchUpdateNow with error handler set to client's one.

    Parameters

    Returns void

  • Process an update right now in the current stack.

    Unlike dispatchUpdate, this does not schedule the update to be dispatched, but dispatches it immediately, and after awaiting this method you can be certain that the update was fully processed by all the registered handlers, including children.

    Parameters

    Returns Promise<boolean>

    Whether the update was handled

  • Extend current dispatcher by copying other dispatcher's handlers and children to the current.

    This might be more efficient for simple cases, but do note that the handler groups, children and scenes will get merged (unlike addChild, where they are independent). Also note that unlike with children, when adding handlers to other after you extended the current dispatcher, changes will not be applied.

    Parameters

    Returns void

  • Get update state object for the given key.

    For custom keys, use prefix starting with $ to avoid clashing with other keys (scene name can't start with $)

    Type Parameters

    • S extends object = State & WizardInternalState

      State type, defaults to dispatcher's state type. Only checked at compile-time

    Parameters

    • key: string

      State storage key

    Returns UpdateState<S>

  • Get update state object for the given object.

    Equivalent to getState(string), but derives the key with the registered StateKeyDelegate, and since it could be async, this method is async too.

    Type Parameters

    • S extends object = State & WizardInternalState

      State type, defaults to dispatcher's state type. Only checked at compile-time

    Parameters

    Returns Promise<UpdateState<S>>

  • Go to the Nth step

    Parameters

    Returns Promise<void>

  • Inject a dependency to be available in this dispatcher and all its children.

    Note: This is only available for the root dispatcher.

    Type Parameters

    • Name extends never

    Parameters

    Returns void

  • Inject dependencies to be available in this dispatcher and all its children.

    Note: This is only available for the root dispatcher.

    Parameters

    Returns void

  • Filter that will only pass if the current step is the one after last one added

    Returns filters.UpdateFilter<any, {}, WizardInternalState>

  • Register an error handler.

    This is used locally within this dispatcher (does not affect children/parent) whenever an error is thrown inside an update handler. Not used for raw update handlers

    When an error is thrown, but there is no error handler, it is propagated to TelegramClient.

    There can be at most one error handler. Pass null to remove it.

    Type Parameters

    • T = {}

    Parameters

    • handler:
          | null
          | (
              err: Error,
              update: ParsedUpdate & T,
              state?: UpdateState<State & WizardInternalState>,
          ) => MaybePromise<boolean>

      Error handler

    Returns void

  • Register post-update middleware.

    This is used locally within this dispatcher (does not affect children/parent) after successfully processing an update, and can be used for stats.

    There can be at most one post-update middleware. Pass null to remove it.

    Type Parameters

    • T = {}

    Parameters

    • handler:
          | null
          | (
              handled: boolean,
              update: ParsedUpdate & T,
              state?: UpdateState<State & WizardInternalState>,
          ) => MaybePromise<void>

      Pre-update middleware

    Returns void

  • Register pre-update middleware.

    This is used locally within this dispatcher (does not affect children/parent) before processing an update, and can be used to skip this update.

    There can be at most one pre-update middleware. Pass null to remove it.

    Type Parameters

    • T = {}

    Parameters

    Returns void

  • Register a scene transition handler

    This handler is called whenever a scene transition occurs in the context of the scene that is being entered, and before any of the its own handlers are called, and can be used to customize the transition behavior:

    • Stop to prevent dispatching the update any further even if ToScene/ToRoot was used
    • Continue same as Stop, but still dispatch the update to children
    • ToScene to prevent the transition and dispatch the update to the scene entered in the transition handler

    Note: if multiple state.enter() calls were made within the same update, this handler will only be called for the last one.

    Parameters

    Returns void

  • Remove a child dispatcher.

    Removing child dispatcher will also remove child dispatcher's client binding.

    If the provided dispatcher is not a child of current, this function will silently fail.

    Parameters

    Returns void

  • Remove an update handler (or handlers) from a given handler group.

    Parameters

    • handler:
          | "poll"
          | "story"
          | "raw"
          | "new_message"
          | "edit_message"
          | "message_group"
          | "delete_message"
          | "chat_member"
          | "inline_query"
          | "chosen_inline_result"
          | "callback_query"
          | "inline_callback_query"
          | "business_callback_query"
          | "poll_vote"
          | "user_status"
          | "user_typing"
          | "history_read"
          | "bot_stopped"
          | "bot_chat_join_request"
          | "chat_join_request"
          | "pre_checkout_query"
          | "delete_story"
          | "bot_reaction"
          | "bot_reaction_count"
          | "business_connection"
          | "new_business_message"
          | "edit_business_message"
          | "business_message_group"
          | "delete_business_message"
          | "all"
          | UpdateHandler

      Update handler to remove, its name or 'all' to remove all

    • group: null | number = 0

      Handler group index (null to affect all groups)

    Returns void

  • Parameters

    Returns void

  • Skip N steps

    Parameters

    • state: UpdateState<WizardInternalState>
    • count: number = 1

    Returns Promise<void>

  • Unbind a dispatcher from the client.

    Returns void

  • Filter that will only pass if the current step is step

    Parameters

    • step: number

    Returns filters.UpdateFilter<any, {}, WizardInternalState>