telega/flow/builder

FlowBuilder and all builder functions for constructing flows.

Types

pub opaque type FlowBuilder(step_type, session, error)

Values

pub fn add_conditional(
  builder: FlowBuilder(step_type, session, error),
  from: step_type,
  condition: fn(types.FlowInstance) -> Bool,
  true on_true: step_type,
  false on_false: step_type,
) -> FlowBuilder(step_type, session, error)

Add conditional transition

pub fn add_global_middleware(
  builder: FlowBuilder(step_type, session, error),
  middleware: fn(
    bot.Context(session, error),
    types.FlowInstance,
    fn() -> Result(
      #(
        bot.Context(session, error),
        types.FlowAction(step_type),
        types.FlowInstance,
      ),
      error,
    ),
  ) -> Result(
    #(
      bot.Context(session, error),
      types.FlowAction(step_type),
      types.FlowInstance,
    ),
    error,
  ),
) -> FlowBuilder(step_type, session, error)

Add global middleware that applies to all steps

pub fn add_multi_conditional(
  builder: FlowBuilder(step_type, session, error),
  from: step_type,
  conditions: List(#(fn(types.FlowInstance) -> Bool, step_type)),
  default: step_type,
) -> FlowBuilder(step_type, session, error)

Add multi-way conditional

pub fn add_parallel_steps(
  builder: FlowBuilder(step_type, session, error),
  trigger_step: step_type,
  parallel_steps: List(step_type),
  join_at: step_type,
) -> FlowBuilder(step_type, session, error)

Add parallel step execution.

@deprecated Use parallel() instead for cleaner API.

pub fn add_step(
  builder: FlowBuilder(step_type, session, error),
  step: step_type,
  handler: fn(bot.Context(session, error), types.FlowInstance) -> Result(
    #(
      bot.Context(session, error),
      types.FlowAction(step_type),
      types.FlowInstance,
    ),
    error,
  ),
) -> FlowBuilder(step_type, session, error)

Add a step to the flow

pub fn add_step_with_hooks(
  builder: FlowBuilder(step_type, session, error),
  step: step_type,
  handler handler: fn(
    bot.Context(session, error),
    types.FlowInstance,
  ) -> Result(
    #(
      bot.Context(session, error),
      types.FlowAction(step_type),
      types.FlowInstance,
    ),
    error,
  ),
  on_enter on_enter: option.Option(
    fn(bot.Context(session, error), types.FlowInstance) -> Result(
      #(bot.Context(session, error), types.FlowInstance),
      error,
    ),
  ),
  on_leave on_leave: option.Option(
    fn(bot.Context(session, error), types.FlowInstance) -> Result(
      #(bot.Context(session, error), types.FlowInstance),
      error,
    ),
  ),
) -> FlowBuilder(step_type, session, error)

Add a step with lifecycle hooks

pub fn add_step_with_hooks_and_middleware(
  builder: FlowBuilder(step_type, session, error),
  step: step_type,
  handler handler: fn(
    bot.Context(session, error),
    types.FlowInstance,
  ) -> Result(
    #(
      bot.Context(session, error),
      types.FlowAction(step_type),
      types.FlowInstance,
    ),
    error,
  ),
  middlewares middlewares: List(
    fn(
      bot.Context(session, error),
      types.FlowInstance,
      fn() -> Result(
        #(
          bot.Context(session, error),
          types.FlowAction(step_type),
          types.FlowInstance,
        ),
        error,
      ),
    ) -> Result(
      #(
        bot.Context(session, error),
        types.FlowAction(step_type),
        types.FlowInstance,
      ),
      error,
    ),
  ),
  on_enter on_enter: option.Option(
    fn(bot.Context(session, error), types.FlowInstance) -> Result(
      #(bot.Context(session, error), types.FlowInstance),
      error,
    ),
  ),
  on_leave on_leave: option.Option(
    fn(bot.Context(session, error), types.FlowInstance) -> Result(
      #(bot.Context(session, error), types.FlowInstance),
      error,
    ),
  ),
) -> FlowBuilder(step_type, session, error)

Add a step with hooks and middleware

pub fn add_step_with_middleware(
  builder: FlowBuilder(step_type, session, error),
  step: step_type,
  middlewares: List(
    fn(
      bot.Context(session, error),
      types.FlowInstance,
      fn() -> Result(
        #(
          bot.Context(session, error),
          types.FlowAction(step_type),
          types.FlowInstance,
        ),
        error,
      ),
    ) -> Result(
      #(
        bot.Context(session, error),
        types.FlowAction(step_type),
        types.FlowInstance,
      ),
      error,
    ),
  ),
  handler: fn(bot.Context(session, error), types.FlowInstance) -> Result(
    #(
      bot.Context(session, error),
      types.FlowAction(step_type),
      types.FlowInstance,
    ),
    error,
  ),
) -> FlowBuilder(step_type, session, error)

Add a step with middleware

pub fn add_subflow(
  builder: FlowBuilder(step_type, session, error),
  trigger_step: step_type,
  subflow subflow: types.Flow(dynamic.Dynamic, session, error),
  return_to return_to: step_type,
  map_args map_args: fn(types.FlowInstance) -> dict.Dict(
    String,
    String,
  ),
  map_result map_result: fn(
    dict.Dict(String, String),
    types.FlowInstance,
  ) -> types.FlowInstance,
) -> FlowBuilder(step_type, session, error)

Add sub-flow

pub fn build(
  builder: FlowBuilder(step_type, session, error),
  initial initial_step: step_type,
) -> types.Flow(step_type, session, error)

Build the flow

pub fn inline_next(
  ctx: bot.Context(session, error),
  instance: types.FlowInstance,
  step_name step_name: String,
) -> Result(
  #(
    bot.Context(session, error),
    types.FlowAction(types.InlineStep),
    types.FlowInstance,
  ),
  error,
)

Navigate to next inline step by name

pub fn new(
  flow_name: String,
  storage: types.FlowStorage(error),
  step_to_string: fn(step_type) -> String,
  string_to_step: fn(String) -> Result(step_type, Nil),
) -> FlowBuilder(step_type, session, error)

Create a new flow builder

pub fn new_with_default_converters(
  flow_name: String,
  storage: types.FlowStorage(error),
  steps: List(#(String, step_type)),
) -> FlowBuilder(step_type, session, error)

Create a flow builder with default string conversion (uses string.inspect)

pub fn on_complete(
  builder: FlowBuilder(step_type, session, error),
  handler: fn(bot.Context(session, error), types.FlowInstance) -> Result(
    bot.Context(session, error),
    error,
  ),
) -> FlowBuilder(step_type, session, error)

Set completion handler

pub fn on_error(
  builder: FlowBuilder(step_type, session, error),
  handler: fn(
    bot.Context(session, error),
    types.FlowInstance,
    option.Option(error),
  ) -> Result(bot.Context(session, error), error),
) -> FlowBuilder(step_type, session, error)

Set error handler

pub fn on_timeout(
  builder: FlowBuilder(step_type, session, error),
  handler: fn(bot.Context(session, error), types.FlowInstance) -> Result(
    bot.Context(session, error),
    error,
  ),
) -> FlowBuilder(step_type, session, error)

Set timeout hook (called when flow or wait expires via lazy check)

pub fn parallel(
  builder: FlowBuilder(step_type, session, error),
  from from: step_type,
  steps steps: List(step_type),
  join join: step_type,
) -> FlowBuilder(step_type, session, error)

Add parallel step execution (simplified API).

pub fn set_on_flow_enter(
  builder: FlowBuilder(step_type, session, error),
  hook: fn(bot.Context(session, error), types.FlowInstance) -> Result(
    #(bot.Context(session, error), types.FlowInstance),
    error,
  ),
) -> FlowBuilder(step_type, session, error)

Set flow enter hook

pub fn set_on_flow_exit(
  builder: FlowBuilder(step_type, session, error),
  hook: fn(bot.Context(session, error), types.FlowInstance) -> Result(
    bot.Context(session, error),
    error,
  ),
) -> FlowBuilder(step_type, session, error)

Set flow exit hook

pub fn set_on_flow_leave(
  builder: FlowBuilder(step_type, session, error),
  hook: fn(bot.Context(session, error), types.FlowInstance) -> Result(
    #(bot.Context(session, error), types.FlowInstance),
    error,
  ),
) -> FlowBuilder(step_type, session, error)

Set flow leave hook

pub fn with_inline_subflow(
  builder: FlowBuilder(step_type, session, error),
  name name: String,
  trigger trigger: step_type,
  return_to return_to: step_type,
  initial initial: String,
  steps steps: List(
    #(
      String,
      fn(bot.Context(session, error), types.FlowInstance) -> Result(
        #(
          bot.Context(session, error),
          types.FlowAction(types.InlineStep),
          types.FlowInstance,
        ),
        error,
      ),
    ),
  ),
) -> FlowBuilder(step_type, session, error)

Add an inline subflow defined within the parent flow

pub fn with_inline_subflow_mapped(
  builder: FlowBuilder(step_type, session, error),
  name name: String,
  trigger trigger: step_type,
  return_to return_to: step_type,
  initial initial: String,
  steps steps: List(
    #(
      String,
      fn(bot.Context(session, error), types.FlowInstance) -> Result(
        #(
          bot.Context(session, error),
          types.FlowAction(types.InlineStep),
          types.FlowInstance,
        ),
        error,
      ),
    ),
  ),
  map_args map_args: fn(types.FlowInstance) -> dict.Dict(
    String,
    String,
  ),
  map_result map_result: fn(
    dict.Dict(String, String),
    types.FlowInstance,
  ) -> types.FlowInstance,
) -> FlowBuilder(step_type, session, error)

Add an inline subflow with custom argument and result mapping

pub fn with_ttl(
  builder: FlowBuilder(step_type, session, error),
  ms ms: Int,
) -> FlowBuilder(step_type, session, error)

Set flow-level TTL (maximum lifetime in milliseconds)

Search Document