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

66 lines
1.5 KiB
Go
Raw Normal View History

package echo
import (
"context"
"cellbot/internal/engine"
"cellbot/internal/protocol"
"go.uber.org/zap"
)
func init() {
// 在 init 函数中注册多个处理函数(类似 ZeroBot 风格)
// 处理私聊消息(使用 OnlyPrivate 中间件,虽然 OnPrivateMessage 已经匹配私聊,这里作为示例)
engine.OnPrivateMessage().
Use(engine.OnlyPrivate()). // 只在私聊中响应
Handle(func(ctx context.Context, event protocol.Event, botManager *protocol.BotManager, logger *zap.Logger) error {
// 获取消息内容
data := event.GetData()
message, ok := data["message"]
if !ok {
return nil
}
userID, ok := data["user_id"]
if !ok {
return nil
}
// 获取 bot 实例
selfID := event.GetSelfID()
bot, ok := botManager.Get(selfID)
if !ok {
bots := botManager.GetAll()
if len(bots) == 0 {
logger.Error("No bot instance available")
return nil
}
bot = bots[0]
}
// 发送回复
action := &protocol.BaseAction{
Type: protocol.ActionTypeSendPrivateMessage,
Params: map[string]interface{}{
"user_id": userID,
"message": message,
},
}
_, err := bot.SendAction(ctx, action)
if err != nil {
logger.Error("Failed to send reply", zap.Error(err))
return err
}
logger.Info("Echo reply sent", zap.Any("user_id", userID))
return nil
})
// 可以继续注册更多处理函数
// engine.OnGroupMessage().Handle(...)
// engine.OnCommand("help").Handle(...)
}