Skip to content

Commit

Permalink
修复examples/receive-and-send配置加载逻辑 (merge request !101)
Browse files Browse the repository at this point in the history
Squash merge branch 'fix_20241122_demo_recevie_and_send' into 'master'
修复examples/receive-and-send配置加载逻辑
  • Loading branch information
rianli committed Nov 25, 2024
1 parent 660d5f6 commit 07ab80d
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 44 deletions.
5 changes: 3 additions & 2 deletions dto/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"` //数据格式版本号

Check failure on line 13 in dto/webhook.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 13 in dto/webhook.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)
}
2 changes: 1 addition & 1 deletion examples/apitest/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions examples/custom-filter/config.yaml.demo

This file was deleted.

3 changes: 0 additions & 3 deletions examples/custom-logger/config.yaml.demo

This file was deleted.

1 change: 0 additions & 1 deletion examples/custom-logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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 => ../
2 changes: 2 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Binary file removed examples/img.png
Binary file not shown.
3 changes: 0 additions & 3 deletions examples/receive-and-send/config.yaml.demo

This file was deleted.

5 changes: 1 addition & 4 deletions examples/receive-and-send/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 0 additions & 3 deletions examples/simulate-callback-request/config.yaml.demo

This file was deleted.

4 changes: 2 additions & 2 deletions examples/simulate-callback-request/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
51 changes: 34 additions & 17 deletions interaction/signature/interaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion interaction/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// 签名验证
Expand Down

0 comments on commit 07ab80d

Please sign in to comment.