diff --git a/dto/webhook.go b/dto/webhook.go index a53dd52..7ba54d3 100644 --- a/dto/webhook.go +++ b/dto/webhook.go @@ -8,6 +8,7 @@ type WHValidationReq struct { // WHValidationRsp 机器人回调验证响应结果 type WHValidationRsp struct { - PlainToken string `json:"plain_token"` - Signature string `json:"signature"` + PlainToken string `json:"plain_token"` + Signature string `json:"signature"` + DataVersion string `json:"data_version"` //数据格式版本号 } diff --git a/examples/apitest/main_test.go b/examples/apitest/main_test.go index fcab8b7..314b51c 100644 --- a/examples/apitest/main_test.go +++ b/examples/apitest/main_test.go @@ -7,7 +7,7 @@ import ( "testing" "time" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" "github.com/tencent-connect/botgo" "github.com/tencent-connect/botgo/openapi" diff --git a/examples/apitest/config.yaml.demo b/examples/config.yaml.demo similarity index 100% rename from examples/apitest/config.yaml.demo rename to examples/config.yaml.demo diff --git a/examples/custom-filter/config.yaml.demo b/examples/custom-filter/config.yaml.demo deleted file mode 100644 index 0f8790a..0000000 --- a/examples/custom-filter/config.yaml.demo +++ /dev/null @@ -1,3 +0,0 @@ -# 在这个配置文件中补充你的 appid 和 secret,并修改文件名为 config.yaml -appid : -secret : \ No newline at end of file diff --git a/examples/custom-logger/config.yaml.demo b/examples/custom-logger/config.yaml.demo deleted file mode 100644 index 0f8790a..0000000 --- a/examples/custom-logger/config.yaml.demo +++ /dev/null @@ -1,3 +0,0 @@ -# 在这个配置文件中补充你的 appid 和 secret,并修改文件名为 config.yaml -appid : -secret : \ No newline at end of file diff --git a/examples/custom-logger/main.go b/examples/custom-logger/main.go index 808dfa9..e0cf573 100644 --- a/examples/custom-logger/main.go +++ b/examples/custom-logger/main.go @@ -35,7 +35,6 @@ func main() { } // 把新的 logger 设置到 sdk 上,替换掉老的控制台 logger botgo.SetLogger(logger) - content, err := os.ReadFile("config.yaml") if err != nil { log.Fatalln("load config file failed, err:", err) diff --git a/examples/go.mod b/examples/go.mod index 85b0575..9bc4b78 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -4,9 +4,8 @@ go 1.21 require ( github.com/google/uuid v1.3.0 - github.com/tencent-connect/botgo v0.0.0-00010101000000-000000000000 + github.com/tencent-connect/botgo v0.1.7 go.uber.org/zap v1.19.1 - gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -22,5 +21,3 @@ require ( golang.org/x/oauth2 v0.23.0 // indirect golang.org/x/sync v0.1.0 // indirect ) - -replace github.com/tencent-connect/botgo => ../ diff --git a/examples/go.sum b/examples/go.sum index d8da5c7..3eab351 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -69,6 +69,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/tencent-connect/botgo v0.1.7 h1:+RkHW9T4WOBRGQtOcFAO52VvZEKoTXFq162k2xo0Jac= +github.com/tencent-connect/botgo v0.1.7/go.mod h1:oO1sG9ybhXNickvt+CVym5khwQ+uKhTR+IhTqEfOVsI= github.com/tidwall/gjson v1.9.3 h1:hqzS9wAHMO+KVBBkLxYdkEeeFHuqr95GfClRLKlgK0E= github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= diff --git a/examples/img.png b/examples/img.png deleted file mode 100644 index 6e2099d..0000000 Binary files a/examples/img.png and /dev/null differ diff --git a/examples/receive-and-send/config.yaml.demo b/examples/receive-and-send/config.yaml.demo deleted file mode 100644 index 0f8790a..0000000 --- a/examples/receive-and-send/config.yaml.demo +++ /dev/null @@ -1,3 +0,0 @@ -# 在这个配置文件中补充你的 appid 和 secret,并修改文件名为 config.yaml -appid : -secret : \ No newline at end of file diff --git a/examples/receive-and-send/main.go b/examples/receive-and-send/main.go index dfff08e..e55a0fa 100644 --- a/examples/receive-and-send/main.go +++ b/examples/receive-and-send/main.go @@ -42,10 +42,7 @@ func main() { log.Fatalln("parse config failed, err:", err) } log.Println("credentials:", credentials) - tokenSource := token.NewQQBotTokenSource(&token.QQBotCredentials{ - AppID: "", - AppSecret: "", - }) + tokenSource := token.NewQQBotTokenSource(credentials) if err = token.StartRefreshAccessToken(ctx, tokenSource); err != nil { log.Fatalln(err) } diff --git a/examples/simulate-callback-request/config.yaml.demo b/examples/simulate-callback-request/config.yaml.demo deleted file mode 100644 index d9072b6..0000000 --- a/examples/simulate-callback-request/config.yaml.demo +++ /dev/null @@ -1,3 +0,0 @@ - # 在这个配置文件中补充你的 appid 和 secret,并修改文件名为 config.yaml -appid: -secret: \ No newline at end of file diff --git a/examples/simulate-callback-request/main.go b/examples/simulate-callback-request/main.go index 794fdc1..48cf873 100644 --- a/examples/simulate-callback-request/main.go +++ b/examples/simulate-callback-request/main.go @@ -14,7 +14,7 @@ import ( "github.com/tencent-connect/botgo/dto" "github.com/tencent-connect/botgo/interaction/signature" "github.com/tencent-connect/botgo/token" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" ) const host = "http://localhost" @@ -29,7 +29,7 @@ func main() { log.Fatalln("load config file failed, err:", err) } credentials := &token.QQBotCredentials{} - if err = yaml.Unmarshal(content, &credentials); err != nil { + if err = yaml.Unmarshal(content, credentials); err != nil { log.Fatalln("parse config failed, err:", err) } log.Println("credentials:", credentials) diff --git a/interaction/signature/interaction_test.go b/interaction/signature/interaction_test.go index e0cefb7..8a8f234 100644 --- a/interaction/signature/interaction_test.go +++ b/interaction/signature/interaction_test.go @@ -5,25 +5,42 @@ import ( "testing" ) -func TestSignature(t *testing.T) { - secret := "abcdefg" - header := http.Header{} - header.Set(HeaderTimestamp, "1234567890") - httpBody := "text body" - sig, err := Generate(secret, header, []byte(httpBody)) - if err != nil { - t.Error(err) +func TestVerify(t *testing.T) { + type args struct { + secret string + header http.Header + httpBody []byte } - t.Log(sig) + tests := []struct { + name string + args args + want bool + wantErr bool + }{ + { + name: "test sig", + args: args{ - header.Set(HeaderSig, sig) - flag, err := Verify(secret, header, []byte(httpBody)) - if err != nil { - t.Error(err) + secret: "123456abcdef", + header: map[string][]string{ + "X-Signature-Ed25519": {"e949b5b94ef4103df903fb031d1d16e358db3db83e79e117edd404c8508be3ce8a76d7bad1bed353194c126a1a5915b4ad8b5288c1191cc53a12acffccd82004"}, + "X-Signature-Timestamp": {"1728981195"}}, + httpBody: []byte(`{"id":"ROBOT1.0_veoihSEXDc8Q.g-6eLpNIa11bH8MisOjn-m-LKxCPntMk6exUXgcWCGpVO7L2QKTNZzjZzFFDSbiOFcqAPWyVA!!","content":"哦一下","timestamp":"2024-10-15T16:33:15+08:00","author":{"id":"675860273","user_openid":"675860273"}}`), + }, + want: true, + wantErr: false, + }, } - if !flag { - t.Error("verify failed, but want ok") - } else { - t.Log("verify ok") + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := Verify(tt.args.secret, tt.args.header, tt.args.httpBody) + if (err != nil) != tt.wantErr { + t.Errorf("Verify() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("Verify() got = %v, want %v", got, tt.want) + } + }) } } diff --git a/interaction/webhook/webhook.go b/interaction/webhook/webhook.go index 5ba2793..7b94344 100644 --- a/interaction/webhook/webhook.go +++ b/interaction/webhook/webhook.go @@ -52,7 +52,7 @@ func HTTPHandler(w http.ResponseWriter, r *http.Request, credentials *token.QQBo log.Errorf("read http callback body error: %s", err) return } - log.Debugf("http callback body: %v", string(body)) + log.Debugf("http callback body: %s,len:%d", string(body), len(body)) log.Debugf("http callback header: %v", r.Header) traceID := r.Header.Get(constant.HeaderTraceID) // 签名验证