Files
goodBaby/drivers/onebot/accountConfigChecker.go
T

64 lines
1.7 KiB
Go
Raw Normal View History

2026-06-20 22:29:21 +08:00
package onebot
import (
"fmt"
2026-07-26 22:19:41 +08:00
"net/url"
"strings"
"github.com/ssdomei232/goodBaby/internal/meta"
2026-06-20 22:29:21 +08:00
)
2026-07-26 22:19:41 +08:00
// OneBotAccountConfigValidator OneBot账号配置验证器
2026-06-20 22:29:21 +08:00
type OneBotAccountConfigValidator struct{}
func (v *OneBotAccountConfigValidator) GetType() string {
2026-07-26 22:19:41 +08:00
return AccountType
2026-06-20 22:29:21 +08:00
}
func (v *OneBotAccountConfigValidator) Validate(config string) error {
2026-09-12 15:32:27 +08:00
cfg, err := ParseAccountConfig(config)
2026-07-26 22:19:41 +08:00
if err != nil {
return err
2026-06-20 22:29:21 +08:00
}
if cfg.Token == "" {
return fmt.Errorf("OneBot账号配置中Token不能为空")
}
if cfg.URL == "" {
return fmt.Errorf("OneBot账号配置中URL不能为空")
}
2026-07-26 22:19:41 +08:00
parsed, err := url.Parse(cfg.URL)
if err != nil || parsed.Host == "" || !strings.HasPrefix(parsed.Scheme, "http") {
return fmt.Errorf("OneBot账号配置中URL不合法,应形如 http://localhost:3000")
}
2026-06-20 22:29:21 +08:00
return nil
}
2026-07-26 22:19:41 +08:00
// Test 调用 get_login_info 验证地址与 Token
func (v *OneBotAccountConfigValidator) Test(config string) error {
2026-09-12 15:32:27 +08:00
cfg, err := ParseAccountConfig(config)
2026-07-26 22:19:41 +08:00
if err != nil {
return err
}
var resp apiResponse
if err := newClient(cfg).DoRequest("POST", "/get_login_info", map[string]any{}, &resp); err != nil {
return fmt.Errorf("连接 OneBot 失败: %w", err)
}
return resp.err()
}
func (v *OneBotAccountConfigValidator) Meta() meta.AccountMeta {
return meta.AccountMeta{
Type: AccountType,
Label: "OneBot (QQ)",
Description: "对接 NapCat / go-cqhttp 等 OneBot HTTP 服务,用于发送 QQ 消息。",
Fields: []meta.Field{
{Key: "url", Label: "HTTP 服务地址", Type: meta.FieldString, Required: true, Placeholder: "http://localhost:3000"},
{Key: "token", Label: "Access Token", Type: meta.FieldPassword, Required: true, Secret: true},
},
}
}