Documentation
¶
Index ¶
- Constants
- func GenerateBindCode() (string, error)
- func RegisterCommand(spec CommandSpec)
- func SplitReply(s, lang string) []string
- type AssistantHandler
- type AuditEntry
- type AuditRecorder
- type BindCodeConsumer
- type BindingInfo
- type BindingLookup
- type Button
- type CommandHandler
- type CommandRegistry
- type CommandRegistryAPI
- type CommandReply
- type CommandSource
- type CommandSpec
- type InboundAssistantMessage
- type MessageChain
- type RateLimitSpec
- type RateLimiter
- type RateLimiterAPI
- type Replier
- type Reply
- type SessionState
- type SessionStore
- type SessionStoreAPI
- type Source
Constants ¶
const ( ReplyRunes = 3500 ReplyParts = 4 )
一条 ChatOps 回复的字数上限(Telegram 一条消息最多 4096 个字符);更长的回复分几条发,最多 ReplyParts 条。
Variables ¶
This section is empty.
Functions ¶
func GenerateBindCode ¶
GenerateBindCode generates an 8-character unambiguous binding code using CSPRNG. Uses charset without 0/O/1/l/I to reduce user error in manual entry. Returns error only on crypto/rand failure (should be extremely rare).
func RegisterCommand ¶
func RegisterCommand(spec CommandSpec)
func SplitReply ¶
SplitReply 把太长的回复分成几条(每条不超过 ReplyRunes 个字,尽量在换行处分),最多 ReplyParts 条, 再多的省略并注明(注明的话也算在字数里)。lang 以 en 开头时注明用英文。
Types ¶
type AssistantHandler ¶
type AssistantHandler interface {
// Enabled 报告现在要不要把非命令消息交给助手(助手启用、ChatOps 开关打开)。
Enabled(ctx context.Context) bool
// Handle 处理一条非命令消息,返回立即回复(可以为空)与审计结果。
Handle(ctx context.Context, msg InboundAssistantMessage) (Reply, string)
}
AssistantHandler 接管绑定用户的非命令消息(内置 AI 助手,路线图 M19)。Handle 只登记、立即返回: 回答由助手在后台经 Replier 异步发出,不在入站路径上等模型(Telegram 的长轮询是同步调用 Process 的)。
type AuditEntry ¶
type AuditEntry struct {
NotificationConfID uint
ChannelType string
ChannelUserID string
Command string
Args map[string]any
Result string
LatencyMs int64
}
AuditEntry is the audit row written by the chain.
type AuditRecorder ¶
type AuditRecorder interface {
Record(ctx context.Context, e AuditEntry) error
}
AuditRecorder writes a single audit row. Implemented by app.AuditService.
type BindCodeConsumer ¶
type BindCodeConsumer interface {
ConsumeCode(ctx context.Context, code string, confID uint, channelType, channelUserID string) error
}
BindCodeConsumer consumes a /bind <code> request. Implemented by app.BindingService. confID 是消息来源的通知通道配置,绑定码只能在签发它的那个通道上兑换。
type BindingInfo ¶
type BindingInfo struct {
ID uint
ConfID uint
ChannelType string
ChannelUserID string
ReplyLang string
PtAdmin bool
Allowed bool
}
BindingInfo is the subset of binding data needed by the chain. Defined locally (instead of importing internal/app) to avoid an import cycle: internal/app already imports internal/chatops for GenerateBindCode.
type BindingLookup ¶
type BindingLookup interface {
FindByChannelUser(ctx context.Context, confID uint, channelType, channelUserID string) (BindingInfo, bool, error)
}
BindingLookup resolves a (notification conf, channel, user) triple to a BindingInfo. 绑定属于具体的通知通道配置:同一个用户在另一个 Bot/通道上发消息,不能沿用这里的绑定。
type CommandHandler ¶
type CommandRegistry ¶
type CommandRegistry struct {
// contains filtered or unexported fields
}
func DefaultRegistry ¶
func DefaultRegistry() *CommandRegistry
func NewCommandRegistry ¶
func NewCommandRegistry() *CommandRegistry
func (*CommandRegistry) Get ¶
func (r *CommandRegistry) Get(name string) (CommandSpec, bool)
func (*CommandRegistry) List ¶
func (r *CommandRegistry) List() []CommandSpec
func (*CommandRegistry) Register ¶
func (r *CommandRegistry) Register(spec CommandSpec)
type CommandRegistryAPI ¶
type CommandRegistryAPI interface {
Get(name string) (CommandSpec, bool)
List() []CommandSpec
}
CommandRegistryAPI abstracts CommandRegistry so the chain can be tested without depending on the concrete registry implementation.
type CommandReply ¶
type CommandReply = Reply
type CommandSource ¶
type CommandSource = Source
type CommandSpec ¶
type CommandSpec struct {
Name string
Description string
Aliases []string
AdminOnly bool
Handler CommandHandler
RateLimit *RateLimitSpec
}
type InboundAssistantMessage ¶
type InboundAssistantMessage struct {
Msg notify.InboundMessage
Src Source
}
InboundAssistantMessage 是交给助手的一条消息:原始入站消息(回复要用)与绑定信息。
type MessageChain ¶
type MessageChain struct {
// contains filtered or unexported fields
}
MessageChain dispatches inbound messages through the permission gate per plan T22 (binding → allowed → command parse → session → registry → ratelimit → admin check → handler).
func NewMessageChain ¶
func NewMessageChain( registry CommandRegistryAPI, bindings BindingLookup, bindCoder BindCodeConsumer, audit AuditRecorder, rl RateLimiterAPI, sessions SessionStoreAPI, replier Replier, ) *MessageChain
NewMessageChain wires the dependencies. replier may be nil when there is no outbound channel (e.g. CLI debugging).
func (*MessageChain) Process ¶
func (mc *MessageChain) Process(ctx context.Context, msg notify.InboundMessage) error
Process is the inbound entry point. It returns an error only on unrecoverable conditions (lookup failure, nil handler); business denies are surfaced via audit + reply, never as errors.
func (*MessageChain) SetAssistant ¶
func (mc *MessageChain) SetAssistant(h AssistantHandler)
SetAssistant 接上内置 AI 助手(nil 摘掉):之后绑定用户的非命令消息交给它。可以在通道运行中调用。
func (*MessageChain) SetLogf ¶
func (mc *MessageChain) SetLogf(fn func(format string, args ...any))
SetLogf 注入日志函数,用于记录审计写入失败。
func (*MessageChain) SetTemplates ¶
func (mc *MessageChain) SetTemplates(t *notify.Templates)
SetTemplates 接上消息模板(在通道启动以前调用)。
type RateLimitSpec ¶
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
func NewRateLimiter ¶
func NewRateLimiter() *RateLimiter
func (*RateLimiter) Allow ¶
func (rl *RateLimiter) Allow(channel, userID, command string) bool
type RateLimiterAPI ¶
RateLimiterAPI abstracts RateLimiter for the chain.
type Reply ¶
type Reply struct {
Text string
Buttons [][]Button
SilentDrop bool
// Failed 表示命令没做成(参数不对、服务不可用、执行出错),Text 里写了原因。
// 处理器照常返回 nil error 让回复发出去,审计据此记为失败而不是 success。
Failed bool
// Event 是这条回复的结构化数据(几个查询命令与 AI 助手的回复才有),按它套用消息模板;Text 是内置的文字。
// 没有时由 MessageChain 按 reply.other(命令名与做成了没有)套用。
Event *notify.Event
}
type SessionState ¶
type SessionState struct {
Step string
Data string
Handler CommandHandler
ExpiresAt time.Time
}
type SessionStore ¶
type SessionStore struct {
// contains filtered or unexported fields
}
func NewSessionStore ¶
func NewSessionStore() *SessionStore
func (*SessionStore) Clear ¶
func (s *SessionStore) Clear(channel string, confID uint, userID string)
func (*SessionStore) Pending ¶
func (s *SessionStore) Pending(channel string, confID uint, userID string) (SessionState, bool)
func (*SessionStore) Set ¶
func (s *SessionStore) Set(channel string, confID uint, userID string, state SessionState, ttl time.Duration)
func (*SessionStore) Stop ¶
func (s *SessionStore) Stop()
type SessionStoreAPI ¶
type SessionStoreAPI interface {
Pending(channel string, confID uint, userID string) (SessionState, bool)
Set(channel string, confID uint, userID string, state SessionState, ttl time.Duration)
Clear(channel string, confID uint, userID string)
}
SessionStoreAPI abstracts SessionStore for the chain.