forked from go-lark/lark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lark.go
165 lines (141 loc) · 3.53 KB
/
lark.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Package lark is an easy-to-use SDK for Feishu and Lark Open Platform,
// which implements messaging APIs, with full-fledged supports on building Chat Bot and Notification Bot.
package lark
import (
"context"
"net/http"
"sync/atomic"
"time"
)
const (
// ChatBot should be created with NewChatBot
// Create from https://open.feishu.cn/ or https://open.larksuite.com/
ChatBot = iota
// NotificationBot for webhook, behave as a simpler notification bot
// Create from Lark group
NotificationBot
)
// Bot definition
type Bot struct {
// bot type
botType int
// Auth info
appID string
appSecret string
accessToken atomic.Value
tenantAccessToken atomic.Value
// user id type for api chat
userIDType string
// webhook for NotificationBot
webhook string
// API Domain
domain string
// http client
client *http.Client
// custom http client
useCustomClient bool
customClient HTTPWrapper
// auth heartbeat
heartbeat chan bool
// auth heartbeat counter (for testing)
heartbeatCounter int64
ctx context.Context
logger LogWrapper
debug bool
}
// Domains
const (
DomainFeishu = "https://open.feishu.cn"
DomainLark = "https://open.larksuite.com"
)
// NewChatBot with appID and appSecret
func NewChatBot(appID, appSecret string) *Bot {
bot := &Bot{
botType: ChatBot,
appID: appID,
appSecret: appSecret,
client: initClient(),
domain: DomainFeishu,
ctx: context.Background(),
logger: initDefaultLogger(),
}
bot.accessToken.Store("")
bot.tenantAccessToken.Store("")
return bot
}
// NewNotificationBot with URL
func NewNotificationBot(hookURL string) *Bot {
bot := &Bot{
botType: NotificationBot,
webhook: hookURL,
client: initClient(),
ctx: context.Background(),
logger: initDefaultLogger(),
}
bot.accessToken.Store("")
bot.tenantAccessToken.Store("")
return bot
}
// requireType checks whether the action is allowed in a list of bot types
func (bot Bot) requireType(botType ...int) bool {
for _, iterType := range botType {
if bot.botType == iterType {
return true
}
}
return false
}
// SetClient assigns a new client to bot.client
func (bot *Bot) SetClient(c *http.Client) {
bot.client = c
}
func initClient() *http.Client {
return &http.Client{
Timeout: 5 * time.Second,
}
}
// SetCustomClient .
func (bot *Bot) SetCustomClient(c HTTPWrapper) {
bot.useCustomClient = true
bot.customClient = c
}
func (bot *Bot) SetAccessToken(token string) {
bot.accessToken.Store(token)
}
func (bot *Bot) SetTenantAccessToken(token string) {
bot.tenantAccessToken.Store(token)
}
// UnsetCustomClient .
func (bot *Bot) UnsetCustomClient() {
bot.useCustomClient = false
bot.customClient = nil
}
// SetDomain set domain of endpoint, so we could call Feishu/Lark
// go-lark does not check your host, just use the right one or fail.
func (bot *Bot) SetDomain(domain string) {
bot.domain = domain
}
// Domain returns current domain
func (bot Bot) Domain() string {
return bot.domain
}
// AppID returns bot.appID for external use
func (bot Bot) AppID() string {
return bot.appID
}
// BotType returns bot.botType for external use
func (bot Bot) BotType() int {
return bot.botType
}
// AccessToken returns bot.accessToken for external use
func (bot Bot) AccessToken() string {
return bot.accessToken.Load().(string)
}
// TenantAccessToken returns bot.tenantAccessToken for external use
func (bot Bot) TenantAccessToken() string {
return bot.tenantAccessToken.Load().(string)
}
// SetWebhook updates webhook URL
func (bot *Bot) SetWebhook(url string) {
bot.webhook = url
}