forked from MrMarble/teledock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.go
204 lines (173 loc) · 4.69 KB
/
telegram.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
package main
import (
"fmt"
"time"
tb "gopkg.in/tucnak/telebot.v2"
"github.com/rs/zerolog/log"
)
// Telegram represents the telegram bot
type Telegram struct {
bot *tb.Bot
handlersRegistered bool
}
// Command represent a telegram command
type Command struct {
Cmd string
Aliases []string
Description string
Handler interface{}
}
// NewBot returns a Telegram bot
func NewBot(token string) (*Telegram, error) {
bot, err := tb.NewBot(tb.Settings{
Token: token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
Reporter: func(err error) {
log.Error().Str("module", "telegram").Err(err).Msg("telebot internal error")
},
})
if err != nil {
return nil, err
}
log.Info().Str("module", "telegram").Int("id", bot.Me.ID).Str("name", bot.Me.FirstName).Str("username", bot.Me.Username).Msg("connected to telegram")
return &Telegram{bot: bot}, nil
}
// Start starts polling for telegram updates
func (t *Telegram) Start() {
t.registerHandlers()
log.Info().Str("module", "telegram").Msg("start polling")
t.bot.Start()
}
// RegisterHandlers registers all the handlers
func (t *Telegram) registerHandlers() {
if t.handlersRegistered {
return
}
log.Info().Str("module", "telegram").Msg("registering handlers")
// Temporal list to send the commands to telegram
botCommandList := []tb.Command{}
var botCommands = []Command{
{
Handler: t.handleStart,
Cmd: "start",
Description: "Shows info",
},
{
Handler: t.handleList,
Cmd: "ps",
Aliases: []string{"ls", "list"},
Description: "List running containers",
},
{
Handler: t.handleListAll,
Cmd: "psa",
Aliases: []string{"lsa", "listall"},
Description: "List all containers",
},
{
Handler: t.handleStop,
Cmd: "stop",
Aliases: []string{"down"},
Description: "Stop a running container. <ContainerID>",
},
{
Handler: t.handleStartContainer,
Cmd: "run",
Description: "Start a stopped container. <ContainerID>",
},
{
Handler: t.handleInspect,
Cmd: "inspect",
Aliases: []string{"describe"},
Description: "Inspect a container. <ContainerID>",
},
{
Handler: t.handleStacks,
Cmd: "stacks",
Aliases: []string{"lss", "liststacks"},
Description: "Lists all compose stacks",
},
{
Handler: t.handleLogs,
Cmd: "logs",
Description: "Shows container logs. <ContainerID> <tail>",
},
}
for _, command := range botCommands {
botCommandList = append(botCommandList, tb.Command{
Text: command.Cmd,
Description: command.Description,
})
for _, alias := range command.Aliases {
t.bot.Handle(fmt.Sprintf("/%s", alias), command.Handler)
}
t.bot.Handle(fmt.Sprintf("/%s", command.Cmd), command.Handler)
}
t.bot.Handle(tb.OnCallback, t.handleCallback)
if err := t.bot.SetCommands(botCommandList); err != nil {
log.Fatal().Str("module", "telegram").Err(err).Msg("error registering commands")
}
t.handlersRegistered = true
}
func (t *Telegram) isSuperAdmin(user *tb.User) bool {
for _, uid := range superAdmins {
if int64(user.ID) == uid {
return true
}
}
return false
}
// send sends a message with error logging and retries
func (t *Telegram) send(to tb.Recipient, what interface{}, options ...interface{}) *tb.Message {
hasParseMode := false
for _, opt := range options {
if _, hasParseMode = opt.(tb.ParseMode); hasParseMode {
break
}
}
if !hasParseMode {
options = append(options, tb.ModeHTML)
}
try := 1
for {
msg, err := t.bot.Send(to, what, options...)
if err == nil {
return msg
}
if try > 5 {
log.Error().Str("module", "telegram").Err(err).Msg("send aborted, retry limit exceeded")
return nil
}
backoff := time.Second * 5 * time.Duration(try)
log.Warn().Str("module", "telegram").Err(err).Str("sleep", backoff.String()).Msg("send failed, sleeping and retrying")
time.Sleep(backoff)
try++
}
}
// reply replies a message with error logging and retries
func (t *Telegram) reply(to *tb.Message, what interface{}, options ...interface{}) *tb.Message {
hasParseMode := false
for _, opt := range options {
if _, hasParseMode = opt.(tb.ParseMode); hasParseMode {
break
}
}
if !hasParseMode {
options = append(options, tb.ModeHTML)
}
try := 1
for {
msg, err := t.bot.Reply(to, what, options...)
if err == nil {
return msg
}
if try > 5 {
log.Error().Str("module", "telegram").Err(err).Msg("reply aborted, retry limit exceeded")
return nil
}
backoff := time.Second * 5 * time.Duration(try)
log.Warn().Str("module", "telegram").Err(err).Str("sleep", backoff.String()).Msg("reply failed, sleeping and retrying")
time.Sleep(backoff)
try++
}
}