-
Notifications
You must be signed in to change notification settings - Fork 2
/
object.go
72 lines (60 loc) · 1.24 KB
/
object.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
package chatgo
import "encoding/json"
type Object interface {
MarshalJSON() ([]byte, error)
}
type Text string
func (t Text) MarshalJSON() ([]byte, error) {
return []byte(t), nil
}
type Keyboard struct {
Buttons []string
}
func (k Keyboard) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string `json:"type"`
Buttons []string `json:"buttons,omitempty"`
}{Type: "buttons", Buttons: k.Buttons})
}
type Message struct {
Text string
Photo Photo
}
func (m Message) MarshalJSON() ([]byte, error) {
type tempMessage struct {
Text string `json:"text,omitempty"`
Photo *Photo `json:"photo,omitempty"`
}
var photo *Photo
if m.Photo != (Photo{}) {
photo = &m.Photo
}
return json.Marshal(&struct {
Message tempMessage `json:"message,omitempty"`
}{
Message: tempMessage{
Text: m.Text,
Photo: photo,
},
})
}
type Photo struct {
Url string
Width int
Height int
}
func (p Photo) MarshalJSON() ([]byte, error) {
width := p.Width
height := p.Height
if width == 0 {
width = 720
}
if height == 0 {
height = 630
}
return json.Marshal(&struct {
Url string `json:"url,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}{Url: p.Url, Width: width})
}