Files
cellbot/internal/plugins/echo/echo.go

138 lines
3.1 KiB
Go
Raw Normal View History

package echo
import (
"context"
"cellbot/internal/protocol"
"go.uber.org/zap"
)
// EchoPlugin 回声插件
type EchoPlugin struct {
logger *zap.Logger
botManager *protocol.BotManager
}
// NewEchoPlugin 创建回声插件
func NewEchoPlugin(logger *zap.Logger, botManager *protocol.BotManager) *EchoPlugin {
return &EchoPlugin{
logger: logger.Named("echo-plugin"),
botManager: botManager,
}
}
// Handle 处理事件
func (p *EchoPlugin) Handle(ctx context.Context, event protocol.Event) error {
// 获取事件数据
data := event.GetData()
// 获取消息内容
message, ok := data["message"]
if !ok {
p.logger.Debug("No message field in event")
return nil
}
rawMessage, ok := data["raw_message"].(string)
if !ok {
p.logger.Debug("No raw_message field in event")
return nil
}
// 获取用户ID
userID, ok := data["user_id"]
if !ok {
p.logger.Debug("No user_id field in event")
return nil
}
p.logger.Info("Received private message",
zap.Any("user_id", userID),
zap.String("message", rawMessage))
// 获取 self_id 来确定是哪个 bot
selfID := event.GetSelfID()
// 获取对应的 bot 实例
bot, ok := p.botManager.Get(selfID)
if !ok {
// 如果通过 selfID 找不到,尝试获取第一个 bot
bots := p.botManager.GetAll()
if len(bots) == 0 {
p.logger.Error("No bot instance available")
return nil
}
bot = bots[0]
p.logger.Debug("Using first available bot",
zap.String("bot_id", bot.GetID()))
}
// 构建回复动作
action := &protocol.BaseAction{
Type: protocol.ActionTypeSendPrivateMessage,
Params: map[string]interface{}{
"user_id": userID,
"message": message, // 原封不动返回消息
},
}
p.logger.Info("Sending echo reply",
zap.Any("user_id", userID),
zap.String("reply", rawMessage))
// 发送消息
result, err := bot.SendAction(ctx, action)
if err != nil {
p.logger.Error("Failed to send echo reply",
zap.Error(err),
zap.Any("user_id", userID))
return err
}
p.logger.Info("Echo reply sent successfully",
zap.Any("user_id", userID),
zap.Any("result", result))
return nil
}
// Priority 获取处理器优先级
func (p *EchoPlugin) Priority() int {
return 100 // 中等优先级
}
// Match 判断是否匹配事件
func (p *EchoPlugin) Match(event protocol.Event) bool {
// 只处理私聊消息
eventType := event.GetType()
detailType := event.GetDetailType()
p.logger.Debug("Echo plugin matching event",
zap.String("event_type", string(eventType)),
zap.String("detail_type", detailType))
if eventType != protocol.EventTypeMessage {
p.logger.Debug("Event type mismatch", zap.String("expected", string(protocol.EventTypeMessage)))
return false
}
if detailType != "private" {
p.logger.Debug("Detail type mismatch", zap.String("expected", "private"), zap.String("got", detailType))
return false
}
p.logger.Info("Echo plugin matched event!")
return true
}
// Name 获取插件名称
func (p *EchoPlugin) Name() string {
return "Echo"
}
// Description 获取插件描述
func (p *EchoPlugin) Description() string {
return "回声插件:将私聊消息原封不动返回"
}