-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (54 loc) · 1.35 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"os"
"sync"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/nikonor/quickgobot/conf"
"github.com/nikonor/quickgobot/reader"
"github.com/nikonor/quickgobot/writer"
)
type Reader interface {
Handler(idx int, wg *sync.WaitGroup, doneChan <-chan struct{}, updates <-chan tgbotapi.Update, wChan chan writer.Message)
}
type Writer interface {
Handler(idx int, wg *sync.WaitGroup, doneChan <-chan struct{}, tbot *tgbotapi.BotAPI)
}
func main() {
token, ok := os.LookupEnv("TLG_TOKEN")
if !ok {
panic("wrong token")
}
cfg, err := conf.Load("./config.json")
if err != nil {
panic(err.Error())
}
tbot, err := tgbotapi.NewBotAPI(token)
if err != nil {
panic(err.Error())
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
// TODO: webhook
whInfo, err := tbot.GetWebhookInfo()
if err != nil {
panic(err.Error())
}
if whInfo.IsSet() {
if _, err = tbot.Send(tgbotapi.DeleteWebhookConfig{DropPendingUpdates: true}); err != nil {
panic(err.Error())
}
}
doneChan := make(chan struct{})
updates := tbot.GetUpdatesChan(u)
wg := new(sync.WaitGroup)
r := reader.NewReader()
w := writer.NewWriter()
ch := w.Chan()
// TODO: слушаем сигналы
for i := 0; i < cfg.Workers; i++ {
wg.Add(2)
go w.Handler(i+1, wg, doneChan, tbot)
go r.Handler(i+1, wg, doneChan, updates, ch)
}
wg.Wait()
}