-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.go
73 lines (58 loc) · 1.15 KB
/
keyboard.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
package telegram
import (
"encoding/json"
)
type KeyboardI interface {
Get() string
}
type KeyboardButton struct {
Text string `json:"text,omitempty"`
Url string `json:"url,omitempty"`
Data string `json:"callback_data,omitempty"`
}
type KeyboardRow struct {
buttons []*KeyboardButton
}
func (r *KeyboardRow) AddButton(text, data string) *KeyboardRow {
r.buttons = append(r.buttons,
&KeyboardButton{
Text: text,
Data: data,
},
)
return r
}
func (r *KeyboardRow) AddURL(text, url string) *KeyboardRow {
r.buttons = append(r.buttons,
&KeyboardButton{
Text: text,
Url: url,
},
)
return r
}
func (r *KeyboardRow) MarshalJSON() ([]byte, error) {
return json.Marshal(r.buttons)
}
type InlineKeyboard struct {
Rows []*KeyboardRow `json:"inline_keyboard"`
}
func (k *InlineKeyboard) NewRow() *KeyboardRow {
row := &KeyboardRow{
buttons: []*KeyboardButton{},
}
k.Rows = append(k.Rows, row)
return row
}
func (k *InlineKeyboard) Get() string {
bytes, err := json.Marshal(k)
if err != nil {
return "{}"
}
return string(bytes)
}
func NewInlineKeyboard() *InlineKeyboard {
return &InlineKeyboard{
Rows: []*KeyboardRow{},
}
}