Chloe

Docs

Telegram

Putting an agent on Telegram: the bot token, who may write, groups, and every option.


  1. In Telegram, message @BotFather, send /newbot, and pick a name. It replies with a token.
  2. Put it in .env as CHLOE_AGENTS_<ID>_TELEGRAM, and in the config as agents: { <id>: { telegram: process.env.CHLOE_AGENTS_<ID>_TELEGRAM } }.
  3. Add the channel to the agent:
// This agent on Telegram. Its token, from @BotFather, goes in .env as
// CHLOE_AGENTS_SHOP_TELEGRAM, and chloe.config.ts hands it over as
// `agents: { shop: { telegram: process.env.CHLOE_AGENTS_SHOP_TELEGRAM } }`.
// Without one the reader says so and does not start.
//
// allowFrom is Telegram user ids, and an empty list answers nobody, which is
// the safe way for this to arrive. Put your own id in it: message the bot, read
// the id out of the log line, and save.
import { telegramChannel } from "@chloejs/core/channels";

export default telegramChannel({ allowFrom: [] });
  1. Message the bot. It answers with your Telegram user id. Put that in allowFrom and save. An allowed person is answered in any chat, groups included.

Besides the options every channel takes:

Option Default What it controls
allowFrom nobody Telegram user ids, as numbers.
send only answers { when } lets the agent write to somebody in allowFrom first, with the tool telegramSendMessage. The channel brings the tool itself, because only the running channel can deliver a message.
inGroups "when-addressed" In a group, answer only a command, a mention or a reply to the bot. "always" answers every message from someone in allowFrom.
stackWithin 1 Seconds to wait for a second message in the same chat, so two sent together are read as one. 0 reads each on its own.
uploadPolicy pictures, PDFs and text, up to 10 MB Which files are handed to the agent: { allowedMediaTypes, maxBytes }. Anything else is named to it, not handed over.
mode "polling" "polling": chloe asks Telegram for messages, and nothing is exposed. "webhook": Telegram posts them to publicUrl + /chloe/v1/<agent>/<name>, which has to get past any login in front of the port.
publicUrl none Where this server is reached from outside, for "webhook".
credentials the token in settings { botToken, webhookSecretToken }, to give them here instead. Without a secret, "webhook" makes a new one each start.

Telegram's command list allows no hyphens, so /stuck_orders runs stuck-orders, and can be registered with BotFather.

Sending a message from a chat or a job

A job sends to an address like telegram:12345 with deliver() from @chloejs/core/services. With send: { when }, the agent gets the tool telegramSendMessage and can write to somebody in allowFrom without waiting for them to write first: when says when, in your words. A tool bound by hand from @chloejs/core/tools/telegram can send to other ids instead.