Files
ARTEX/docs/chat-bridge.md
T
shivandkrad b6ec42f15a fix(chatbridge): address review notes on the bridge core
- Require https for base_url. The bot token travels in the URL path and
  the responses are the updates the bridge acts on, so plain http is
  accepted only with ARTEX_NOTIFY_ALLOW_LOCAL=1, for a local test server.
- Cut replies by UTF-16 code units, which is how Telegram counts its 4096
  limit, so emoji-heavy text is no longer rejected. A cut never splits a
  character.
- Cap a server-sent retry_after at the backoff ceiling (1 minute).
- Pause one second after an empty poll, so a self-hosted Bot API that
  ignores the long-poll timeout cannot spin the loop.
- When the refused-chat memory is full, start over instead of muting
  every new chat, which also muted the operator's own chat. Saving the
  configuration of the same bot keeps the memory.
- Fix two stale comments that said an empty allowlist keeps the bridge
  off (migration 0015 is unreleased, so editing its comment is safe).

Co-authored-by: krad <222954346+dar1k0@users.noreply.github.com>
2026-11-26 22:24:28 +05:30

67 lines
4.2 KiB
Markdown

# Chat Bot (Inbound Chat Bridge)
The chat bot lets an operator talk to ARTEX from a chat app instead of the web UI. Telegram is the
first transport. This page describes the bridge core: connecting a bot, the chat allowlist and how
updates are processed. Task commands, the main-agent conversation and remote approvals build on it
([issue #5](https://github.com/BaaRai/ARTEX/issues/5)).
Outbound notifications (pushing findings to a chat) are a separate feature under
**System → Notifications** and keep working without the bot.
---
## Setup
1. Create a bot with [@BotFather](https://t.me/BotFather) and copy its token. Give ARTEX a bot of
its own: Telegram allows one consumer per token, so a token already used by another poller or by
a webhook fails with a conflict error.
2. Open **System → Chat bot**, paste the token, switch the bot on and save. **Test connection** asks
Telegram which bot the token belongs to; it sends no message. The status card shows **Online**
and the bot's username once it is polling.
3. With the allowlist still empty the bot serves no chat. From each chat you want to use (a private
chat or a group), send the bot any message: it answers once with a refusal that names the chat ID.
4. Add those chat IDs to **Allowed chat IDs** and save. The change applies at once.
5. Send `/help` to see the commands. `/whoami` shows the chat ID and your user ID.
## Security model
- **Only allowlisted chats are served.** A chat that is not listed gets one refusal line, naming its
chat ID, and is then ignored. The bot remembers up to 1024 refused chats, across saves of the same
bot; past that it starts over, so a chat may be refused one more time but a new chat always learns
its ID. An empty allowlist serves no chat at all; that is the state a new bot starts in while you
collect chat IDs. Allowing a group allows every member of that group.
- **No inbound port.** The bot uses long polling (`getUpdates`) over outbound HTTPS.
- **The token is a secret.** It is encrypted at rest with `keys/secret.key` like other stored
secrets, shown masked in the UI and API, and never written to logs or error messages (errors keep
only the scheme and host of the API address). Changing the API address requires entering the
token again, so a stored token is never sent to a new host. The API address must use `https`:
the token travels in the URL path, and the replies are the updates the bot acts on. Plain `http`
is accepted only with `ARTEX_NOTIFY_ALLOW_LOCAL=1`, for a local test server.
- **Same network guard as notifications.** Calls refuse loopback and link-local addresses (including
cloud metadata) unless `ARTEX_NOTIFY_ALLOW_LOCAL=1`, and never follow a cross-host redirect.
- **No replays.** The update cursor is committed before a batch is acted on, so a crash loses a
batch rather than running it twice. Messages older than two minutes when they reach ARTEX (for
example a backlog queued while ARTEX was down) are dropped and logged instead of executed.
- **Message text is not logged.** Logs (tag `[tgbot]`) record the chat, the sender and the decision.
## Configuration changes
Saving the form takes effect immediately, without a restart: a poll in progress is interrupted and
the new configuration is read. A new token clears the update cursor, because Telegram update IDs
are per bot. A batch fetched under a configuration that was replaced during the poll is dropped.
## API
All routes require the administrator JWT.
| Method | Path | Purpose |
| --- | --- | --- |
| `GET` | `/api/chat-bridges` | Every transport with its masked configuration, allowlist and live status |
| `PUT` | `/api/chat-bridges/{kind}` | Save `enabled`, `config` and `allowed_chats`; omitted fields keep their stored value |
| `POST` | `/api/chat-bridges/{kind}/test` | Check the stored token and return the bot identity |
`config` follows the notification channel convention: a masked value sent back unchanged keeps the
stored secret, and an empty string clears a field. Telegram's fields are `bot_token` and the
optional `base_url` (an `https` address of a self-hosted Bot API server). The live `status.state` is `off`,
`connecting`, `online` or `error`, with `last_error` when it fails.