telega/flow/handler

Built-in step handlers and resume handler factories.

Values

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

Create a router handler for resuming flows from callback queries

pub fn create_resume_handler_with_keyboard(
  flow: types.Flow(step_type, session, error, dependencies),
  callback_data: keyboard.KeyboardCallbackData(String),
) -> fn(bot.Context(session, error, dependencies), update.Update) -> Result(
  bot.Context(session, error, dependencies),
  error,
)

Create a router handler for resuming flows from callback queries with keyboard parsing

pub fn create_text_handler(
  flow: types.Flow(step_type, session, error, dependencies),
) -> fn(bot.Context(session, error, dependencies), update.Update) -> Result(
  bot.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(
  bot.Context(session, error, dependencies),
  types.FlowInstance,
) -> Result(
  #(
    bot.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(
    bot.Context(session, error, dependencies),
    types.FlowInstance,
  ) -> String,
  next_step next_step: option.Option(step_type),
) -> fn(
  bot.Context(session, error, dependencies),
  types.FlowInstance,
) -> Result(
  #(
    bot.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(
  bot.Context(session, error, dependencies),
  types.FlowInstance,
) -> Result(
  #(
    bot.Context(session, error, dependencies),
    types.FlowAction(step_type),
    types.FlowInstance,
  ),
  error,
)

Create a text input step

pub fn text_step_with(
  prompt prompt: fn(
    bot.Context(session, error, dependencies),
    types.FlowInstance,
  ) -> String,
  data_key data_key: String,
  next_step next_step: step_type,
) -> fn(
  bot.Context(session, error, dependencies),
  types.FlowInstance,
) -> Result(
  #(
    bot.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