-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (136 loc) · 3.62 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
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
package main
import (
"net/http"
"os"
"os/signal"
"syscall"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
func newCollector(logger *zap.Logger) *collector {
return &collector{
logger: logger,
messageCount: prometheus.NewCounter(prometheus.CounterOpts{
Subsystem: "ont_watch_bot",
Name: "message_count",
Help: "how many requests performed bot",
}),
}
}
type collector struct {
logger *zap.Logger
messageCount prometheus.Counter
}
func (c *collector) IncMessage() {
c.messageCount.Inc()
}
func (c *collector) prometheusRegister() {
metrics := [...]prometheus.Collector{
c.messageCount,
}
for _, m := range metrics {
if err := prometheus.Register(m); err != nil {
c.logger.Error("failed to register in Prometheus", zap.Error(err))
}
}
}
func main() {
sigKillCh := make(chan os.Signal, 1) // Kills the process immediately (not implemented)
sigIntCh := make(chan os.Signal, 1) // Stops the server gracefully
sigHupCh := make(chan os.Signal, 1) // Reloads server configuration file (not implemented)
signal.Notify(sigKillCh, syscall.SIGKILL)
signal.Notify(sigIntCh, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(sigHupCh, syscall.SIGHUP)
debug := false
debugEnv := os.Getenv("DEBUG")
if "true" == debugEnv || "1" == debugEnv {
debug = true
}
InitGlobalLogger(debug)
logger := zap.L()
collector := newCollector(logger)
collector.prometheusRegister()
logger.Info("starting otn watch bot")
apiToken := os.Getenv("API_TOKEN")
if "" == apiToken {
logger.Info("You should expect API_TOKEN env variable")
message, err := getData()
logger.Info(message)
if nil != err {
logger.Info(err.Error())
}
return
}
bot, err := tgbotapi.NewBotAPI(apiToken)
if nil != err {
logger.Fatal("cannot create bot", zap.Error(err))
}
go func() {
http.Handle("/metrics", promhttp.Handler())
logger.Info("starting prometheus handler")
_ = http.ListenAndServe(":9123", nil)
}()
bot.Debug = debug
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
logger.Fatal("cannot get update channel", zap.Error(err))
}
for {
select {
case update := <-updates:
if nil == update.Message {
continue
}
switch update.Message.Text {
case "/rate":
message, err := getData()
if nil != err {
logger.Error("cannot get exchange data", zap.Error(err))
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
msg.ReplyToMessageID = update.Message.MessageID
_, err = bot.Send(msg)
if err != nil {
logger.Error("cannot send message", zap.Error(err))
}
collector.IncMessage()
}
case <-sigIntCh:
logger.Info("got SIGINT")
return
case <-sigHupCh:
logger.Info("got SIGHUP")
return
case <-sigKillCh:
logger.Info("got SIGKILL")
return
}
}
}
func InitGlobalLogger(debug bool) {
var zapConfig zap.Config
if debug {
zapConfig = zap.NewDevelopmentConfig()
} else {
zapConfig = zap.NewProductionConfig()
}
zapConfig.DisableStacktrace = true
zapConfig.DisableCaller = true
zapConfig.EncoderConfig.LevelKey = "level"
zapConfig.EncoderConfig.TimeKey = "timestamp"
zapConfig.EncoderConfig.MessageKey = "message"
zapConfig.EncoderConfig.CallerKey = "caller"
zapConfig.EncoderConfig.NameKey = "name"
zapConfig.EncoderConfig.StacktraceKey = "stack"
zapLogger, err := zapConfig.Build()
if err != nil {
panic("can not create logger: " + err.Error())
}
// After logger is configurated replace global logger
zap.ReplaceGlobals(zapLogger)
}