feat: v2(Incomplete,very early version)
This commit is contained in:
@@ -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"
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user