forked from lonelyevil/kook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (48 loc) · 1.21 KB
/
main.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
package kook
import (
"bytes"
"net/http"
"time"
)
// New creates a kook session with default settings
func New(token string, l Logger, o ...SessionOption) (s *Session) {
s = &Session{
Client: &http.Client{Timeout: 30 * time.Second},
sequence: new(int64),
MaxRetry: 3,
RetryTimeout: 60 * time.Second,
ContentType: "application/json",
Logger: l,
snStore: newBloomSnStore(),
Sync: true,
}
s.Identify.Token = "Bot " + token
s.Identify.Compress = true
for _, item := range o {
item(s)
}
return
}
// SessionOption is the optional arguments for creating a session.
type SessionOption func(*Session)
// SessionWithVerifyToken adds the token for verifying webhook request.
func SessionWithVerifyToken(token string) SessionOption {
return func(session *Session) {
session.Identify.VerifyToken = token
}
}
// SessionWithEncryptKey adds the key for decrypting webhook request.
func SessionWithEncryptKey(key []byte) SessionOption {
return func(session *Session) {
if len(key) < 32 {
buf := &bytes.Buffer{}
buf.Grow(32)
buf.Write(key)
for i := len(key); i < 32; i++ {
buf.WriteByte(byte(0))
}
key = buf.Bytes()
}
session.Identify.WebsocketKey = key
}
}