feat: v2(Incomplete,very early version)

This commit is contained in:
mei
2026-04-05 10:36:22 +08:00
parent 2ab9e060eb
commit 6190a8853a
41 changed files with 1131 additions and 766 deletions
+11
View File
@@ -0,0 +1,11 @@
package onebot
type OneBotConfig struct {
SendGroups []int `json:"send_groups"`
Msg string `json:"msg"`
}
type OneBotAccount struct {
URL string `json:"url"` // 例如 "http://localhost:5700"
Token string `json:"token"` // 例如 "your_token_here"
}
+42
View File
@@ -0,0 +1,42 @@
package onebot
import (
"context"
"log"
"time"
"github.com/cenkalti/backoff/v5"
"github.com/ssdomei232/goodBaby/configs"
"github.com/ssdomei232/goodBaby/model"
)
func SendOneBotMsg(rule *model.Rule) {
oneBotConfig := getOneBotConfigFromRule(rule)
oneBotAccount, err := getOneBotAccountFromRule(rule)
if err != nil {
log.Printf("获取OneBot账户失败: %v", err)
return
}
for _, groupID := range oneBotConfig.SendGroups {
config, err := configs.GetConfig()
if err != nil {
log.Printf("获取配置失败: %v", err)
return
}
var timeout time.Duration = time.Duration(config.TimeoutDurationHours) * time.Hour
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
operation := func() (string, error) {
err := sendOneBotMsg(oneBotAccount, groupID, oneBotConfig.Msg)
return "", err
}
_, err = backoff.Retry(ctx, operation, backoff.WithBackOff(backoff.NewExponentialBackOff()))
if err != nil {
log.Printf("发送OneBot消息失败: %v", err)
}
}
}
+47
View File
@@ -0,0 +1,47 @@
package onebot
import (
"encoding/json"
"strconv"
"github.com/ssdomei232/goodBaby/handler/db"
"github.com/ssdomei232/goodBaby/model"
napcat "github.com/ssdomei232/napcat-http-go-sdk"
)
// 从 Rule 中获取 OneBotConfig
func getOneBotConfigFromRule(rule *model.Rule) *OneBotConfig {
var oneBotConfig OneBotConfig
err := json.Unmarshal([]byte(rule.ConfigJson), &oneBotConfig)
if err != nil {
return nil
}
return &oneBotConfig
}
// 从 Rule 中获取 OneBotAccount
func getOneBotAccountFromRule(rule *model.Rule) (*OneBotAccount, error) {
gormDB, err := db.GetGormDB()
if err != nil {
return nil, err
}
var oneBotAccountEntry model.Account
gormDB.Where("id = ?", rule.AccountID).First(&oneBotAccountEntry)
var oneBotAccount OneBotAccount
err = json.Unmarshal([]byte(oneBotAccountEntry.Config), &oneBotAccount)
if err != nil {
return nil, err
}
return &oneBotAccount, nil
}
// 发送一条消息到OneBot
func sendOneBotMsg(account *OneBotAccount, groupID int, msg string) error {
client := napcat.NewClient(account.Token, account.URL)
err := client.SendGroupMsg(strconv.Itoa(groupID), msg)
return err
}