-
Notifications
You must be signed in to change notification settings - Fork 19
/
telegram.go
144 lines (117 loc) · 3.61 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
package main
import "time"
import "github.com/go-telegram-bot-api/telegram-bot-api"
import "strconv"
import "fmt"
//TelegramBot is the main function used to communicate with Telegram chat and remote control
func TelegramBot(rigs []Rig) {
bot, err := tgbotapi.NewBotAPI(Config.TgAPIKey)
if err != nil {
log.Fatal("Problem with the API key. Fix the problem or disable the TelegramBot from the config file")
}
bot.Debug = false
log.Noticef("TelegramBot Authorteleized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
time.Sleep(time.Millisecond * 500)
updates.Clear()
tgbotapi.NewMessageToChannel(Config.TgAdminUserName, "TEST MSG")
for update := range updates {
if update.Message == nil {
continue
}
log.Noticef("[%s] %s", update.Message.From.UserName, update.Message.Text)
if update.Message.IsCommand() {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
commandArgs := update.Message.CommandArguments()
if update.Message.From.UserName != Config.TgAdminUserName {
msg.Text = "You are not my master"
} else {
switch update.Message.Command() {
case "help":
msg.Text = "there is no help for the people here\nbut you can try /status /config /turnon /turnoff /restart /ping"
case "status":
msg.Text = "I'm fine. Thanks for asking"
case "config":
msg.Text = handleConfig()
case "ping":
msg.Text = handlePing(rigs, commandArgs)
case "restart":
msg.Text = handleRestart(rigs, commandArgs)
case "turnon":
msg.Text = handleTurnOn(rigs, commandArgs)
case "turnoff":
msg.Text = handleTurnOff(rigs, commandArgs)
default:
msg.Text = "I don't know that command, try /help"
}
}
bot.Send(msg)
}
}
}
//handleConfig return string with raw data from config file
func handleConfig() string {
return fmt.Sprintf("Config:\nLogging: %t\nStartupcheck: %t\nTimer: %d\nMiners:\n%+v",
Config.Log, Config.StartupCheck, Config.WaitSeconds, Config.Miners)
}
//handlePing return information about the ping response from machine
func handlePing(rigs []Rig, commandArgs string) (s string) {
if commandArgs != "" {
args, _ := strconv.ParseInt(commandArgs, 10, 0)
if args >= int64(len(Config.Miners)) || args < 0 {
s = "Invalid number"
} else {
s = fmt.Sprintf("Machine online status: %t", rigs[args].Ping())
}
} else {
s = "Provide arguments"
}
return
}
//handleRestart restarts the machine and returns string
func handleRestart(rigs []Rig, commandArgs string) (s string) {
if commandArgs != "" {
args, _ := strconv.ParseInt(commandArgs, 10, 0)
if args >= int64(len(Config.Miners)) || args < 0 {
s = "Invalid number"
} else {
rigs[args].Restarter()
s = fmt.Sprintf("Machine %d was restarted", args)
}
} else {
s = "Provide arguments"
}
return
}
//handleTurnOn turns on the machine and returns string
func handleTurnOn(rigs []Rig, commandArgs string) (s string) {
if commandArgs != "" {
args, _ := strconv.ParseInt(commandArgs, 10, 0)
if args >= int64(len(Config.Miners)) || args < 0 {
s = "Invalid number"
} else {
rigs[args].TurnOn()
s = fmt.Sprintf("TURN ON Machine %d", args)
}
} else {
s = "Provide arguments"
}
return
}
//handleTurnOff restarts the machine and returns string
func handleTurnOff(rigs []Rig, commandArgs string) (s string) {
if commandArgs != "" {
args, _ := strconv.ParseInt(commandArgs, 10, 0)
if args >= int64(len(Config.Miners)) || args < 0 {
s = "Invalid number"
} else {
rigs[args].ForceShutDown()
s = fmt.Sprintf("TURN OFF Machine %d", args)
}
} else {
s = "Provide arguments"
}
return
}