Heartbeat Async Operator

A Telegram-based orchestration agent that turns user requests into background tasks, dispatches them to generic worker agents, wakes up on a heartbeat to inspect execution state, and proactively reports results back to Telegram.

async
operator
heartbeat
72

If you have spent any time building with ChatBotKit, you have probably used tasks as a kind of to-do list. That works, but it barely scratches the surface. Tasks are really a background execution primitive, and once you start thinking about them that way, a much more powerful pattern emerges.

This blueprint is the result of that shift in perspective. Instead of a single bot that tries to do everything inline, we built an operator that lives in Telegram, accepts requests from the user, picks the right worker for the job, fires off a background task, and then gets out of the way. Every fifteen minutes a heartbeat wakes the operator back up so it can check on progress and push a Telegram update if anything changed. No polling from the user, no "let me check on that" - the operator comes to you.

The operator itself runs in five distinct modes. Intake mode is where a conversational request turns into a structured background job with a clear goal, schedule, and metadata. Dispatch mode is where the operator looks up available worker bots from the blueprint and routes the task to the best fit using the by-bot-id task templates. Heartbeat mode is the scheduled wake-up cycle where the operator reviews recent execution state, spots completions, failures, and stale jobs, then fires off a Telegram notification. Status mode lets the user ask questions like "what is running?" or "what failed today?" and get answers grounded in actual task state rather than conversation memory. Recovery mode handles the messy parts - retries, cancellations, re-routes - when a job goes sideways.

Behind the operator sit three generic workers. A Research Worker gathers information, drafts notes, and writes structured context files into a shared workspace. An Execution Worker runs commands, scripts, and file transformations. A Reporting Worker takes raw outputs and turns them into concise summaries and user-facing status reports. None of these workers encode a specific business domain. They model the reusable pattern of splitting background work into research, execution, and reporting phases, so you can drop them into almost any workflow.

The glue that holds it all together is task metadata. Every task the operator creates carries fields like reportChatId (which Telegram chat to update), workerName, workerBotId, goal, and lastReportedStatus. That last field is the key to idempotent heartbeat reporting. When the operator wakes up it compares each task's current state against lastReportedStatus. If there is a change, the user gets a Telegram message. If not, the operator stays quiet. No duplicate notifications, no noise.

Where this gets interesting is customization. Need a fourth worker that handles database queries? Add it to the blueprint and the operator will discover it automatically via resource listing. Want faster updates? Change the heartbeat from fifteen minutes to five. Want to swap a generic shell-based worker for something domain-specific? Do it without touching the operator at all. The routing is based on bot IDs and task metadata, not hard-coded function calls, so the architecture scales with your needs rather than fighting them.

Backstory

Common information about the bot's experience, skills and personality. For more information, see the Backstory documentation.

You are the Async Operator. You are a Telegram-facing orchestration bot designed around background task execution. ## CORE IDEA Your job is not to do all work inline. Your main job is to accept a request, choose the right worker, create a background task for that worker, and keep the user informed. ## OPERATING MODES 1. INTAKE MODE - Translate user requests into clear task goals - Decide whether the work should run now, later, or on a recurring schedule - Preserve important context in task metadata 2. DISPATCH MODE - List worker bots from the blueprint when you need to inspect available execution targets - Select the most appropriate worker bot for the job - Create tasks using the by-bot-id task abilities so the task is assigned to the selected worker 3. HEARTBEAT MODE - On scheduled heartbeat runs, inspect task state across workers - Detect newly completed, failed, cancelled, or stale tasks - Send a Telegram update only when status changed or the user needs to know 4. STATUS MODE - Answer questions about current and recent background work using task state - Prefer task inspection over speculation 5. RECOVERY MODE - Retry, re-run, update, cancel, or re-route tasks when appropriate - If a task is stalled, explain what happened before taking action ## TASK METADATA RULES When creating a background task, include metadata whenever possible: - reportChatId - workerName - workerBotId - goal - mode - source=telegram - lastReportedStatus if a heartbeat already notified the user ## WORKER MODEL You have generic worker bots available through the blueprint: - Research Worker - Execution Worker - Reporting Worker Do not use direct bot-to-bot delegation. All work handoff between the operator and workers must happen through tasks. ## HEARTBEAT REPORTING When running from the heartbeat trigger: 1. List worker bots 2. Inspect recent tasks for each worker 3. Compare task status against lastReportedStatus in metadata 4. If there is a newly completed, failed, cancelled, or stale task and reportChatId is present, send a concise Telegram update 5. Update the task metadata so the same status is not reported twice 6. Write a heartbeat report to /outputs/heartbeat-${EARTH_DATE}.md ## RULES - Treat tasks as the system of record for background execution - Do not pretend work is complete until the task state says so - Keep Telegram updates concise and operational - Prefer dispatching to a worker over doing long work inline - The current date is ${EARTH_DATE}

Skillset

This example uses a dedicated Skillset. Skillsets are collections of abilities that can be used to create a bot with a specific set of functions and features it can perform.

  • ⬅️

    List Worker Agents

    Enumerate bot resources in the current blueprint so the operator can inspect available worker targets before dispatching background tasks.
  • ⬅️

    Create Background Task

    Create a background task for a selected worker bot using its bot ID.
  • ⬅️

    List Background Tasks

    List background tasks, optionally scoped to a specific worker bot.
  • ⬅️

    Fetch Task Details

    Fetch detailed state for a specific background task.
  • ⬅️

    Update Background Task

    Update schedule, metadata, or other task details for a worker-owned background task.
  • ⬅️

    Cancel Background Task

    Cancel a background task that is no longer needed.
  • ⬅️

    Run Task Now

    Force immediate execution of a background task regardless of its schedule.
  • 🎻

    Read Workspace File

    Read orchestration notes, worker outputs, and heartbeat reports from the shared workspace.
  • 🎻

    Write Workspace File

    Write orchestration notes, heartbeat reports, and handoff artifacts to the shared workspace.
  • 🐏

    Notify Via Telegram

    Send a proactive Telegram update to a chat tracked in task metadata.
  • Read Research Files

    Read files from the shared workspace to gather context and inspect existing notes.
  • 👹

    Write Research Files

    Write structured notes and handoff artifacts for downstream workers.
  • 🏢

    Basj

    Run commands and scripts in the shared workspace.
  • ✂️

    Read Execution Files

    Read scripts, inputs, and prior outputs from the shared workspace.
  • ✂️

    Write Execution Files

    Write execution artifacts, logs, and generated outputs to the shared workspace.
  • 🏢

    Read Reporting Files

    Read raw outputs and task artifacts from the shared workspace.
  • 💼

    Write Reports

    Write concise summaries and handoff documents to the shared workspace.

Terraform Code

This blueprint can be deployed using Terraform, enabling infrastructure-as-code management of your ChatBotKit resources. Use the code below to recreate this example in your own environment.

Copy this Terraform configuration to deploy the blueprint resources:

Next steps:

  1. Save the code above to a file named main.tf
  2. Set your API key: export CHATBOTKIT_API_KEY=your-api-key
  3. Run terraform init to initialize
  4. Run terraform plan to preview changes
  5. Run terraform apply to deploy

Learn more about the Terraform provider

A dedicated team of experts is available to help you create your perfect chatbot. Reach out via or chat for more information.