Files
Cyrene_v2/plugins/channels/kook/config.go
T
Aska 764d592859
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
feat(channel): KOOK 渠道适配器 + 框架兼容增强(wsutil/httpclient)
- plugins/channels/kook: config/adapter/gateway/websocket/send/message/errors — 实现 PlatformAdapter 13方法
  - WS 有状态网关: 取gateway地址→连WS→信令0/1/2/3/4/6 + 30s心跳/6s超时 + resume(拼接sn/session_id) + 指数退避重连
  - 收事件: 信令0 → UnifiedMessage(word注 channel_type/type/target_id/extra.author→字段)
  - 发消息: /message/create(频道) + /direct-message/create(私聊,修正正确端点,非user-chat) + Bearer鉴权
  - 注册 channels.go + CHANNELS_JSON 多账号 + channelbridge.configured 加 kook(bot_token非空才连)
- 框架增强: internal/channel/adapter/wsutil(连接管理/心跳/重连/resume骨架,复用gorilla,KOOK用上) + httpclient(Bearer+envelope归一,KOOK用)
- 全仓 go build/vet/test 绿, 零新依赖(复用餐gorilla), 未动telegram/qq/weixin/webhook契约
- §4.6 隔离/黑白名单/第三方边界未破坏

注: KOOK WS/WH 双模式待补(ReceiveMode), 实测需主人提供 CHANNEL_KOOK_TOKEN
2026-09-07 12:01:35 +08:00

56 lines
2.2 KiB
Go

// Package kook implements the built-in KOOK (KaiHeiLa) channel adapter for
// Cyrene v2. KOOK is a stateful WebSocket-gateway channel: inbound events are
// delivered over a gateway WebSocket (hello/heartbeat/event/resume signaling),
// while outbound messages are sent over a bearer-authenticated REST API.
//
// The adapter conforms to internal/channel/adapter.PlatformAdapter, translating
// native KOOK events into the normalized adapter.UnifiedMessage model and
// delivering replies via /api/v3/message/create (channels) and
// /api/v3/direct-message/create (private chat). Identity is carried as channel
// actor metadata (author id + nickname) only — owner/member resolution is the
// channel layer's job, never a prompt-text heuristic.
//
// The design authority is DevDocs/design/08-渠道接入.md §4 (kook) and §7
// (framework reusability). It depends on internal/channel/adapter (including the
// shared wsutil and httpclient primitives), internal/logger, and the standard
// library. The bot token is never logged.
package kook
import (
"strings"
"time"
)
// Defaults and knobs for the KOOK integration.
const (
// defaultBaseURL is the KOOK REST API root.
defaultBaseURL = "https://www.kookapp.cn/api/v3"
// defaultHTTPTimeout bounds a single REST request.
defaultHTTPTimeout = 30 * time.Second
// gatewayPath is the gateway-address fetch endpoint relative to baseURL.
gatewayPath = "/gateway/index"
// maxMessageLen is KOOK's documented per-message content limit.
maxMessageLen = 8000
// heartbeatInterval is how often the client sends a ping (KOOK: 30s).
heartbeatInterval = 30 * time.Second
// helloTimeout bounds the wait for the handshake (hello/resume ack).
helloTimeout = 6 * time.Second
)
// Config holds the settings required to reach the KOOK API.
type Config struct {
// BotToken is the secret bot token issued by the KOOK developer center. It is
// surfaced through Config() but never written to logs.
BotToken string `json:"bot_token"`
// BaseURL is the KOOK REST API root; empty uses the production default.
BaseURL string `json:"base_url"`
}
// Validate checks the configuration for values needed to connect.
func (c Config) Validate() error {
if strings.TrimSpace(c.BotToken) == "" {
return ErrMissingToken
}
return nil
}