mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-27 18:30:54 +08:00
First part of #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 <222954346+dar1k0@users.noreply.github.com>
74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
package chatbridge
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/Autumn-27/artex/notify"
|
|
)
|
|
|
|
// The masking convention is the one the notification channels use, so the UI
|
|
// handles both forms the same way: a credential is echoed as
|
|
// notify.MaskedPrefix plus its last characters, sending the masked value back
|
|
// unchanged keeps the stored secret, and an empty string clears the field.
|
|
|
|
// MaskConfig returns a copy of cfg with the transport's credential fields
|
|
// masked. An unknown kind yields an empty map rather than the raw config.
|
|
func MaskConfig(kind string, cfg map[string]any) map[string]any {
|
|
t, ok := Get(kind)
|
|
if !ok {
|
|
return map[string]any{}
|
|
}
|
|
secrets := map[string]bool{}
|
|
for _, k := range t.SecretKeys() {
|
|
secrets[k] = true
|
|
}
|
|
out := make(map[string]any, len(cfg))
|
|
for k, v := range cfg {
|
|
if !secrets[k] {
|
|
out[k] = v
|
|
continue
|
|
}
|
|
if s, ok := v.(string); ok && s != "" {
|
|
out[k] = notify.MaskedValue(s)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// PrepareConfigUpdate merges an incoming configuration from the API into the
|
|
// stored one.
|
|
//
|
|
// A masked secret keeps the stored value. When a destination field changes
|
|
// (for example base_url, which decides where the bot token is sent) every
|
|
// secret must be supplied again in clear: a masked value would otherwise send
|
|
// the old token to the new address.
|
|
func PrepareConfigUpdate(kind string, stored, incoming map[string]any) (map[string]any, error) {
|
|
t, ok := Get(kind)
|
|
if !ok {
|
|
return nil, fmt.Errorf("chat bridge kind %q is not registered", kind)
|
|
}
|
|
if stored == nil {
|
|
stored = map[string]any{}
|
|
}
|
|
changed := false
|
|
for _, k := range t.DestinationKeys() {
|
|
in, present := incoming[k]
|
|
if !present {
|
|
continue
|
|
}
|
|
if fmt.Sprint(in) != fmt.Sprint(stored[k]) && !(in == "" && stored[k] == nil) {
|
|
changed = true
|
|
}
|
|
}
|
|
if changed {
|
|
for _, k := range t.SecretKeys() {
|
|
s, _ := incoming[k].(string)
|
|
if s == "" || notify.IsMasked(s) {
|
|
return nil, errors.New("the API address changed: enter the bot token again")
|
|
}
|
|
}
|
|
}
|
|
return notify.MergeConfig(stored, incoming), nil
|
|
}
|