-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
78 lines (63 loc) · 2.06 KB
/
util.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
package bot
import (
"fmt"
"html"
"strings"
"github.com/d-Rickyy-b/gogeizhalsbot/v2/internal/bot/models"
"github.com/PaulSonOfLars/gotgbot/v2"
)
// createLink generates a clickable html link given a display name and a url
func createLink(url, name string) string {
name = strings.TrimSpace(name)
return fmt.Sprintf("<a href=\"%s\">%s</a>", url, html.EscapeString(name))
}
// createPrice formats a given float to a formatted pricetag string
func createPrice(price float64, currency string) string {
return fmt.Sprintf("%.2f %s", price, currency)
}
// bold encapsulates a string in a html <b> tag
func bold(text string) string {
text = strings.TrimSpace(text)
return fmt.Sprintf("<b>%s</b>", text)
}
// generateEntityKeyboard generates a gotgbot keyboard from a given list of entities
func generateEntityKeyboard(priceagents []models.PriceAgent, menuID string, numColumns int) gotgbot.InlineKeyboardMarkup {
var keyboard [][]gotgbot.InlineKeyboardButton
var row []gotgbot.InlineKeyboardButton //nolint:prealloc
colCounter := 0
for _, priceagent := range priceagents {
row = append(row, gotgbot.InlineKeyboardButton{
Text: priceagent.Name,
CallbackData: fmt.Sprintf("%s_%d", menuID, priceagent.ID),
})
colCounter++
if colCounter%numColumns == 0 {
keyboard = append(keyboard, row)
row = []gotgbot.InlineKeyboardButton{}
}
}
if len(row) > 0 {
keyboard = append(keyboard, row)
}
if len(priceagents) == 0 {
keyboard = [][]gotgbot.InlineKeyboardButton{
{
{Text: "🆕 Neuer Preisagent", CallbackData: "m01_00"},
{Text: "↩️ Zurück", CallbackData: "m01_01"},
},
}
} else {
// Add back button at the bottom row
keyboard = append(keyboard, []gotgbot.InlineKeyboardButton{{Text: "↩️ Zurück", CallbackData: "m01_01"}})
}
return gotgbot.InlineKeyboardMarkup{InlineKeyboard: keyboard}
}
func isAllowedLocation(location string) (allowed bool) {
allowedLocations := []string{"de", "at", "uk", "pl"}
for _, allowedLocation := range allowedLocations {
if location == allowedLocation {
return true
}
}
return false
}