-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtgbot.go
299 lines (266 loc) · 8.69 KB
/
tgbot.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"encoding/json"
"fmt"
"log"
"regexp"
"strconv"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func startbot() {
bot, err := tgbotapi.NewBotAPI(cfg.TgToken)
if err != nil {
log.Println("Error creating bot", err)
return
}
bot.Debug = false
log.Printf("Authorized on account %s", bot.Self.UserName)
setBotCommands(bot)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message != nil { // If we got a message
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
if update.Message.IsCommand() {
if update.Message.Command() == "akall" {
var ret []Data
db.Model(&Data{}).Find(&ret)
var mm []M
for _, v := range ret {
var m M
json.Unmarshal([]byte(v.Data), &m)
mm = append(mm, m)
}
var online int
var cpu int
var mem uint64
var memused uint64
var downspeed uint64
var upspeed uint64
var downflow uint64
var upflow uint64
var swapused uint64
var swap uint64
time_now := time.Now().Unix()
for _, v := range mm {
if v.TimeStamp > time_now-30 {
online++
}
cpu += parseCPU(v.Host.CPU[0])
mem += v.Host.MemTotal
memused += v.State.MemUsed
swap += v.Host.SwapTotal
swapused += v.State.SwapUsed
downspeed += v.State.NetInSpeed
upspeed += v.State.NetOutSpeed
downflow += v.State.NetInTransfer
upflow += v.State.NetOutTransfer
}
//流量对等性
var duideng string
if downflow > upflow {
duideng = fmt.Sprintf("%.2f%%", float64(upflow)/float64(downflow)*100)
} else {
duideng = fmt.Sprintf("%.2f%%", float64(downflow)/float64(upflow)*100)
}
msg := fmt.Sprintf(`统计信息
===========================
服务器数量: %d
在线服务器: %d
CPU核心数: %d
内存: %s [%s/%s]
交换分区: %s [%s/%s]
下行速度: ↓%s/s
上行速度: ↑%s/s
下行流量: ↓%s
上行流量: ↑%s
流量对等性: %s
更新于:%s UTC`,
len(mm), online, cpu,
fmt.Sprintf("%.2f%%", float64(memused)/float64(mem)*100),
formatSize(memused), formatSize(mem),
fmt.Sprintf("%.2f%%", float64(swapused)/float64(swap)*100),
formatSize(swapused), formatSize(swap),
formatSize(downspeed), formatSize(upspeed),
formatSize(downflow), formatSize(upflow),
duideng,
time.Now().UTC().Format("2006-01-02 15:04:05"),
)
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, msg))
} else if update.Message.Command() == "id" {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("你的ID是: %d", update.Message.From.ID))
bot.Send(msg)
} else if update.Message.Command() == "server" {
var servers []Data
db.Model(&Data{}).Find(&servers)
if len(servers) == 0 {
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "没有找到任何服务器信息。"))
return
}
var rows [][]tgbotapi.InlineKeyboardButton
row := []tgbotapi.InlineKeyboardButton{}
for i, server := range servers {
callbackData := fmt.Sprintf("/status %s", server.Name)
row = append(row, tgbotapi.NewInlineKeyboardButtonData(server.Name, callbackData))
if (i+1)%2 == 0 {
rows = append(rows, row)
row = []tgbotapi.InlineKeyboardButton{}
}
}
if len(row) > 0 {
rows = append(rows, row)
}
keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "请选择一个服务器:")
msg.ReplyMarkup = keyboard
bot.Send(msg)
} else if update.Message.Command() == "status" {
serverName := update.Message.CommandArguments()
if serverName == "" {
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "用法: /status <服务器名称>"))
} else {
sendOrEditServerStatus(bot, update.Message.Chat.ID, 0, serverName)
}
}
}
} else if update.CallbackQuery != nil {
callbackData := update.CallbackQuery.Data
if len(callbackData) > 8 && callbackData[:8] == "/status " {
serverName := callbackData[8:]
// 编辑原消息并展示服务器信息
sendOrEditServerStatus(bot, update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Message.MessageID, serverName)
}
}
}
}
// 设置命令列表
func setBotCommands(bot *tgbotapi.BotAPI) {
commands := []tgbotapi.BotCommand{
{Command: "id", Description: "查看你的 Telegram 用户 ID"},
{Command: "akall", Description: "查看所有服务器的统计信息"},
{Command: "server", Description: "获取服务器列表"},
{Command: "status", Description: "查看某个服务器状态"},
}
// 创建命令配置
_, err := bot.Request(tgbotapi.NewSetMyCommands(commands...))
if err != nil {
log.Println("设置命令失败:", err)
} else {
log.Println("命令设置成功")
}
}
// 新建/编辑消息展示单个服务器信息
func sendOrEditServerStatus(bot *tgbotapi.BotAPI, chatID int64, messageID int, serverName string) {
var data Data
if err := db.First(&data, "name = ?", serverName).Error; err != nil {
// messageID != 0 说明消息来自点击列表中按钮回调, 只需要编辑原信息; 否则消息来自直接调用 /status 命令, 需要新建信息
if messageID != 0 {
editMsg := tgbotapi.NewEditMessageText(chatID, messageID, "未找到指定的服务器信息。")
bot.Send(editMsg)
} else {
bot.Send(tgbotapi.NewMessage(chatID, "未找到指定的服务器信息。"))
}
return
}
var m M
if err := json.Unmarshal([]byte(data.Data), &m); err != nil {
// messageID != 0 说明消息来自点击列表中按钮回调, 只需要编辑原信息; 否则消息来自直接调用 /status 命令, 需要新建信息
if messageID != 0 {
editMsg := tgbotapi.NewEditMessageText(chatID, messageID, "解析服务器数据失败。")
bot.Send(editMsg)
} else {
bot.Send(tgbotapi.NewMessage(chatID, "解析服务器数据失败。"))
}
return
}
msgText := formatServerMessage(serverName, &m)
if messageID != 0 {
editMsg := tgbotapi.NewEditMessageText(chatID, messageID, msgText)
bot.Send(editMsg)
} else {
bot.Send(tgbotapi.NewMessage(chatID, msgText))
}
}
// 格式化服务器信息
func formatServerMessage(serverName string, m *M) string {
//流量对等性
var duideng string
if m.State.NetInTransfer > m.State.NetOutTransfer {
duideng = fmt.Sprintf("%.2f%%", float64(m.State.NetOutTransfer)/float64(m.State.NetInTransfer)*100)
} else {
duideng = fmt.Sprintf("%.2f%%", float64(m.State.NetInTransfer)/float64(m.State.NetOutTransfer)*100)
}
return fmt.Sprintf(`服务器: %s
CPU核心数: %d
内存: %s [%s/%s]
交换分区: %s [%s/%s]
下行速度: ↓%s/s
上行速度: ↑%s/s
下行流量: ↓%s
上行流量: ↑%s
流量对等性: %s
运行时间: %s
更新于: %s UTC`,
serverName,
parseCPU(m.Host.CPU[0]),
fmt.Sprintf("%.2f%%", float64(m.State.MemUsed)/float64(m.Host.MemTotal)*100),
formatSize(m.State.MemUsed),
formatSize(m.Host.MemTotal),
fmt.Sprintf("%.2f%%", float64(m.State.SwapUsed)/float64(m.Host.SwapTotal)*100),
formatSize(m.State.SwapUsed),
formatSize(m.Host.SwapTotal),
formatSize(m.State.NetInSpeed),
formatSize(m.State.NetOutSpeed),
formatSize(m.State.NetInTransfer),
formatSize(m.State.NetOutTransfer),
duideng,
time.Duration(m.State.Uptime)*time.Second,
time.Now().UTC().Format("2006-01-02 15:04:05"),
)
}
// 解析CPU数量
func parseCPU(cpu string) int {
re := regexp.MustCompile(`(\d+) (Virtual) Core`)
// 查找匹配项
matches := re.FindStringSubmatch(cpu)
if len(matches) >= 2 {
virtualCores := matches[1]
vint, err := strconv.Atoi(virtualCores)
if err != nil {
return 0
}
return vint
}
return 0
}
// 格式化字节大小
func formatSize(size uint64) string {
if size < 1024 {
return fmt.Sprintf("%d B", size)
} else if size < 1024*1024 {
return fmt.Sprintf("%.2f KB", float64(size)/1024)
} else if size < 1024*1024*1024 {
return fmt.Sprintf("%.2f MB", float64(size)/1024/1024)
} else if size < 1024*1024*1024*1024 {
return fmt.Sprintf("%.2f GB", float64(size)/1024/1024/1024)
} else if size < 1024*1024*1024*1024*1024 {
return fmt.Sprintf("%.2f TB", float64(size)/1024/1024/1024/1024)
} else if size < 1024*1024*1024*1024*1024*1024 {
return fmt.Sprintf("%.2f PB", float64(size)/1024/1024/1024/1024/1024)
} else {
return fmt.Sprintf("%.2f EB", float64(size)/1024/1024/1024/1024/1024/1024)
}
}
func SendTGMessage(msg string) {
bot, err := tgbotapi.NewBotAPI(cfg.TgToken)
if err != nil {
log.Println("Error creating bot", err)
return
}
bot.Debug = false
log.Printf("Authorized on account %s", bot.Self.UserName)
msgs := tgbotapi.NewMessage(cfg.TgChatID, msg)
bot.Send(msgs)
}