Files
goodBaby/drivers/onebot/onebot.go
T

41 lines
1011 B
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
)
2026-09-12 15:32:27 +08:00
// SendMessage 向规则中配置的群和好友发送消息
//
// 每个目标单独重试、互不阻塞,最后汇总失败信息交给上层记录。
func SendMessage(ctx context.Context, account *OneBotAccount, config *OneBotConfig) error {
2026-07-26 22:19:41 +08:00
var fails []string
total := 0
2026-09-12 15:32:27 +08:00
for _, groupID := range config.SendGroups {
2026-07-26 22:19:41 +08:00
total++
2026-09-12 15:32:27 +08:00
if err := retry.Do(ctx, func() error {
return sendGroupMsg(account, groupID, config.Msg)
}); 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
2026-09-12 15:32:27 +08:00
for _, userID := range config.SendUsers {
2026-07-26 22:19:41 +08:00
total++
2026-09-12 15:32:27 +08:00
if err := retry.Do(ctx, func() error {
return sendPrivateMsg(account, userID, config.Msg)
}); err != nil {
2026-07-26 22:19:41 +08:00
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
}