Free lesson

Bot contract and secrets

Bot contract and secrets — Telegram bot with a local AI model. One private-chat scenario; Keep the token outside prompts; Inject dependencies first. Practical assignment: Practice: Bot contract and secrets.

Demo edition. The materials are undergoing editorial review.

Topics covered

  • One private-chat scenario
  • Keep the token outside prompts
  • Inject dependencies first

One private-chat scenario

The educational bot accepts short text from an allowed user, calls local Ollama and prepares a reply to the same private chat. It cannot publish channels, execute shell commands or read files at the model's request. Use Node.js with fetch, a separate test bot and an already installed local model. Start with offline fixtures that need no Telegram token. Those fixtures test your program without proving real message delivery or model speed on your own hardware.

Keep the token outside prompts

BotFather issues a token that controls the bot; the owner handles creation or revocation separately. This course creates and publishes no credentials. Store the real value in a protected file or secret manager rather than code, screenshots or shell history. Bot API URLs contain the token, so raw fetch errors or debug logs may leak it. Return sanitized error codes rather than full URLs. Removing a leaked line from Git does not revoke an exposed credential.

Inject dependencies first

Separate Telegram transport, authorization, model adapter and update processing into functions. Pass dependencies into the handler so a test can supply a fixture model and a send function that only records calls in an array. That mode has no network at all. Label fixture responses explicitly rather than presenting them as model output. Build policy.mjs, model.mjs, transport.mjs, worker.mjs and offline.test.mjs incrementally. Importing an early module must not automatically begin polling or send a message.

Practical assignment

Practice: Bot contract and secrets

Continue to learning

Course outline

  1. Bot contract and secrets
  2. Authorization before the model
  3. Local model adapter
  4. Bot API transport and long polling
  5. State and uncertain delivery
  6. Offline tests and controlled startup