/
command_select_command.go
65 lines (54 loc) · 1.81 KB
/
command_select_command.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
package ssh
import (
"context"
"github.com/weAutomateEverything/go2hal/telegram"
"gopkg.in/telegram-bot-api.v4"
"log"
)
type sshSelect struct {
store Store
telegramStore telegram.Store
telegramService telegram.Service
}
func NewSshSelectCommand(store Store,
telegramStore telegram.Store,
telegramService telegram.Service) telegram.Commandlet {
return &sshSelect{
telegramService: telegramService,
store: store,
telegramStore: telegramStore,
}
}
func (sshSelect) CanExecute(update tgbotapi.Update, state telegram.State) bool {
return state.State == "SSH_COMMAND"
}
func (s sshSelect) Execute(ctx context.Context, update tgbotapi.Update, state telegram.State) {
id, err := s.telegramStore.GetUUID(update.Message.Chat.ID, update.Message.Chat.Description)
if err != nil {
log.Println(err)
s.telegramService.SendMessage(ctx, update.Message.Chat.ID, "Technical Error - please let the devs know", update.Message.MessageID)
return
}
servers, err := s.store.getServers(id)
if err != nil {
log.Println(err)
s.telegramService.SendMessage(ctx, update.Message.Chat.ID, "Technical Error - please let the devs know", update.Message.MessageID)
return
}
buttons := make([]string, len(servers))
for x, server := range servers {
buttons[x] = server.Description
}
_, err = s.telegramService.SendKeyboard(ctx, buttons, "On which server should this command run? ", update.Message.Chat.ID)
if err != nil {
log.Println(err)
s.telegramService.SendMessage(ctx, update.Message.Chat.ID, "Technical Error - please let the devs know", update.Message.MessageID)
return
}
}
func (sshSelect) NextState(update tgbotapi.Update, state telegram.State) string {
return "SSH_SERVER"
}
func (sshSelect) Fields(update tgbotapi.Update, state telegram.State) []string {
return []string{update.Message.Text}
}