Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复examples/receive-and-send配置加载逻辑 #52

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

// 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)
}
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 @@ -36,7 +36,7 @@
return string(s)
}

// Deprecated: DefaultGetSecretFunc 默认的获取 secret 的函数,默认从环境变量读取

Check warning on line 39 in interaction/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / build

exported: comment on exported var DefaultGetSecretFunc should be of the form "DefaultGetSecretFunc ..." (revive)
// 开发者如果需要从自己的配置文件,或者是其他地方获取 secret,可以重写这个函数
var DefaultGetSecretFunc = func() string {
return os.Getenv("QQBotSecret")
Expand All @@ -52,7 +52,7 @@
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
Loading