feat: 初始化多机器人服务端项目框架
基于Go语言构建多机器人服务端框架,包含配置管理、事件总线、依赖注入等核心模块 添加项目基础结构、README、gitignore和初始代码实现
This commit is contained in:
77
internal/protocol/interface.go
Normal file
77
internal/protocol/interface.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// Protocol 通用协议接口
|
||||
// 参考OneBot12协议核心设计理念,定义统一的机器人协议接口
|
||||
type Protocol interface {
|
||||
// Name 获取协议名称
|
||||
Name() string
|
||||
// Version 获取协议版本
|
||||
Version() string
|
||||
// Connect 建立连接
|
||||
Connect(ctx context.Context) error
|
||||
// Disconnect 断开连接
|
||||
Disconnect(ctx context.Context) error
|
||||
// IsConnected 检查连接状态
|
||||
IsConnected() bool
|
||||
// SendAction 发送动作
|
||||
SendAction(ctx context.Context, action Action) (map[string]interface{}, error)
|
||||
// HandleEvent 处理事件
|
||||
HandleEvent(ctx context.Context, event Event) error
|
||||
// GetSelfID 获取机器人自身ID
|
||||
GetSelfID() string
|
||||
}
|
||||
|
||||
// Adapter 协议适配器接口
|
||||
// 实现具体协议的接入逻辑
|
||||
type Adapter interface {
|
||||
Protocol
|
||||
// ParseMessage 解析原始消息为Event
|
||||
ParseMessage(raw []byte) (Event, error)
|
||||
// SerializeAction 序列化Action为协议格式
|
||||
SerializeAction(action Action) ([]byte, error)
|
||||
}
|
||||
|
||||
// BotInstance 机器人实例接口
|
||||
// 管理单个机器人实例的生命周期
|
||||
type BotInstance interface {
|
||||
Protocol
|
||||
// GetID 获取实例ID
|
||||
GetID() string
|
||||
// Start 启动实例
|
||||
Start(ctx context.Context) error
|
||||
// Stop 停止实例
|
||||
Stop(ctx context.Context) error
|
||||
// GetStatus 获取实例状态
|
||||
GetStatus() BotStatus
|
||||
}
|
||||
|
||||
// BotStatus 机器人状态
|
||||
type BotStatus string
|
||||
|
||||
const (
|
||||
BotStatusStarting BotStatus = "starting"
|
||||
BotStatusRunning BotStatus = "running"
|
||||
BotStatusStopping BotStatus = "stopping"
|
||||
BotStatusStopped BotStatus = "stopped"
|
||||
BotStatusError BotStatus = "error"
|
||||
)
|
||||
|
||||
// EventHandler 事件处理器接口
|
||||
type EventHandler interface {
|
||||
// Handle 处理事件
|
||||
Handle(ctx context.Context, event Event) error
|
||||
// Priority 获取处理器优先级(数值越小优先级越高)
|
||||
Priority() int
|
||||
// Match 判断是否匹配事件
|
||||
Match(event Event) bool
|
||||
}
|
||||
|
||||
// Middleware 中间件接口
|
||||
type Middleware interface {
|
||||
// Process 处理事件
|
||||
Process(ctx context.Context, event Event, next func(ctx context.Context, event Event) error) error
|
||||
}
|
||||
Reference in New Issue
Block a user