telega/flow/handler

Built-in step handlers and the text resume handler.

Values

pub fn create_text_handler(
  flow: types.Flow(step_type, session, error, dependencies),
) -> fn(
  context.Context(session, error, dependencies),
  update.Update,
) -> Result(context.Context(session, error, dependencies), error)

Create a text handler for resuming flows

pub fn message_step(
  message_fn: fn(types.FlowInstance) -> String,
  next_step: option.Option(step_type),
) -> fn(
  context.Context(session, error, dependencies),
  types.FlowInstance,
) -> Result(
  #(
    context.Context(session, error, dependencies),
    types.FlowAction(step_type),
    types.FlowInstance,
  ),
  error,
)

Create a message display step

pub fn message_step_with(
  message_fn message_fn: fn(
    context.Context(session, error, dependencies),
    types.FlowInstance,
  ) -> String,
  next_step next_step: option.Option(step_type),
) -> fn(
  context.Context(session, error, dependencies),
  types.FlowInstance,
) -> Result(
  #(
    context.Context(session, error, dependencies),
    types.FlowAction(step_type),
    types.FlowInstance,
  ),
  error,
)

Like message_step, but the message is computed from the Context (in addition to the flow instance), enabling localization.

builder.add_step(
  Welcome,
  handler.message_step_with(
    fn(ctx, _instance) { i18n.t(ctx, "book.welcome", []) },
    option.Some(Date),
  ),
)
pub fn text_step(
  prompt: String,
  data_key: String,
  next_step: step_type,
) -> fn(
  context.Context(session, error, dependencies),
  types.FlowInstance,
) -> Result(
  #(
    context.Context(session, error, dependencies),
    types.FlowAction(step_type),
    types.FlowInstance,
  ),
  error,
)

Create a text input step

pub fn text_step_with(
  prompt prompt: fn(
    context.Context(session, error, dependencies),
    types.FlowInstance,
  ) -> String,
  data_key data_key: String,
  next_step next_step: step_type,
) -> fn(
  context.Context(session, error, dependencies),
  types.FlowInstance,
) -> Result(
  #(
    context.Context(session, error, dependencies),
    types.FlowAction(step_type),
    types.FlowInstance,
  ),
  error,
)

Like text_step, but the prompt is computed per update from the Context and the flow instance instead of being fixed when the flow is built.

Use this when the prompt depends on something only known at update time — most commonly the active locale for internationalization. text_step bakes its prompt in at flow-construction time (startup), which is too early to know the user’s language; text_step_with resolves it on every prompt instead.

builder.add_step(
  Date,
  handler.text_step_with(
    fn(ctx, _instance) { i18n.t(ctx, "book.ask_date", []) },
    "booking_date",
    Time,
  ),
)
✨ Search Document