-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.go
137 lines (115 loc) · 3.15 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
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
package main
import (
"log"
"os"
tgbotapi "github.com/OvyFlash/telegram-bot-api"
"gopkg.in/yaml.v3"
)
type Menu struct {
Title string `yaml:"Title"`
Command string `yaml:"Command"`
Submenu []Menu `yaml:"Submenu"`
Flatmenu map[string]Menu
Url string `yaml:"Url"`
}
var menu []Menu
var flatMenu map[string]Menu
func startMenu() {
readMenu()
// Register command handlers
registerCommandHandlers()
}
func readMenu() {
// Parse menu.yaml
data, err := os.ReadFile("menu.yaml")
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(data, &menu)
if err != nil {
log.Fatal(err)
}
flatMenu = flattenMenu(menu)
}
func flattenMenu(menu []Menu) map[string]Menu {
flatItems := make(map[string]Menu)
for _, item := range menu {
fullCommand := "." + item.Command
var flatmenu map[string]Menu
if item.Submenu != nil {
flatmenu = flattenMenu(item.Submenu)
}
flatItems[fullCommand] = Menu{
Title: item.Title,
Command: fullCommand,
Submenu: item.Submenu,
Flatmenu: flatmenu,
Url: item.Url,
}
}
return flatItems
}
// Add commands for Menu button
func registerCommandHandlers() {
//Generate commands for Menu button
var commands []tgbotapi.BotCommand
commands = append(commands, tgbotapi.BotCommand{
Command: "/start",
Description: "Показать главное меню",
},
tgbotapi.BotCommand{
Command: "/triggers",
Description: "Список быстрых команд",
},
)
/*for _, item := range menu {
commands = append(commands, tgbotapi.BotCommand{
Command: item.Command,
Description: item.Text,
})
}*/
//Set menu for Private chats
scope := tgbotapi.NewBotCommandScopeAllPrivateChats()
menuConfig := tgbotapi.SetChatMenuButtonConfig{
MenuButton: &tgbotapi.MenuButton{
Type: "commands",
},
}
bot.Send(tgbotapi.NewSetMyCommandsWithScope(scope, commands...))
bot.Send(menuConfig)
}
func rootMenu() tgbotapi.InlineKeyboardMarkup {
return generateMenuKeyboard(menu, false)
}
func prepareCallbackMenuCommand(command string) string {
return "{\"command\": \"show_menu\", \"data\": \"" + command + "\"}"
}
// Used only for submenu
func replyWithMenu(item string) tgbotapi.InlineKeyboardMarkup {
for key, value := range flatMenu {
if key == "."+item {
if value.Flatmenu != nil {
return generateMenuKeyboard(value.Submenu, true)
}
}
}
return tgbotapi.NewInlineKeyboardMarkup()
}
func generateMenuKeyboard(menu []Menu, addBack bool) tgbotapi.InlineKeyboardMarkup {
var keyboard [][]tgbotapi.InlineKeyboardButton
for _, item := range menu {
var buttonRow []tgbotapi.InlineKeyboardButton
if item.Url != "" {
buttonRow = append(buttonRow, tgbotapi.NewInlineKeyboardButtonURL(item.Title, item.Url))
} else {
buttonRow = append(buttonRow, tgbotapi.NewInlineKeyboardButtonData(item.Title, prepareCallbackMenuCommand(item.Command)))
}
keyboard = append(keyboard, buttonRow)
}
if addBack {
var buttonRow []tgbotapi.InlineKeyboardButton
buttonRow = append(buttonRow, tgbotapi.NewInlineKeyboardButtonData("↩️", "{\"command\": \"show_root_menu\"}"))
keyboard = append(keyboard, buttonRow)
}
return tgbotapi.NewInlineKeyboardMarkup(keyboard...)
}