Files
cellbot/internal/protocol/event.go
xiaolan ac0dfb64c9 feat: 初始化多机器人服务端项目框架
基于Go语言构建多机器人服务端框架,包含配置管理、事件总线、依赖注入等核心模块
添加项目基础结构、README、gitignore和初始代码实现
2026-01-04 21:19:17 +08:00

106 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package protocol
import "time"
// EventType 事件类型
type EventType string
const (
// 事件类型常量
EventTypeMessage EventType = "message"
EventTypeNotice EventType = "notice"
EventTypeRequest EventType = "request"
EventTypeMeta EventType = "meta"
EventTypeMessageSent EventType = "message_sent"
EventTypeNoticeSent EventType = "notice_sent"
EventTypeRequestSent EventType = "request_sent"
)
// Event 通用事件接口
// 参考OneBot12协议设计提供统一的事件抽象
type Event interface {
// GetType 获取事件类型
GetType() EventType
// GetDetailType 获取详细类型
GetDetailType() string
// GetSubType 获取子类型
GetSubType() string
// GetTimestamp 获取时间戳
GetTimestamp() time.Time
// GetSelfID 获取机器人自身ID
GetSelfID() string
// GetData 获取事件数据
GetData() map[string]interface{}
}
// BaseEvent 基础事件结构
type BaseEvent struct {
Type EventType `json:"type"`
DetailType string `json:"detail_type"`
SubType string `json:"sub_type,omitempty"`
Timestamp int64 `json:"timestamp"`
SelfID string `json:"self_id"`
Data map[string]interface{} `json:"data"`
}
// GetType 获取事件类型
func (e *BaseEvent) GetType() EventType {
return e.Type
}
// GetDetailType 获取详细类型
func (e *BaseEvent) GetDetailType() string {
return e.DetailType
}
// GetSubType 获取子类型
func (e *BaseEvent) GetSubType() string {
return e.SubType
}
// GetTimestamp 获取时间戳
func (e *BaseEvent) GetTimestamp() time.Time {
return time.Unix(e.Timestamp, 0)
}
// GetSelfID 获取机器人自身ID
func (e *BaseEvent) GetSelfID() string {
return e.SelfID
}
// GetData 获取事件数据
func (e *BaseEvent) GetData() map[string]interface{} {
return e.Data
}
// MessageEvent 消息事件
type MessageEvent struct {
BaseEvent
MessageID string `json:"message_id"`
Message string `json:"message"`
AltText string `json:"alt_text,omitempty"`
}
// NoticeEvent 通知事件
type NoticeEvent struct {
BaseEvent
GroupID string `json:"group_id,omitempty"`
UserID string `json:"user_id,omitempty"`
Operator string `json:"operator,omitempty"`
}
// RequestEvent 请求事件
type RequestEvent struct {
BaseEvent
RequestID string `json:"request_id"`
UserID string `json:"user_id"`
Comment string `json:"comment"`
Flag string `json:"flag"`
}
// MetaEvent 元事件
type MetaEvent struct {
BaseEvent
Status string `json:"status"`
}