This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cortexbot.go
271 lines (235 loc) · 5.67 KB
/
cortexbot.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package cortexbot
import (
"database/sql"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"
"golang.org/x/net/proxy"
"github.com/go-telegram-bot-api/telegram-bot-api"
cortex "github.com/ilyaglow/go-cortex/v3"
// sqlite3 driver
_ "github.com/mattn/go-sqlite3"
)
const (
sqliteFileName = "cortexbot.db"
tgTokenEnvName = "TGBOT_API_TOKEN"
cortexURLEnvName = "CORTEX_URL"
cortexAPIKeyEnvName = "CORTEX_API_KEY"
socksURLEnvName = "SOCKS5_URL"
cortexBotPWEnvName = "CORTEX_BOT_PASSWORD"
cortexTimeoutEnvName = "CORTEX_TIMEOUT"
debugEnvName = "CORTEX_BOT_DEBUG"
)
var (
// defaultTLP is Green because indicators reach telegram servers.
// Same for defaultPAP.
// TODO: think about making it configurable.
defaultTLP = cortex.TLPGreen
defaultPAP = cortex.PAPGreen
pollTimeout = 20 * time.Second
cortexTimeout = 5 * time.Minute
tgTokenEnvValue = os.Getenv(tgTokenEnvName)
cortexURLEnvValue = os.Getenv(cortexURLEnvName)
cortexAPIKeyEnvValue = os.Getenv(cortexAPIKeyEnvName)
cortexBotPWEnvValue = os.Getenv(cortexBotPWEnvName)
socksURLEnvValue = os.Getenv(socksURLEnvName)
cortexTimeoutEnvValue = os.Getenv(cortexTimeoutEnvName)
debugEnvValue = os.Getenv(debugEnvName)
)
// Client defines bot's abilities to interact with services.
// Deprecated: use Cortexbot instead.
type Client Cortexbot
// Cortexbot defines bot's abilities to interact with services.
type Cortexbot struct {
Bot *tgbotapi.BotAPI
Cortex *cortex.Client
Password string
DB *sql.DB
dbpath string
TLP cortex.TLP
PAP cortex.PAP
Timeout time.Duration
Debug bool
}
func (c *Cortexbot) log(v ...interface{}) {
if c.Debug {
log.Println(v...)
}
}
// socks5Client bootstraps http.Client that uses socks5 proxy
func socks5Client(u *url.URL) (*http.Client, error) {
dialer, err := proxy.FromURL(u, proxy.Direct)
if err != nil {
return nil, err
}
return &http.Client{
Transport: &http.Transport{Dial: dialer.Dial},
Timeout: time.Second * 30,
}, nil
}
// NewClient bootstraps the Client struct from env variables.
// Deprecated: Use NewFromEnv, or New method instead.
func NewClient() *Client {
client, err := NewFromEnv()
if err != nil {
panic(err)
}
return ((*Client)(client))
}
// NewFromEnv bootstraps Cortexbot from environment variables.
func NewFromEnv() (*Cortexbot, error) {
var (
bot *tgbotapi.BotAPI
err error
)
if tgTokenEnvValue == "" || cortexURLEnvValue == "" || cortexAPIKeyEnvValue == "" || cortexBotPWEnvValue == "" {
return nil, fmt.Errorf(
"not enough parameters: check that environment variables %s, %s, %s and %s are set",
tgTokenEnvName,
cortexURLEnvName,
cortexAPIKeyEnvName,
cortexBotPWEnvName,
)
}
if socksURLEnvValue != "" {
surl, err := url.Parse(socksURLEnvValue)
if err != nil {
return nil, err
}
sc, err := socks5Client(surl)
if err != nil {
return nil, err
}
bot, err = tgbotapi.NewBotAPIWithClient(tgTokenEnvValue, sc)
if err != nil {
return nil, err
}
} else {
bot, err = tgbotapi.NewBotAPI(tgTokenEnvValue)
if err != nil {
return nil, err
}
}
crtx, err := cortex.NewClient(cortexURLEnvValue, &cortex.ClientOpts{
Auth: &cortex.APIAuth{
APIKey: cortexAPIKeyEnvValue,
},
})
if err != nil {
return nil, err
}
db, err := sql.Open("sqlite3", sqliteFileName)
if err != nil {
return nil, err
}
timeout := cortexTimeout
if cortexTimeoutEnvValue != "" {
timeout, err = time.ParseDuration(cortexTimeoutEnvValue)
if err != nil {
return nil, err
}
}
var debug bool
debug, err = strconv.ParseBool(debugEnvValue)
if err != nil {
debug = false
}
c := &Cortexbot{
Bot: bot,
Cortex: crtx,
Password: cortexBotPWEnvValue,
DB: db,
TLP: defaultTLP,
PAP: defaultPAP,
Timeout: timeout,
Debug: debug,
}
err = c.createUsersTbl()
if err != nil {
return nil, err
}
return c, nil
}
// SetupChatbot constructs a client to the messenger.
func SetupChatbot(token string, client *http.Client) func(*Cortexbot) error {
return func(c *Cortexbot) error {
if client == nil {
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
return err
}
c.Bot = bot
return nil
}
bot, err := tgbotapi.NewBotAPIWithClient(token, client)
if err != nil {
return err
}
c.Bot = bot
return nil
}
}
// SetCortex sets a cortex.Client.
func SetCortex(client *cortex.Client) func(*Cortexbot) error {
return func(c *Cortexbot) error {
c.Cortex = client
return nil
}
}
// SetDBPath sets a sqlitedb path.
func SetDBPath(n string) func(*Cortexbot) error {
return func(c *Cortexbot) error {
c.dbpath = n
return nil
}
}
// SetTLP sets TLP as an option.
func SetTLP(tlp cortex.TLP) func(*Cortexbot) error {
return func(c *Cortexbot) error {
c.TLP = tlp
return nil
}
}
// SetPAP sets PAP as an option.
func SetPAP(pap cortex.PAP) func(*Cortexbot) error {
return func(c *Cortexbot) error {
c.PAP = pap
return nil
}
}
// SetCortexTimeout will set the timeout that the client will wait for a
// response from Cortex at most.
func SetCortexTimeout(t time.Duration) func(*Cortexbot) error {
return func(c *Cortexbot) error {
c.Timeout = t
return nil
}
}
// Debug sets debug mode for the Cortexbot.
func Debug() func(*Cortexbot) error {
return func(c *Cortexbot) error {
c.Debug = true
return nil
}
}
// New bootstraps cortexbot configuration.
func New(opts ...func(*Cortexbot) error) (*Cortexbot, error) {
cortexbot := &Cortexbot{
dbpath: sqliteFileName,
TLP: defaultTLP,
PAP: defaultPAP,
Timeout: cortexTimeout,
Debug: false,
}
for _, option := range opts {
err := option(cortexbot)
if err != nil {
return nil, err
}
}
return cortexbot, nil
}