Repository navigation
feat(chatbridge): inbound chat bridge core with a Telegram long-poll transport (#5, part 1 of 3) - #456
Open
Shiv9993 wants to merge 1 commit into
Open
feat(chatbridge): inbound chat bridge core with a Telegram long-poll transport (#5, part 1 of 3)#456Shiv9993 wants to merge 1 commit into
Shiv9993 wants to merge 1 commit into
Conversation
Shiv9993
force-pushed
the
feat/chat-bridge-core
branch
3 times, most recently
from
November 21, 2026 19:14
c7b34b7 to
24bebcf
Compare
…transport First part of BaaRai#5: the transport-agnostic bridge core, the Telegram adapter, the mandatory chat allowlist, storage, API and settings page. Task commands, main-agent routing and remote approvals follow in separate PRs on top of this one. - chatbridge: Transport interface and Router. The router enforces the allowlist (an unknown chat gets one refusal naming its chat ID, then silence, bounded memory), drops updates older than two minutes so a backlog is never executed, ignores bot senders and commands addressed to another bot, and dispatches commands (/help, /whoami for now). - Telegram transport: getUpdates long polling (no inbound port), plain-text sendMessage truncated to the Bot API limit, getMe for the identity. It reuses notify's dial guard and redirect policy through small exported wrappers, and its errors never carry the token. - Storage: migration 0015 adds chat_bridges, one row per kind. config is sealed at rest (added to secretColumns) and masked in the API with the notification-channel convention. The update cursor is committed before a batch is acted on, only moves forward, and is written only for the config_version the poller read, so a poll of a replaced token can never move the new bot's cursor. A token change clears it. - Server: one loop per transport started by server.New; it re-reads its row each round and Reload interrupts a poll in flight, so saving the form applies without a restart. Exponential backoff on errors, honours Telegram's retry_after, logs under [tgbot] without message text. - API: GET /api/chat-bridges, PUT /api/chat-bridges/{kind}, POST /api/chat-bridges/{kind}/test. A bridge may run with an empty allowlist: it then serves no chat and only sends the one-time refusal naming the chat ID, which is how an operator finds the ID to add. - Web: System -> Chat bot page with live status, zh-CN and tr strings. - Docs: docs/chat-bridge.md; README lists the token among stored secrets. The migration test lists 0015; the pre-0012 test rolls it back with the others and it re-applies (the migration is idempotent). Co-authored-by: krad <[email protected]>
Shiv9993
force-pushed
the
feat/chat-bridge-core
branch
from
November 24, 2026 09:59
24bebcf to
58bafdb
Compare
Author
|
@BaaRai rebased on current
No functional change to the bridge. Re-ran on the rebased commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part 1 of 3 for #5, as agreed in the issue: the bridge core, the Telegram long-poll adapter, the chat allowlist, settings and the migration, with tests. Task commands with main-agent routing (PR 2) and approval buttons with a one-time nonce and expiry (PR 3) build on this one.
Problem
ARTEX can push findings to Telegram, but an operator cannot talk back: there is no inbound path from a chat app.
Behaviour change
chatbridgepackage. It is transport-agnostic: aTransportinterface plus aRouterthat owns every chat-app-independent rule./cmd@other_bot) are ignored./helpand/whoamifor now. PR 2 adds the task commands.getUpdateslong polling, so ARTEX opens no inbound port. Replies go out through plain-textsendMessage, cut to the Bot API limit, andgetMeidentifies the bot.notify/http.go, so both directions share one guard.chat_bridges, one row per kind).configis sealed at rest (added tosecretColumns) and masked in the API using the notification-channel convention.base_urlrequires entering the token again.config_versionthe poller read, so a poll that was running under a replaced token can never move the new bot's cursor. A token change clears the cursor in the same statement that saves the token.server.Newstarts one loop per transport. Each loop re-reads its row every round, and saving the form interrupts the poll in flight, so token, allowlist and on/off changes apply without a restart.retry_afteris honoured.[tgbot]tag and never include message text.GET /api/chat-bridges,PUT /api/chat-bridges/{kind},POST /api/chat-bridges/{kind}/test. The test route checks the token and sends no message.docs/chat-bridge.md. The README lists the token among the encrypted secrets.A bridge may run with an empty allowlist. It then serves no chat and only sends the one-time refusal naming the chat ID, which is how an operator learns the ID to add. I first refused to enable a bridge with an empty allowlist, but the live test below showed that makes the first setup impossible without a placeholder ID. Only listed chats are ever served either way.
What I tested
Automated (local PostgreSQL 16,
ARTEX_PG_DSNset, so the DB tests ran rather than skipped):go test ./chatbridge@other_botcommandsgo test ./db: full suite. Includes the newTestChatBridgeSaveSealedAndOffset(sealed at rest, forward-only cursor, version-guarded cursor, reset on token change) and the migration tests with 0015 added.go test ./server -run TestChatBridgeretry_aftergo test ./notify,go vet ./...npm run test:i18npasses.biome checkis clean on the new page and the sidebar.tsc --noEmitpasses.NEXT_EXPORT=1 next build) includes/system/chat-bridge.Live, with a real Telegram bot against a locally built binary (
-tags embedui):/helpand/whoamianswered. Changing the allowlist and switching the bot on applied with no restart./bot…URL, andchat_bridges.configwas sealed in the database.Note on the full suite: I ran
go test ./...on Windows. 27 tests inagent,evidenceandserverfail there because of Windows-only causes:\in paths, no Python on PATH, andfsyncon a directory returning "Access is denied". The same 27 fail on an untouchedmaincheckout. None of them touches this change. I have not run the suite on Linux, so CI is the authoritative run.