-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (61 loc) · 2.31 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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"gopkg.in/telebot.v3"
)
func main() {
token := os.Getenv("K8S_KUBECTL_BOT_TOKEN")
if token == "" {
log.Fatal("K8S_KUBECTL_BOT_TOKEN is not set")
}
b, err := telebot.NewBot(telebot.Settings{
Token: token,
})
if err != nil {
log.Fatal(err)
}
b.Handle(telebot.OnText, func(c telebot.Context) error {
// If you want you can check if the user is authorized
// by adding a check using c.Sender().Username here if necessary
userCommand := c.Text()
log.Printf("Received command: \"%s\" from user %s", userCommand, c.Sender().Username)
// Determine the command prefix
if strings.HasPrefix(userCommand, "kubectl") {
// userCommand = "kubectl get pods -n monitoring"
// strings.TrimPrefix(userCommand, "kubectl") removes kubectl --> " get pods -n monitoring"
// strings.TrimSpace(..) removes any leading spaces --> "get pods -n monitoring"
userCommand = strings.TrimSpace(strings.TrimPrefix(userCommand, "kubectl"))
} else if strings.HasPrefix(userCommand, "k") {
// same thing as before but for command that start with k
userCommand = strings.TrimSpace(strings.TrimPrefix(userCommand, "k"))
} else {
return c.Send("Invalid command. Please start your command with `kubectl` or `k`.")
}
// The command is split for exec.Command
commandParts := strings.Fields(userCommand)
if len(commandParts) == 0 {
return c.Send("No command provided after prefix.")
}
// Even if the user used k the command will start with kubectl
cmd := exec.Command("kubectl", commandParts...)
output, err := cmd.CombinedOutput()
if err != nil {
return c.Send(fmt.Sprintf("Failed to execute kubectl: %s\n%s", err.Error(), string(output)))
}
response := string(output)
if len(response) > 4000 { // Telegram message length limit is 4096
response = response[:4000] + "\n...output truncated..."
}
// Format the response string with MarkdownV2 syntax, wrapping the output in triple backticks to preserve
// the formatting when displayed in Telegram. In this way it is better if the output is very large
// like "kubectl get po -A -o wide"
response = fmt.Sprintf("```\n%s\n```", response)
return c.Send(response, &telebot.SendOptions{ParseMode: telebot.ModeMarkdownV2})
})
log.Println("k8s-kubectl-bot is running...")
b.Start()
}