Skip to content

Commit

Permalink
Test4 (#49)
Browse files Browse the repository at this point in the history
* Compiled main.go and pushed changes

* test

* 适配了频道私聊,用bolt数据库取代ini

* 适配了nonebot2

* add license

* add a lot

* trss support

* add action

* add action

* add action

* fixbug

* add wss

* bugfix

* fix action

* fix action again

* fa

* fix

* add a lot

* add ws server token

* bugifx

* fixat

* bugfix

* bugfix

* test

* test2

* add url service

* add url service

* bugfix

* fix

* fix

* fix

* bug fix

* fix

* test

* add webui

* add image_compress

* bug fix

* fix cq code

* fixbug

* bugfix

* bugfix

* bugfix

* bugfix

* bugfix

* bugfix

* bugfix

* add new feature

* bugfix

* bugfix

* bugfix

* bugfix

* bugfix

* merge

* bugfix

* bugfix

* bugfix

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* bugfix

* bugfix

* Remove dist directory from tracking

* test

* test

* bugfix2
  • Loading branch information
Hoshinonyaruko authored Nov 7, 2023
1 parent 8b70122 commit b7524a1
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 65 deletions.
130 changes: 109 additions & 21 deletions botgo/token/authtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strconv"
"sync"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
"github.com/tencent-connect/botgo/log"
)

Expand Down Expand Up @@ -127,9 +128,10 @@ type queryTokenReq struct {
ClientSecret string `json:"clientSecret"`
}

// 自定义地址返回需满足这个格式
type queryTokenRsp struct {
AccessToken string `json:"access_token"`
ExpiresIn string `json:"expires_in"`
AccessToken string `json:"access_token"`
ExpiresIn interface{} `json:"expires_in"` // 允许任何类型
}

// func queryAccessToken(ctx context.Context, tokenURL, appID, clientSecrent string) (AccessTokenInfo, error) {
Expand Down Expand Up @@ -191,48 +193,134 @@ type queryTokenRsp struct {
// return rdata, err
// }

// queryAccessToken retrieves a new AccessToken.
func queryAccessToken(ctx context.Context, tokenURL, appID, clientSecret string) (AccessTokenInfo, error) {
// 是否使用自定义ac地址
configURL := config.GetDevelop_Acdir()
if tokenURL == "" {
tokenURL = getAccessTokenURL // Assumes getAccessTokenURL is declared elsewhere
if configURL != "" {
tokenURL = configURL
} else {
tokenURL = getAccessTokenURL // 默认值
}
}

reqBody := queryTokenReq{
AppID: appID,
ClientSecret: clientSecret,
}
reqData, err := json.Marshal(reqBody)
if err != nil {
return AccessTokenInfo{}, err
}
var req *http.Request
var err error

req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, bytes.NewReader(reqData))
if err != nil {
return AccessTokenInfo{}, err
//自定义地址使用get,无需参数
if configURL != "" {
req, err = http.NewRequestWithContext(ctx, http.MethodGet, configURL, nil)
} else {
// 默认ac地址使用post
reqBody := queryTokenReq{
AppID: appID,
ClientSecret: clientSecret,
}
reqData, err := json.Marshal(reqBody)
if err != nil {
return AccessTokenInfo{}, err
}

req, err = http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, bytes.NewReader(reqData))
if err != nil {
return AccessTokenInfo{}, err
}
req.Header.Set("Content-Type", "application/json")
}
req.Header.Add("Content-Type", "application/json")

// Perform the HTTP request.
resp, err := http.DefaultClient.Do(req)
if err != nil {
return AccessTokenInfo{}, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
// Read the response body.
body, err := io.ReadAll(resp.Body)
if err != nil {
return AccessTokenInfo{}, err
}

var respData queryTokenRsp
if err := json.Unmarshal(body, &respData); err != nil {
if err = json.Unmarshal(body, &respData); err != nil {
return AccessTokenInfo{}, err
}

expiresIn, _ := strconv.ParseInt(respData.ExpiresIn, 10, 64) // Ignoring error can be dangerous, handle it as needed
// 自定义地址返回可能是string或者int
expiresInInt, err := parseExpiresIn(respData.ExpiresIn)
if err != nil {
return AccessTokenInfo{}, fmt.Errorf("expires_in is not a valid int or string: %v", err)
}

return AccessTokenInfo{
Token: respData.AccessToken,
ExpiresIn: expiresIn,
ExpiresIn: expiresInInt,
UpTime: time.Now(),
}, nil
}

func parseExpiresIn(expiresIn interface{}) (int64, error) {
switch v := expiresIn.(type) {
case float64:
// JSON numbers come as floats by default
return int64(v), nil
case int64:
// In case the JSON decoder was set to use integers
return v, nil
case json.Number:
// Convert json.Number to int64
return v.Int64()
case string:
// If the value is a string, try converting it to an integer
return strconv.ParseInt(v, 10, 64)
default:
// If none of the above types matched, return an error
return 0, fmt.Errorf("expires_in is not a valid type, got %T", expiresIn)
}
}

//原函数
// func queryAccessToken(ctx context.Context, tokenURL, appID, clientSecret string) (AccessTokenInfo, error) {
// if tokenURL == "" {
// tokenURL = getAccessTokenURL // Assumes getAccessTokenURL is declared elsewhere
// }

// reqBody := queryTokenReq{
// AppID: appID,
// ClientSecret: clientSecret,
// }
// reqData, err := json.Marshal(reqBody)
// if err != nil {
// return AccessTokenInfo{}, err
// }

// req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, bytes.NewReader(reqData))
// if err != nil {
// return AccessTokenInfo{}, err
// }
// req.Header.Add("Content-Type", "application/json")

// resp, err := http.DefaultClient.Do(req)
// if err != nil {
// return AccessTokenInfo{}, err
// }
// defer resp.Body.Close()

// body, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// return AccessTokenInfo{}, err
// }

// var respData queryTokenRsp
// if err := json.Unmarshal(body, &respData); err != nil {
// return AccessTokenInfo{}, err
// }

// expiresIn, _ := strconv.ParseInt(respData.ExpiresIn, 10, 64) // Ignoring error can be dangerous, handle it as needed

// return AccessTokenInfo{
// Token: respData.AccessToken,
// ExpiresIn: expiresIn,
// UpTime: time.Now(),
// }, nil
// }
39 changes: 39 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Settings struct {
ImageLimit int `yaml:"image_sizelimit"`
RemovePrefix bool `yaml:"remove_prefix"`
BackupPort string `yaml:"backup_port"`
DevlopAcDir string `yaml:"develop_access_token_dir"`
RemoveAt bool `yaml:"remove_at"`
DevBotid string `yaml:"develop_bot_id"`
}

// LoadConfig 从文件中加载配置并初始化单例配置
Expand Down Expand Up @@ -348,6 +351,30 @@ func GetServer_dir() string {
return instance.Settings.Server_dir
}

// 获取DevBotid
func GetDevBotid() string {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to get DevBotid.")
return "1234"
}
return instance.Settings.DevBotid
}

// 获取Develop_Acdir服务的地址
func GetDevelop_Acdir() string {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to get DevlopAcDir.")
return ""
}
return instance.Settings.DevlopAcDir
}

// 获取lotus的值
func GetLotusValue() bool {
mu.Lock()
Expand All @@ -360,6 +387,18 @@ func GetLotusValue() bool {
return instance.Settings.Lotus
}

// 获取RemoveAt的值
func GetRemoveAt() bool {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to get RemoveAt value.")
return false
}
return instance.Settings.RemoveAt
}

// 获取port的值
func GetPortValue() string {
mu.Lock()
Expand Down
15 changes: 13 additions & 2 deletions handlers/message_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func RevertTransformedText(data interface{}) string {
default:
return ""
}
//处理前 先去前后空
messageText := strings.TrimSpace(msg.Content)

// 将messageText里的BotID替换成AppID
Expand All @@ -232,9 +233,13 @@ func RevertTransformedText(data interface{}) string {
submatches := re.FindStringSubmatch(m)
if len(submatches) > 1 {
userID := submatches[1]
// 检查是否是 BotID,如果是则直接返回,不进行映射
// 检查是否是 BotID,如果是则直接返回,不进行映射,或根据用户需求移除
if userID == AppID {
return "[CQ:at,qq=" + AppID + "]"
if config.GetRemoveAt() {
return ""
} else {
return "[CQ:at,qq=" + AppID + "]"
}
}

// 不是 BotID,进行正常映射
Expand Down Expand Up @@ -271,6 +276,12 @@ func RevertTransformedText(data interface{}) string {
}
}

//如果移除了前部at,信息就会以空格开头,因为只移去了最前面的at,但at后紧跟随一个空格
if config.GetRemoveAt() {
//再次去前后空
messageText = strings.TrimSpace(messageText)
}

return messageText
}

Expand Down
Loading

0 comments on commit b7524a1

Please sign in to comment.