telega/chat_action

Keeps a chat action indicator (“typing…”, “sending photo…”) alive while a long-running handler executes.

Telegram clears a chat action after ~5 seconds, so a single api.send_chat_action call is not enough for slow handlers. with_action re-sends the action every ~4 seconds until the wrapped function returns:

import telega/chat_action

fn handler(ctx, _) {
  use <- chat_action.with_action(ctx, chat_action.Typing)
  // ... long-running work: LLM call, file processing, etc.
  reply.with_text(ctx, "Done!")
}

The repeating sender runs in an unlinked worker process that monitors the caller: it stops when the wrapped function returns or when the calling process dies, so no processes are leaked even if the handler crashes.

Types

Chat action to broadcast, mirrors ChatAction.

pub type Action {
  Typing
  UploadPhoto
  RecordVideo
  UploadVideo
  RecordVoice
  UploadVoice
  UploadDocument
  ChooseSticker
  FindLocation
  RecordVideoNote
  UploadVideoNote
}

Constructors

  • Typing
  • UploadPhoto
  • RecordVideo
  • UploadVideo
  • RecordVoice
  • UploadVoice
  • UploadDocument
  • ChooseSticker
  • FindLocation
  • RecordVideoNote
  • UploadVideoNote

Values

pub fn with_action(
  ctx ctx: bot.Context(session, error, dependencies),
  action action: Action,
  run run: fn() -> a,
) -> a

Sends action immediately and then re-sends it every ~4 seconds while run executes. Returns the result of run.

use <- chat_action.with_action(ctx, chat_action.Typing)
slow_work(ctx)
pub fn with_action_every(
  ctx ctx: bot.Context(session, error, dependencies),
  action action: Action,
  interval interval: Int,
  run run: fn() -> a,
) -> a

Same as with_action but with a custom re-send interval in milliseconds. Useful for tests and for long uploads where a different cadence is needed.

Search Document