-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer_tgbotapi.go
113 lines (94 loc) · 2.96 KB
/
renderer_tgbotapi.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
package botform
import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
func RenderChoiceControl(bckc BotChoiceKeyboardControl) tgbotapi.Chattable {
m := tgbotapi.NewMessage(0, "")
m.Text = bckc.Title
row := []tgbotapi.KeyboardButton{}
rows := [][]tgbotapi.KeyboardButton{}
controlRow := []tgbotapi.KeyboardButton{}
ssmChoices := SortedStringMap(bckc.ControlChoices)
ssmChoices.Range(func(k, choice string) bool {
controlRow = append(controlRow, tgbotapi.NewKeyboardButton(choice))
return true
}, false)
if len(controlRow) > 0 {
rows = append(rows, controlRow)
}
ssmMainChoices := SortedStringMap(bckc.Choices)
ssmMainChoices.Range(func(k, choice string) bool {
row = append(row, tgbotapi.NewKeyboardButton(choice))
if len(row) >= bckc.ButtonsPerRow {
rows = append(rows, row)
row = []tgbotapi.KeyboardButton{}
}
return true
}, true)
if len(row) > 0 {
rows = append(rows, row)
}
rk := tgbotapi.NewReplyKeyboard(rows...)
m.ReplyMarkup = rk
return m
}
func RenderTextControl(text BotTextKeyboardControl) tgbotapi.Chattable {
m := tgbotapi.NewMessage(0, "")
m.Text = text.Title
row := []tgbotapi.KeyboardButton{}
rows := [][]tgbotapi.KeyboardButton{}
ssmChoices := SortedStringMap(text.ControlChoices)
ssmChoices.Range(func(k, choice string) bool {
row = append(row, tgbotapi.NewKeyboardButton(choice))
return true
}, false)
rows = append(rows, row)
rk := tgbotapi.NewReplyKeyboard(rows...)
m.ReplyMarkup = rk
return m
}
func RenderMultiChoiceControl(multichoice BotMultiChoiceKeyboardControl) tgbotapi.Chattable {
bckc := multichoice
m := tgbotapi.NewMessage(0, "")
m.Text = bckc.Title
row := []tgbotapi.KeyboardButton{}
rows := [][]tgbotapi.KeyboardButton{}
controlRow := []tgbotapi.KeyboardButton{}
ssmChoices := SortedStringMap(bckc.ControlChoices)
ssmChoices.Range(func(k, choice string) bool {
controlRow = append(controlRow, tgbotapi.NewKeyboardButton(choice))
return true
}, false)
if len(controlRow) > 0 {
rows = append(rows, controlRow)
}
ssmMainChoices := SortedStringMap(bckc.Choices)
ssmMainChoices.Range(func(k, choice string) bool {
_, existsInSelected := bckc.SelectedValues[choice]
if existsInSelected {
choice = bckc.SelectedPrefix + choice
}
row = append(row, tgbotapi.NewKeyboardButton(choice))
if len(row) >= bckc.ButtonsPerRow {
rows = append(rows, row)
row = []tgbotapi.KeyboardButton{}
}
return true
}, true)
if len(row) > 0 {
rows = append(rows, row)
}
rk := tgbotapi.NewReplyKeyboard(rows...)
m.ReplyMarkup = rk
return m
}
func RenderToTgBotApi(control BotControl) tgbotapi.Chattable {
switch control.(type) {
case BotChoiceKeyboardControl:
return RenderChoiceControl(control.(BotChoiceKeyboardControl))
case BotTextKeyboardControl:
return RenderTextControl(control.(BotTextKeyboardControl))
case BotMultiChoiceKeyboardControl:
return RenderMultiChoiceControl(control.(BotMultiChoiceKeyboardControl))
default:
panic("Unsupported control type to render")
}
}