-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
157 lines (133 loc) · 3.16 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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"telegram-shell-bot/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var numericKeyboard tgbotapi.ReplyKeyboardMarkup
var appSetting models.Setting
func main() {
bot, err := tgbotapi.NewBotAPI(appSetting.Token)
if err != nil {
log.Panic(err)
}
bot.Debug = appSetting.IsDebug
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil { // ignore non-Message updates
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
isValidWord := false
switch update.Message.Text {
case "open":
msg.ReplyMarkup = numericKeyboard
isValidWord = true
case "reset":
err = SetButtons()
if err != nil {
log.Println(err)
msg.Text = err.Error()
break
}
msg.ReplyMarkup = numericKeyboard
isValidWord = true
case "close":
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
isValidWord = true
}
for _, v := range appSetting.Buttons {
if v == msg.Text {
isValidWord = true
cmd := exec.Command("bash", appSetting.ShellLocation+v+".sh")
output, err := cmd.CombinedOutput()
if err != nil {
log.Println(err)
msg.Text = err.Error()
break
}
msg.Text = string(output)
}
}
if !isValidWord {
msg.Text = "not valid word"
}
if len(msg.Text) > 4096 { // Telegram limit
fmt.Println("yes")
msg.Text = fmt.Sprintf("%s\n...\n...\n...\nOVER TELEGRAM TEXTSIZE", msg.Text[0:4000])
}
if _, err := bot.Send(msg); err != nil {
log.Panic(err)
}
}
}
func init() {
err := error(nil)
err = SetSettings()
if err != nil {
panic(err)
}
if appSetting.Token == "" {
panic("token must be set")
}
if appSetting.RowButtonCount <= 0 {
panic("line button count must be set over 0")
}
err = SetButtons()
if err != nil {
panic(err)
}
}
func SetSettings() (err error) {
jsonFile, err := os.Open("./env/settings.json")
if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()
dataBytes, err := io.ReadAll(jsonFile)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(dataBytes, &appSetting)
return
}
func SetButtons() (err error) {
files, err := os.ReadDir(appSetting.ShellLocation)
if err != nil {
return
}
shellFiles := []string{}
for _, v := range files {
filename := v.Name()
if filepath.Ext(appSetting.ShellLocation+filename) == ".sh" {
shellFiles = append(shellFiles, filename[:len(filename)-3])
}
}
shellFilesLen := len(shellFiles)
if shellFilesLen == 0 {
err = errors.New("empty shell files")
return
}
wholeButtons := [][]tgbotapi.KeyboardButton{}
rowButtons := []tgbotapi.KeyboardButton{}
for i, v := range shellFiles {
appSetting.Buttons = append(appSetting.Buttons, v)
rowButtons = append(rowButtons, tgbotapi.NewKeyboardButton(v))
if (i+1)%appSetting.RowButtonCount == 0 || i+1 == shellFilesLen {
wholeButtons = append(wholeButtons, rowButtons)
rowButtons = []tgbotapi.KeyboardButton{}
}
}
numericKeyboard = tgbotapi.NewReplyKeyboard(wholeButtons...)
return
}