telega/flow/types

Shared type definitions for the flow system.

This module contains all types used across flow submodules.

Types

Composed step type for flow composition

pub type ComposedStep {
  ComposedFlowStep(Int)
  ComposedSelectFlow
  ComposedStartParallel
  ComposedParallelFlow(Int)
  ComposedMergeResults
}

Constructors

  • ComposedFlowStep(Int)
  • ComposedSelectFlow
  • ComposedStartParallel
  • ComposedParallelFlow(Int)
  • ComposedMergeResults
pub type ConditionalTransition(step_type) {
  ConditionalTransition(
    from: String,
    conditions: List(#(fn(FlowInstance) -> Bool, step_type)),
    default: step_type,
  )
}

Constructors

  • ConditionalTransition(
      from: String,
      conditions: List(#(fn(FlowInstance) -> Bool, step_type)),
      default: step_type,
    )

Built flow ready for execution

pub type Flow(step_type, session, error) {
  Flow(
    name: String,
    steps: dict.Dict(
      String,
      StepConfig(step_type, session, error),
    ),
    initial_step: step_type,
    step_to_string: fn(step_type) -> String,
    string_to_step: fn(String) -> Result(step_type, Nil),
    storage: FlowStorage(error),
    on_complete: option.Option(
      fn(bot.Context(session, error), FlowInstance) -> Result(
        bot.Context(session, error),
        error,
      ),
    ),
    on_error: option.Option(
      fn(
        bot.Context(session, error),
        FlowInstance,
        option.Option(error),
      ) -> Result(bot.Context(session, error), error),
    ),
    global_middlewares: List(
      fn(
        bot.Context(session, error),
        FlowInstance,
        fn() -> Result(
          #(
            bot.Context(session, error),
            FlowAction(step_type),
            FlowInstance,
          ),
          error,
        ),
      ) -> Result(
        #(
          bot.Context(session, error),
          FlowAction(step_type),
          FlowInstance,
        ),
        error,
      ),
    ),
    conditionals: List(ConditionalTransition(step_type)),
    parallel_configs: List(ParallelConfig(step_type)),
    subflows: List(SubflowConfig(step_type, session, error)),
    on_flow_enter: option.Option(
      fn(bot.Context(session, error), FlowInstance) -> Result(
        #(bot.Context(session, error), FlowInstance),
        error,
      ),
    ),
    on_flow_leave: option.Option(
      fn(bot.Context(session, error), FlowInstance) -> Result(
        #(bot.Context(session, error), FlowInstance),
        error,
      ),
    ),
    on_flow_exit: option.Option(
      fn(bot.Context(session, error), FlowInstance) -> Result(
        bot.Context(session, error),
        error,
      ),
    ),
    ttl_ms: option.Option(Int),
    on_timeout: option.Option(
      fn(bot.Context(session, error), FlowInstance) -> Result(
        bot.Context(session, error),
        error,
      ),
    ),
  )
}

Constructors

pub type FlowAction(step_type) {
  Next(step_type)
  NextString(String)
  Back
  Complete(dict.Dict(String, String))
  Cancel
  Wait
  WaitCallback
  WaitWithTimeout(timeout_ms: Int)
  WaitCallbackWithTimeout(timeout_ms: Int)
  GoTo(step_type)
  Exit(option.Option(dict.Dict(String, String)))
  ReturnFromSubflow(result: dict.Dict(String, String))
  StartParallel(steps: List(step_type), join_at: step_type)
  CompleteParallelStep(
    step: step_type,
    result: dict.Dict(String, String),
  )
  EnterSubflow(
    subflow_name: String,
    data: dict.Dict(String, String),
  )
}

Constructors

  • Next(step_type)

    Move to the next step

  • NextString(String)

    Move to next step by string name (for dynamic navigation)

  • Back

    Go back to previous step

  • Complete(dict.Dict(String, String))

    Complete the flow with data

  • Cancel

    Cancel the flow

  • Wait

    Wait for user input

  • WaitCallback

    Wait for callback query

  • WaitWithTimeout(timeout_ms: Int)

    Wait for user input with timeout

  • WaitCallbackWithTimeout(timeout_ms: Int)

    Wait for callback query with timeout

  • GoTo(step_type)

    Jump to any step (clears step data)

  • Exit(option.Option(dict.Dict(String, String)))

    Exit flow with result

  • ReturnFromSubflow(result: dict.Dict(String, String))

    Return from subflow

  • StartParallel(steps: List(step_type), join_at: step_type)

    Start parallel execution

  • CompleteParallelStep(
      step: step_type,
      result: dict.Dict(String, String),
    )

    Complete a parallel step

  • EnterSubflow(
      subflow_name: String,
      data: dict.Dict(String, String),
    )

    Enter a subflow (pushes current state to stack)

Hook called when entering a flow (from trigger or from parent flow)

pub type FlowEnterHook(session, error) =
  fn(bot.Context(session, error), FlowInstance) -> Result(
    #(bot.Context(session, error), FlowInstance),
    error,
  )

Hook called when completely exiting the flow (cancel or complete)

pub type FlowExitHook(session, error) =
  fn(bot.Context(session, error), FlowInstance) -> Result(
    bot.Context(session, error),
    error,
  )

Persistent flow instance with unique ID

pub type FlowInstance {
  FlowInstance(
    id: String,
    flow_name: String,
    user_id: Int,
    chat_id: Int,
    state: @internal FlowState,
    step_data: dict.Dict(String, String),
    wait_token: option.Option(String),
    wait_timeout_at: option.Option(Int),
    created_at: Int,
    updated_at: Int,
  )
}

Constructors

  • FlowInstance(
      id: String,
      flow_name: String,
      user_id: Int,
      chat_id: Int,
      state: @internal FlowState,
      step_data: dict.Dict(String, String),
      wait_token: option.Option(String),
      wait_timeout_at: option.Option(Int),
      created_at: Int,
      updated_at: Int,
    )

Flat representation of a FlowInstance for database serialization

pub type FlowInstanceRow {
  FlowInstanceRow(
    id: String,
    flow_name: String,
    user_id: Int,
    chat_id: Int,
    current_step: String,
    data: dict.Dict(String, String),
    step_data: dict.Dict(String, String),
    wait_token: option.Option(String),
    wait_timeout_at: option.Option(Int),
    created_at: Int,
    updated_at: Int,
  )
}

Constructors

  • FlowInstanceRow(
      id: String,
      flow_name: String,
      user_id: Int,
      chat_id: Int,
      current_step: String,
      data: dict.Dict(String, String),
      step_data: dict.Dict(String, String),
      wait_token: option.Option(String),
      wait_timeout_at: option.Option(Int),
      created_at: Int,
      updated_at: Int,
    )

Hook called when leaving a flow to enter a subflow

pub type FlowLeaveHook(session, error) =
  fn(bot.Context(session, error), FlowInstance) -> Result(
    #(bot.Context(session, error), FlowInstance),
    error,
  )

Storage interface for persistent flows

pub type FlowStorage(error) {
  FlowStorage(
    save: fn(FlowInstance) -> Result(Nil, error),
    load: fn(String) -> Result(option.Option(FlowInstance), error),
    delete: fn(String) -> Result(Nil, error),
    list_by_user: fn(Int, Int) -> Result(
      List(FlowInstance),
      error,
    ),
  )
}

Constructors

Trigger type for flow registration

pub type FlowTrigger {
  OnCommand(command: String)
  OnText(pattern: router.Pattern)
  OnCallback(pattern: router.Pattern)
  OnFiltered(filter: router.Filter)
  OnPhoto
  OnVideo
  OnAudio
  OnVoice
  OnAnyText
}

Constructors

Inline subflow step type (uses strings internally)

pub type InlineStep {
  InlineStep(name: String)
}

Constructors

  • InlineStep(name: String)
pub type ParallelConfig(step_type) {
  ParallelConfig(
    trigger_step: String,
    parallel_steps: List(step_type),
    join_step: step_type,
  )
}

Constructors

  • ParallelConfig(
      trigger_step: String,
      parallel_steps: List(step_type),
      join_step: step_type,
    )
pub type StepConfig(step_type, session, error) {
  StepConfig(
    handler: fn(bot.Context(session, error), FlowInstance) -> Result(
      #(
        bot.Context(session, error),
        FlowAction(step_type),
        FlowInstance,
      ),
      error,
    ),
    middlewares: List(
      fn(
        bot.Context(session, error),
        FlowInstance,
        fn() -> Result(
          #(
            bot.Context(session, error),
            FlowAction(step_type),
            FlowInstance,
          ),
          error,
        ),
      ) -> Result(
        #(
          bot.Context(session, error),
          FlowAction(step_type),
          FlowInstance,
        ),
        error,
      ),
    ),
    on_enter: option.Option(
      fn(bot.Context(session, error), FlowInstance) -> Result(
        #(bot.Context(session, error), FlowInstance),
        error,
      ),
    ),
    on_leave: option.Option(
      fn(bot.Context(session, error), FlowInstance) -> Result(
        #(bot.Context(session, error), FlowInstance),
        error,
      ),
    ),
  )
}

Constructors

Hook called when entering a step (before the handler)

pub type StepEnterHook(session, error) =
  fn(bot.Context(session, error), FlowInstance) -> Result(
    #(bot.Context(session, error), FlowInstance),
    error,
  )
pub type StepHandler(step_type, session, error) =
  fn(bot.Context(session, error), FlowInstance) -> Result(
    #(
      bot.Context(session, error),
      FlowAction(step_type),
      FlowInstance,
    ),
    error,
  )

Hook called when leaving a step (after the handler, before transition)

pub type StepLeaveHook(session, error) =
  fn(bot.Context(session, error), FlowInstance) -> Result(
    #(bot.Context(session, error), FlowInstance),
    error,
  )
pub type StepMiddleware(step_type, session, error) =
  fn(
    bot.Context(session, error),
    FlowInstance,
    fn() -> Result(
      #(
        bot.Context(session, error),
        FlowAction(step_type),
        FlowInstance,
      ),
      error,
    ),
  ) -> Result(
    #(
      bot.Context(session, error),
      FlowAction(step_type),
      FlowInstance,
    ),
    error,
  )

Result type for flow steps

pub type StepResult(step_type, session, error) =
  Result(
    #(
      bot.Context(session, error),
      FlowAction(step_type),
      FlowInstance,
    ),
    error,
  )

Sub-flow configuration

pub type SubflowConfig(step_type, session, error) {
  SubflowConfig(
    trigger_step: String,
    flow: Flow(dynamic.Dynamic, session, error),
    return_step: step_type,
    map_args: fn(FlowInstance) -> dict.Dict(String, String),
    map_result: fn(dict.Dict(String, String), FlowInstance) -> FlowInstance,
  )
}

Constructors

Result of waiting for user input or callback

pub type WaitResult {
  TextInput(value: String)
  BoolCallback(value: Bool)
  DataCallback(value: String)
  PhotoInput(file_ids: List(String))
  VideoInput(file_id: String)
  VoiceInput(file_id: String)
  AudioInput(file_id: String)
  LocationInput(latitude: Float, longitude: Float)
  CommandInput(command: String, payload: String)
  Pending
}

Constructors

  • TextInput(value: String)

    Text message from user

  • BoolCallback(value: Bool)

    Yes/No callback button press

  • DataCallback(value: String)

    Other callback data (non-boolean)

  • PhotoInput(file_ids: List(String))

    Photo message (list of file_ids for different sizes)

  • VideoInput(file_id: String)

    Video message

  • VoiceInput(file_id: String)

    Voice message

  • AudioInput(file_id: String)

    Audio message

  • LocationInput(latitude: Float, longitude: Float)

    Location message

  • CommandInput(command: String, payload: String)

    Command message

  • Pending

    No input yet (first visit to step)

Search Document