-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.go
122 lines (102 loc) · 2.64 KB
/
button.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
package tg
import (
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"fmt"
"crypto/rand"
"encoding/base64"
)
// The type wraps Telegram API's button to provide Action functionality.
type Button struct {
Text string
Data string
Url string
SendLocation bool
Action Action
}
type ButtonMap map[string]*Button
// Returns the only location button in the map.
func (btnMap ButtonMap) LocationButton() *Button {
for _, btn := range btnMap {
if btn.SendLocation {
return btn
}
}
return nil
}
// Represents the reply button row.
type ButtonRow []*Button
// Returns new button with the specified text and no action.
func NewButton(format string, v ...any) *Button {
return &Button{
Text: fmt.Sprintf(format, v...),
}
}
// Randomize buttons data to make the key unique.
func (btn *Button) Rand() *Button {
rData := make([]byte, 8)
rand.Read(rData)
data := make([]byte, base64.StdEncoding.EncodedLen(len(rData)))
base64.StdEncoding.Encode(data, rData)
btn.Data = string(data)
return btn
}
// Set the URL for the button. Only for inline buttons.
func (btn *Button) WithUrl(format string, v ...any) *Button {
btn.Url = fmt.Sprintf(format, v...)
return btn
}
// Set the action when pressing the button.
// By default is nil and does nothing.
func (btn *Button) WithAction(a Action) *Button {
btn.Action = a
return btn
}
func (btn *Button) WithData(dat string) *Button {
btn.Data = dat
return btn
}
// Sets whether the button must send owner's location.
func (btn *Button) WithSendLocation(ok bool) *Button {
btn.SendLocation = ok
return btn
}
func (btn *Button) ActionFunc(fn ActionFunc) *Button {
return btn.WithAction(fn)
}
func (btn *Button) Go(pth Path, args ...any) *Button {
return btn.WithAction(ScreenGo{
Path: pth,
Args: args,
})
}
func (btn *Button) ToTelegram() apix.KeyboardButton {
ret := apix.NewKeyboardButton(btn.Text)
if btn.SendLocation {
ret.RequestLocation = true
}
return ret
}
func (btn *Button) ToTelegramInline() apix.InlineKeyboardButton {
if btn.Data != "" {
return apix.NewInlineKeyboardButtonData(btn.Text, btn.Data)
}
if btn.Url != "" {
return apix.NewInlineKeyboardButtonURL(btn.Text, btn.Url)
}
// If no match then return the data one with data the same as the text.
return apix.NewInlineKeyboardButtonData(btn.Text, btn.Text)
}
// Return the key of the button to identify it by messages and callbacks.
func (btn *Button) Key() string {
if btn == nil {
return ""
}
if btn.Data != "" {
return btn.Data
}
// If no match then return the data one with data the same as the text.
return btn.Text
}
func NewButtonRow(btns ...*Button) ButtonRow {
return btns
}