-
Notifications
You must be signed in to change notification settings - Fork 0
/
zgo.go
60 lines (49 loc) · 1.22 KB
/
zgo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package zgo
import (
"context"
"crypto/sha256"
"encoding/hex"
"net/http"
)
const (
getAccessTokenURL = "https://oauth.zaloapp.com/v4/oa/access_token"
getConversationURL = "https://openapi.zalo.me/v2.0/oa/conversation"
)
type Zgo interface {
GetAppID() string
EventSignature(data string, timestamp string) string
RefreshAccessToken(ctx context.Context, refreshToken string) (GetAccessTokenResp, error)
GetConversation(ctx context.Context, accessToken string, reqData GetConversationReq) (GetConversationResp, error)
}
type zgo struct {
appID string
appSecret string
oaSecret string
httpClient *http.Client
}
type Option func(z *zgo)
func OptionHTTPClient(client *http.Client) func(*zgo) {
return func(z *zgo) {
z.httpClient = client
}
}
func New(appID, appSecret, oaSecret string, opts ...Option) Zgo {
z := &zgo{
appID: appID,
appSecret: appSecret,
oaSecret: oaSecret,
httpClient: http.DefaultClient,
}
for _, opt := range opts {
opt(z)
}
return z
}
func (z *zgo) GetAppID() string {
return z.appID
}
func (z *zgo) EventSignature(data string, timestamp string) string {
data = z.appID + data + timestamp + z.oaSecret
sum := sha256.Sum256([]byte(data))
return hex.EncodeToString(sum[:])
}