Files
goodBaby/drivers/onebot/onebot.go
T

52 lines
1.2 KiB
Go
Raw Normal View History

2026-04-05 10:36:22 +08:00
package onebot
import (
"context"
2026-07-26 22:19:41 +08:00
"fmt"
"strings"
2026-04-05 10:36:22 +08:00
2026-07-26 22:19:41 +08:00
"github.com/ssdomei232/goodBaby/internal/retry"
2026-04-05 10:36:22 +08:00
"github.com/ssdomei232/goodBaby/model"
)
2026-07-26 22:19:41 +08:00
// SendOneBotMsg 向规则中配置的群和好友发送消息
func SendOneBotMsg(ctx context.Context, rule *model.Rule) error {
oneBotConfig, err := getOneBotConfigFromRule(rule)
if err != nil {
return err
}
2026-04-05 10:36:22 +08:00
oneBotAccount, err := getOneBotAccountFromRule(rule)
if err != nil {
2026-07-26 22:19:41 +08:00
return fmt.Errorf("获取OneBot账户失败: %w", err)
2026-04-05 10:36:22 +08:00
}
2026-07-26 22:19:41 +08:00
var fails []string
total := 0
2026-04-05 10:36:22 +08:00
for _, groupID := range oneBotConfig.SendGroups {
2026-07-26 22:19:41 +08:00
total++
err := retry.Do(ctx, func() error {
return sendGroupMsg(oneBotAccount, groupID, oneBotConfig.Msg)
})
2026-04-05 10:36:22 +08:00
if err != nil {
2026-07-26 22:19:41 +08:00
fails = append(fails, fmt.Sprintf("群 %d: %v", groupID, err))
2026-04-05 10:36:22 +08:00
}
}
2026-07-26 22:19:41 +08:00
for _, userID := range oneBotConfig.SendUsers {
total++
err := retry.Do(ctx, func() error {
return sendPrivateMsg(oneBotAccount, userID, oneBotConfig.Msg)
})
if err != nil {
fails = append(fails, fmt.Sprintf("好友 %d: %v", userID, err))
}
}
if len(fails) > 0 {
return fmt.Errorf("%d/%d 条消息发送失败: %s", len(fails), total, strings.Join(fails, "; "))
}
return nil
2026-04-05 10:36:22 +08:00
}