-
Notifications
You must be signed in to change notification settings - Fork 5
/
bot.go
85 lines (65 loc) · 1.82 KB
/
bot.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
package main
import (
"github.com/BoltzExchange/channel-bot/notifications/providers"
"github.com/BoltzExchange/channel-bot/utils"
"github.com/lightningnetwork/lnd/lnrpc"
"sync"
"github.com/google/logger"
)
func main() {
cfg := loadConfig()
initLogger(cfg.LogFile)
logConfig(cfg)
lndInfo := initLnd(cfg)
provider := getNotificationProvider(cfg)
err := provider.SendMessage("Started channel bot with LND node: **" + lndInfo.Alias + "** (`" + lndInfo.IdentityPubkey + "`)")
checkError("Notification provider", err)
var wg sync.WaitGroup
wg.Add(2)
go func() {
cfg.Notifications.Init(cfg.SignificantChannels, cfg.LogInsignificant, cfg.Lnd, provider)
wg.Done()
}()
go func() {
cfg.ChannelCleaner.Init(cfg.Lnd, provider)
wg.Done()
}()
wg.Wait()
logger.Info("Shutting down")
}
func initLnd(cfg *config) *lnrpc.GetInfoResponse {
logger.Info("Initializing LND client")
err := cfg.Lnd.Connect()
checkError("LND", err)
lndInfo, err := cfg.Lnd.GetInfo()
checkError("LND", err)
lndInfo.Features = nil
logger.Info("Initialized LND client: ", utils.Stringify(lndInfo))
return lndInfo
}
func getNotificationProvider(cfg *config) providers.NotificationProvider {
logger.Info("Initializing notification provider client")
for _, provider := range []providers.NotificationProvider{
cfg.Mattermost,
cfg.Discord,
} {
initialized := initNotificationProvider(provider)
if initialized != nil {
return initialized
}
}
return cfg.Discord
}
func initNotificationProvider(provider providers.NotificationProvider) providers.NotificationProvider {
err := provider.Init()
if err != nil {
logger.Warningf("Could not init %s: %s\n", provider.Name(), err.Error())
return nil
}
return provider
}
func checkError(service string, err error) {
if err != nil {
logger.Fatal("Could not initialize "+service+": ", err)
}
}