-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.go
76 lines (63 loc) · 1.72 KB
/
menu.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
package main
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/labstack/gommon/log"
)
type ActionReply struct{
}
type ActionParams struct {
bot *tgbotapi.BotAPI
update *tgbotapi.Update
}
type Menuer interface {
DefaultAction() string
RunAction(string, *ActionParams) (ActionReply, error)
}
type Menu struct {
Channel *chan tgbotapi.Update
States map[string] func(*ActionParams)(error, ActionReply)
}
func newMenu(c chan *tgbotapi.Update)(Menuer){
m := new(Menu)
return m
}
func (m *Menu) DefaultAction() string {
return ""
}
func (m *Menu) alertmanagerMenu(params *ActionParams) {
log.Printf("We are in alertmanager menu handler")
state := findUserState(params.update.Message.Chat.ID)
state.CurMenu = newAlertmanagerMenu(globalChan)
state.Action = ""
setUserState(state)
globalChan <- params.update
}
func (m *Menu) handleMain(params *ActionParams){
switch params.update.Message.Text {
case "alertmanager":
m.alertmanagerMenu(params)
}
}
func (m *Menu) RunAction(action string, params *ActionParams) (ActionReply, error){
switch action {
case "handlemain":
m.handleMain(params)
default:
m.Draw(params)
}
return ActionReply{}, nil
}
func (m *Menu) Draw(params *ActionParams){
menuKeys := []tgbotapi.KeyboardButton{}
menuKeys = append(menuKeys, tgbotapi.NewKeyboardButton("alertmanager"))
menuKeys = append(menuKeys, tgbotapi.NewKeyboardButton("close"))
msg := tgbotapi.NewMessage(params.update.Message.Chat.ID, "MainMenu")
var keyboard = tgbotapi.NewReplyKeyboard(menuKeys)
keyboard.OneTimeKeyboard = true
msg.ReplyMarkup = keyboard
state := findUserState(params.update.Message.Chat.ID)
state.PrevMenu = state.CurMenu
state.Action = "handlemain"
setUserState(state)
params.bot.Send(msg)
}