Documentation
¶
Overview ¶
anybot 包提供机器人运行时、事件路由、中间件、会话存储、插件生命周期和协议适配核心。
Index ¶
- Variables
- func Configure(opts ...Option)
- func OnError(handler ErrorHandler)
- func OnReady(hook Hook)
- func OnShutdown(hook Hook)
- func OnStart(hook Hook)
- func OnStop(hook Hook)
- func ResetDefault(opts ...Option)
- func Run(ctxs ...context.Context) error
- func Use(middleware ...Middleware)
- func UsePlugin(plugin Plugin) error
- func UsePlugins(plugins ...Plugin) error
- type ActionClient
- type ActionError
- type ActionResponse
- type Adapter
- type App
- func (a *App) Adapter() Adapter
- func (a *App) Client() ActionClient
- func (a *App) Command(names ...string) *Route
- func (a *App) Dispatch(ctx context.Context, event *Event) error
- func (a *App) Group(rules ...Rule) *Router
- func (a *App) Logger() *slog.Logger
- func (a *App) On(rules ...Rule) *Route
- func (a *App) OnError(handler ErrorHandler)
- func (a *App) OnMessage(rules ...Rule) *Route
- func (a *App) OnReady(hook Hook)
- func (a *App) OnShutdown(hook Hook)
- func (a *App) OnStart(hook Hook)
- func (a *App) OnStop(hook Hook)
- func (a *App) Plugins() []Manifest
- func (a *App) Regex(pattern string) *Route
- func (a *App) Router() *Router
- func (a *App) Run(ctx context.Context) error
- func (a *App) Store() Store
- func (a *App) Use(middleware ...Middleware)
- func (a *App) UsePlugin(plugin Plugin) error
- func (a *App) UsePlugins(plugins ...Plugin) error
- type Context
- func (c *Context) Adapter() Adapter
- func (c *Context) App() *App
- func (c *Context) Args() string
- func (c *Context) Argv() []string
- func (c *Context) Client() ActionClient
- func (c *Context) Command() string
- func (c *Context) ConversationID() string
- func (c *Context) Event() *Event
- func (c *Context) Get(key string) (any, bool)
- func (c *Context) GroupID() string
- func (c *Context) GroupSession() *Session
- func (c *Context) IsGroup() bool
- func (c *Context) IsPrivate() bool
- func (c *Context) Logger() *slog.Logger
- func (c *Context) Match() Match
- func (c *Context) Pass() error
- func (c *Context) RawEvent() any
- func (c *Context) Reply(chain message.Chain) (MessageReceipt, error)
- func (c *Context) ReplyText(text string) (MessageReceipt, error)
- func (c *Context) Route() *Route
- func (c *Context) RouteName() string
- func (c *Context) SelfID() string
- func (c *Context) Session() *Session
- func (c *Context) SessionBy(key string) *Session
- func (c *Context) Set(key string, value any)
- func (c *Context) Stop()
- func (c *Context) StopError() error
- func (c *Context) Stopped() bool
- func (c *Context) Store() Store
- func (c *Context) String(key string) string
- func (c *Context) Target() ReplyTarget
- func (c *Context) Text() string
- func (c *Context) UserID() string
- func (c *Context) UserSession() *Session
- type EmitFunc
- type ErrorHandler
- type Event
- type EventKeyFunc
- type Handler
- type Hook
- type Manifest
- type Match
- type MemoryStore
- func (s *MemoryStore) Delete(ctx context.Context, key string) error
- func (s *MemoryStore) Get(ctx context.Context, key string) ([]byte, bool, error)
- func (s *MemoryStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (s *MemoryStore) Sweep(ctx context.Context) error
- type MessageReceipt
- type Middleware
- func OnlyGroup() Middleware
- func OnlyPrivate() Middleware
- func RateLimit(limit int, window time.Duration) Middleware
- func RateLimitBy(limit int, window time.Duration, keyFunc func(*Context) string) Middleware
- func Recover(loggers ...*slog.Logger) Middleware
- func SuperUser(ids ...any) Middleware
- func Timeout(timeout time.Duration) Middleware
- func Trace(loggers ...*slog.Logger) Middleware
- type Option
- func WithAdapter(adapter Adapter) Option
- func WithBuffer(size int) Option
- func WithErrorHandler(handler ErrorHandler) Option
- func WithLogger(logger *slog.Logger) Option
- func WithSerialBy(fn EventKeyFunc) Option
- func WithSerialByConversation() Option
- func WithStore(store Store) Option
- func WithWorkers(workers int) Option
- type PanicError
- type Plugin
- type PluginFunc
- type Protocol
- type ReplyTarget
- type Route
- type Router
- type Rule
- func All(rules ...Rule) Rule
- func Any() Rule
- func AnyOf(rules ...Rule) Rule
- func CommandRule(names ...string) Rule
- func CommandWithPrefixes(prefixes []string, names ...string) Rule
- func Contains(substr string) Rule
- func DetailType(kind string) Rule
- func EventType(kind string) Rule
- func FromSelf() Rule
- func FromUser(ids ...any) Rule
- func Group() Rule
- func InGroup(ids ...any) Rule
- func Mentioned(ids ...any) Rule
- func MessageEvent() Rule
- func Not(rule Rule) Rule
- func NotFromSelf() Rule
- func Prefix(prefix string) Rule
- func Private() Rule
- func RegexRule(pattern string) Rule
- func RegexpRule(re *regexp.Regexp) Rule
- func ToMe() Rule
- type RuleFunc
- type Session
- func (s *Session) Delete(ctx context.Context, name string) error
- func (s *Session) Get(ctx context.Context, name string) ([]byte, bool, error)
- func (s *Session) Key() string
- func (s *Session) LoadJSON(ctx context.Context, name string, out any) (bool, error)
- func (s *Session) SaveJSON(ctx context.Context, name string, value any, ttl time.Duration) error
- func (s *Session) Set(ctx context.Context, name string, value []byte, ttl time.Duration) error
- type Store
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPass 表示当前路由主动让出处理权,后续路由继续匹配。 ErrPass = errors.New("anybot: pass route") // ErrStop 表示当前事件应停止向后续路由传播。 ErrStop = errors.New("anybot: stop route") ErrUnauthorized = errors.New("anybot: unauthorized") // ErrRateLimited 表示当前限速窗口已被耗尽。 ErrRateLimited = errors.New("anybot: rate limited") )
Functions ¶
Types ¶
type ActionClient ¶
type ActionClient interface {
Call(context.Context, string, any, any) error
CallRaw(context.Context, string, any) (*ActionResponse, error)
Send(context.Context, ReplyTarget, message.Chain) (MessageReceipt, error)
}
ActionClient 定义协议动作调用和消息发送能力。
type ActionError ¶
ActionError 描述一次由协议端明确拒绝或返回失败状态的动作调用。
func (*ActionError) Error ¶
func (e *ActionError) Error() string
type ActionResponse ¶
type ActionResponse struct {
Status string
RetCode int
Message string
Wording string
Echo string
Data json.RawMessage
Raw json.RawMessage
}
ActionResponse 是协议无关的动作响应封套,保留标准字段和原始 data。
func (*ActionResponse) Decode ¶
func (r *ActionResponse) Decode(out any) error
Decode 将动作响应的 data 字段解码到 out;空 data 会被视为成功空值。
type Adapter ¶
type Adapter interface {
Protocol() Protocol
Start(context.Context, EmitFunc) error
Client() ActionClient
}
Adapter 把具体聊天协议接入 AnyBot 运行时。
type App ¶
type App struct {
// contains filtered or unexported fields
}
App 是 AnyBot 的运行时入口,负责连接适配器、调度事件并执行路由链。
func (*App) UsePlugins ¶
UsePlugins 按传入顺序安装多个插件,遇到错误时停止。
type Context ¶
Context 是路由处理函数的工作上下文,聚合事件、运行时能力、匹配结果和局部值。
func NewTestContext ¶
NewTestContext 创建测试用上下文;app 为空时会使用默认配置的新 App。
func (*Context) Client ¶
func (c *Context) Client() ActionClient
Client 返回适配器动作客户端,可用于发送消息或调用协议 API。
func (*Context) ConversationID ¶
ConversationID 返回当前事件对应的会话键,可直接用于会话存储。
func (*Context) GroupSession ¶
GroupSession 返回当前群或频道维度的存储视图。
func (*Context) Reply ¶
func (c *Context) Reply(chain message.Chain) (MessageReceipt, error)
Reply 使用事件的自然目标发送消息。
func (*Context) ReplyText ¶
func (c *Context) ReplyText(text string) (MessageReceipt, error)
ReplyText 使用事件的自然目标发送纯文本回复。
type Event ¶
type Event struct {
ID string
Protocol Protocol
SelfID string
Type string
DetailType string
SubType string
UserID string
GroupID string
GuildID string
ChannelID string
Text string
Message message.Chain
Raw json.RawMessage
Data any
}
Event 是 AnyBot 的协议无关事件封套,保留常用标准字段和原始协议数据。
func (*Event) ConversationID ¶
ConversationID 返回适合会话存储的稳定键,优先按频道、群聊、私聊逐级区分。
func (*Event) GroupSessionID ¶
GroupSessionID 返回按群或频道维度隔离的存储键。
func (*Event) Target ¶
func (e *Event) Target() ReplyTarget
Target 返回该事件的自然回复目标,供 Context.Reply 和适配器发送消息使用。
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore 是 goroutine 安全的进程内 Store,适合短期会话状态。
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(ctx context.Context, key string) error
Delete 删除指定键。
type Middleware ¶
Middleware 包装处理函数,用于实现日志、鉴权、限速等横切能力。
func RateLimit ¶
func RateLimit(limit int, window time.Duration) Middleware
RateLimit 使用当前会话键做简单的进程内限速。
func RateLimitBy ¶
RateLimitBy 使用自定义键做进程内限速;键为空时回退到会话、用户或全局维度。
func Recover ¶
func Recover(loggers ...*slog.Logger) Middleware
Recover 捕获处理链中的 panic,记录调用栈,并把 panic 转成 PanicError。
func SuperUser ¶
func SuperUser(ids ...any) Middleware
SuperUser 仅允许指定用户进入后续处理链,未授权时返回 ErrUnauthorized。
func Timeout ¶
func Timeout(timeout time.Duration) Middleware
Timeout 为后续处理函数派生带超时的 context;timeout 不大于 0 时不生效。
type Option ¶
type Option func(*App)
Option 调整 App 的运行时配置。
func WithErrorHandler ¶
func WithErrorHandler(handler ErrorHandler) Option
WithErrorHandler 注册错误处理器。
func WithSerialBy ¶
func WithSerialBy(fn EventKeyFunc) Option
WithSerialBy 按自定义事件键串行处理同键事件,适合保护会话状态流转。
func WithSerialByConversation ¶
func WithSerialByConversation() Option
WithSerialByConversation 让同一会话内的事件按顺序处理。
func WithWorkers ¶
WithWorkers 配置并发处理事件的 worker 数;不大于 0 时由适配器回调同步处理。
type PanicError ¶
PanicError 包装处理函数或中间件中恢复到的 panic 值和调用栈。
func (*PanicError) Error ¶
func (e *PanicError) Error() string
type PluginFunc ¶
PluginFunc 将普通安装函数适配为 Plugin。
func (PluginFunc) Install ¶
func (p PluginFunc) Install(app *App) error
Install 执行插件安装函数;未提供函数时视为成功。
type Protocol ¶
type Protocol string
Protocol 标识事件和动作所属的协议。
const ( // ProtocolOneBot11 表示 OneBot v11 协议。 ProtocolOneBot11 Protocol = "onebot11" )
type ReplyTarget ¶
type ReplyTarget struct {
Protocol Protocol
SelfID string
UserID string
GroupID string
GuildID string
ChannelID string
EventID string
}
ReplyTarget 描述一次消息回复的自然目标。
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route 表示一条可配置的事件路由。
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router 组织路由、中间件和共享基础规则。
type Rule ¶
Rule 判断路由是否应处理当前事件。
func CommandWithPrefixes ¶
CommandWithPrefixes 使用显式前缀集合匹配命令,并写入 prefix、command、args 和 argv。
func RegexpRule ¶
RegexpRule 使用已编译的正则表达式匹配事件文本,并写入 matches 与命名分组变量。
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session 是基于固定键前缀的 Store 视图。
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
adapters
|
|
|
onebot11
onebot11 包提供 AnyBot 的 OneBot v11 适配器、消息段构造器和类型化动作客户端。
|
onebot11 包提供 AnyBot 的 OneBot v11 适配器、消息段构造器和类型化动作客户端。 |
|
onebot11/napcat
napcat 包提供 NapCat 专属 OneBot v11 扩展动作的类型化辅助方法。
|
napcat 包提供 NapCat 专属 OneBot v11 扩展动作的类型化辅助方法。 |
|
cmd
|
|
|
anybot
command
|
|
|
examples
|
|
|
httpaction
command
|
|
|
media
command
|
|
|
permission
command
|
|
|
ping
command
|
|
|
pluginbot
command
|
|
|
reversews
command
|
|
|
session
command
|
|
|
websocket
command
|
|
|
internal
|
|
|
message 包定义 AnyBot 的协议中立消息链。
|
message 包定义 AnyBot 的协议中立消息链。 |
|
plugins
|
|
|
admin
admin 包提供一个超级用户命令示例插件。
|
admin 包提供一个超级用户命令示例插件。 |
|
echo
echo 包提供一个复读命令示例插件。
|
echo 包提供一个复读命令示例插件。 |
|
help
help 包提供一个可配置的基础帮助命令插件。
|
help 包提供一个可配置的基础帮助命令插件。 |
|
ratelimit
ratelimit 包提供把会话限速中间件打包后的示例插件。
|
ratelimit 包提供把会话限速中间件打包后的示例插件。 |
