/
telegram_handler.go
218 lines (165 loc) · 5.36 KB
/
telegram_handler.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
package teleterm
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/alfiankan/teleterm/v2/common"
"github.com/alfiankan/teleterm/v2/executor"
"github.com/spf13/viper"
tele "gopkg.in/telebot.v3"
"gopkg.in/telebot.v3/middleware"
)
const (
formatOk = `
Running : %s
===========
Out :
===========
%s`
formatErr = `
Running : %s
===========
Err :
===========
%s`
)
func createButtonReplay(ctx context.Context, persist Persistence, menu *tele.ReplyMarkup) (teleMenus []tele.Row, ere error) {
var menus []tele.Row
buttons, err := persist.GetAllButtons(ctx)
if err != nil {
return
}
for _, button := range buttons {
menus = append(menus, menu.Row(menu.Text(fmt.Sprintf("/run %s", button.Name))))
}
teleMenus = menus
return
}
func Start(ctx context.Context, db *sql.DB, telebotToken string) {
log := common.InitLog()
log.Info().Str("version", "v2.2.0").Msg("Starting Teleterm")
commandExecutor := executor.CommandOutputWriter{
TimeoutSecond: viper.GetInt("execution_timeout_second"),
}
persist := Persistence{db: db}
pref := tele.Settings{
Token: telebotToken,
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
}
b, err := tele.NewBot(pref)
if err != nil {
log.Error().Str("state", "init bot").Msg(err.Error())
return
}
configWhitelist := viper.GetIntSlice("whitelist")
log.Info().Str("state", "white listing").Msg(fmt.Sprintf("total: %v", len(configWhitelist)))
if len(configWhitelist) > 0 {
var whitelist []int64
for _, id := range configWhitelist {
whitelist = append(whitelist, int64(id))
}
b.Use(middleware.Whitelist(whitelist...))
}
menu := &tele.ReplyMarkup{ResizeKeyboard: true}
b.Handle("/refresh", func(c tele.Context) error {
menus, err := createButtonReplay(ctx, persist, menu)
if err != nil {
log.Error().Str("state", "refresh bot").Msg(err.Error())
return c.Reply(err.Error())
}
menu.Reply(menus...)
return c.Reply("Bot Reloaded", menu)
})
b.Handle("/addbutton", func(c tele.Context) error {
log.Info().Str("state", "exec").Msg(c.Message().Payload)
payload := strings.Split(c.Message().Payload, "!!")
err := persist.AddButton(ctx, payload[0], payload[1])
if err != nil {
log.Error().Str("state", "add button").Msg(err.Error())
return c.Reply(err.Error())
}
menus, err := createButtonReplay(ctx, persist, menu)
if err != nil {
log.Error().Str("state", "create reply button").Msg(err.Error())
return c.Reply(err.Error())
}
menu.Reply(menus...)
return c.Reply("Button created", menu)
})
b.Handle("/deletebutton", func(c tele.Context) error {
log.Info().Str("state", "exec").Msg(c.Message().Payload)
err := persist.DeleteButtonsByName(ctx, c.Message().Payload)
if err != nil {
log.Error().Str("state", "delete button").Msg(err.Error())
return c.Reply(err.Error())
}
menus, err := createButtonReplay(ctx, persist, menu)
if err != nil {
log.Error().Str("state", "create reply button").Msg(err.Error())
return c.Reply(err.Error())
}
menu.Reply(menus...)
return c.Reply("Button deleted", menu)
})
b.Handle("/run", func(c tele.Context) error {
commandName := c.Message().Payload
log.Info().Str("state", "exec").Msg(commandName)
button, err := persist.GetButtonByname(ctx, commandName)
if err != nil {
outOk, outErr, errExecution := commandExecutor.ExecFullOutput(commandName)
if errExecution != nil {
log.Error().Str("state", "exec").Msg(errExecution.Error())
return c.Reply(fmt.Sprintf(formatErr, commandName, outErr))
}
return c.Reply(fmt.Sprintf(formatOk, commandName, outOk))
}
outOk, outErr, errExecution := commandExecutor.ExecFullOutput(button.Cmd)
if errExecution != nil {
log.Error().Str("state", "exec").Msg(err.Error())
return c.Reply(fmt.Sprintf(formatErr, button.Cmd, outErr))
}
return c.Reply(fmt.Sprintf(formatOk, button.Cmd, outOk))
})
b.Handle("/runtail", func(c tele.Context) error {
commandName := c.Message().Payload
log.Info().Str("state", "exec").Msg(commandName)
button, err := persist.GetButtonByname(ctx, commandName)
if err != nil {
outOk, outErr, errExecution := commandExecutor.ExecTailOutput(commandName)
if errExecution != nil {
log.Error().Str("state", "exec").Msg(err.Error())
return c.Reply(fmt.Sprintf(formatErr, commandName, outErr))
}
return c.Reply(fmt.Sprintf(formatOk, commandName, outOk))
}
outOk, outErr, errExecution := commandExecutor.ExecFullOutput(button.Cmd)
if errExecution != nil {
log.Error().Str("state", "exec").Msg(err.Error())
return c.Reply(fmt.Sprintf(formatErr, button.Cmd, outErr))
}
return c.Reply(fmt.Sprintf(formatOk, button.Cmd, outOk))
})
// send document
b.Handle("/getfile", func(c tele.Context) error {
fileName := c.Message().Payload
log.Info().Str("state", "download file").Msg(fileName)
file := tele.FromDisk(fileName)
return c.Reply(&tele.Document{File: file})
})
// receive document
b.Handle(tele.OnDocument, func(c tele.Context) error {
log.Info().Str("state", "upload file").Msg(c.Message().Document.FileID)
savePath := "./"
if c.Message().Caption != "" {
savePath = c.Message().Caption
}
common.DownloadFile(
fmt.Sprintf("%s/%s", savePath, c.Message().Document.FileName),
fmt.Sprintf("https://api.telegram.org/bot%s/getFile?file_id=%s", viper.GetString("teleterm.telegram_token"), c.Message().Document.FileID),
)
return c.Reply("File Uploaded")
})
b.Start()
}