forked from go-telegram/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_button.go
71 lines (57 loc) · 1.76 KB
/
menu_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
package models
import (
"bytes"
"encoding/json"
"fmt"
)
type MenuButtonType int
const (
MenuButtonTypeCommands MenuButtonType = iota
MenuButtonTypeWebApp
MenuButtonTypeDefault
)
type InputMenuButton interface {
menuButtonTag()
}
// MenuButton https://core.telegram.org/bots/api#menubutton
type MenuButton struct {
Type MenuButtonType
Commands *MenuButtonCommands
WebApp *MenuButtonWebApp
Default *MenuButtonDefault
}
func (c *MenuButton) UnmarshalJSON(data []byte) error {
if bytes.Contains(data, []byte(`"type":"commands"`)) {
c.Type = MenuButtonTypeCommands
c.Commands = &MenuButtonCommands{}
return json.Unmarshal(data, c.Commands)
}
if bytes.Contains(data, []byte(`"type":"web_app"`)) {
c.Type = MenuButtonTypeWebApp
c.WebApp = &MenuButtonWebApp{}
return json.Unmarshal(data, c.WebApp)
}
if bytes.Contains(data, []byte(`"type":"default"`)) {
c.Type = MenuButtonTypeDefault
c.Default = &MenuButtonDefault{}
return json.Unmarshal(data, c.Default)
}
return fmt.Errorf("unsupported MenuButton type")
}
// MenuButtonCommands https://core.telegram.org/bots/api#menubuttoncommands
type MenuButtonCommands struct {
Type string `json:"type" rules:"required,equals:commands"`
}
func (MenuButtonCommands) menuButtonTag() {}
// MenuButtonWebApp https://core.telegram.org/bots/api#menubuttonwebapp
type MenuButtonWebApp struct {
Type string `json:"type" rules:"required,equals:web_app"`
Text string `json:"text" rules:"required"`
WebApp WebAppInfo `json:"web_app" rules:"required"`
}
func (MenuButtonWebApp) menuButtonTag() {}
// MenuButtonDefault https://core.telegram.org/bots/api#menubuttondefault
type MenuButtonDefault struct {
Type string `json:"type" rules:"required,equals:default"`
}
func (MenuButtonDefault) menuButtonTag() {}