-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.go
82 lines (65 loc) · 1.69 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
package telegram
import (
"fmt"
"log"
"strings"
"unicode"
"github.com/ashwinath/kubetelebot/pkg/shell"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type Telegram struct {
bot *tgbotapi.BotAPI
allowedUser string
}
func New(
apiKey string,
isDebug bool,
allowedUser string,
) (*Telegram, error) {
bot, err := tgbotapi.NewBotAPI(apiKey)
if err != nil {
return nil, err
}
bot.Debug = isDebug
log.Printf("Authorized on account %s", bot.Self.UserName)
return &Telegram{
bot: bot,
allowedUser: allowedUser,
}, nil
}
func (t *Telegram) Run() {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := t.bot.GetUpdatesChan(u)
for update := range updates {
if update.Message.From.UserName == t.allowedUser && update.Message != nil { // If we got a message
log.Printf("[%s to bot] %s", update.Message.From.UserName, update.Message.Text)
args := strings.Split(firstToLower(update.Message.Text), " ")
reply, err := shell.RunShell("kubectl", args...)
if err != nil {
errString := err.Error()
reply = &errString
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, wrapInMonospace(reply))
msg.ReplyToMessageID = update.Message.MessageID
msg.ParseMode = "Markdown"
_, err = t.bot.Send(msg)
if err != nil {
log.Printf("[bot to %s] error: %s", update.Message.From.UserName, err)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Message was too long to respond.")
t.bot.Send(msg)
}
}
}
}
func firstToLower(s string) string {
if len(s) == 0 {
return s
}
r := []rune(s)
r[0] = unicode.ToLower(r[0])
return string(r)
}
func wrapInMonospace(text *string) string {
return fmt.Sprintf("```\n%s\n```", *text)
}