-
Notifications
You must be signed in to change notification settings - Fork 525
Expand file tree
/
Copy pathrich_block.go
More file actions
165 lines (132 loc) · 5.55 KB
/
Copy pathrich_block.go
File metadata and controls
165 lines (132 loc) · 5.55 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package telebot
import "encoding/json"
// RichBlock type discriminators, as carried in the "type" field of a received
// rich block (Bot API 10.1).
const (
RichBlockParagraph = "paragraph"
RichBlockHeading = "heading"
RichBlockPre = "pre"
RichBlockFooter = "footer"
RichBlockDivider = "divider"
RichBlockMath = "mathematical_expression"
RichBlockAnchor = "anchor"
RichBlockList = "list"
RichBlockBlockquote = "blockquote"
RichBlockPullquote = "pullquote"
RichBlockCollage = "collage"
RichBlockSlideshow = "slideshow"
RichBlockTable = "table"
RichBlockDetails = "details"
RichBlockMap = "map"
RichBlockAnimationType = "animation"
RichBlockAudioType = "audio"
RichBlockPhotoType = "photo"
RichBlockVideoType = "video"
RichBlockVoiceNote = "voice_note"
RichBlockThinking = "thinking"
)
// RichBlock is a single block of a received RichMessage (Bot API 10.1). It is a
// polymorphic type discriminated by Type; only the fields relevant to a given
// Type are populated. RichBlock is a received-only type.
type RichBlock struct {
// Type is the block discriminator (see the RichBlock* constants).
Type string `json:"type"`
// Text is the block's formatted text (paragraph, heading, pre, footer,
// pullquote, thinking).
Text *RichText `json:"text,omitempty"`
// Size is the heading level, 1 (largest) to 6 (smallest).
Size int `json:"size,omitempty"`
// Language is the optional source language of a "pre" block.
Language string `json:"language,omitempty"`
// Expression is the LaTeX of a "mathematical_expression" block.
Expression string `json:"expression,omitempty"`
// Name is the identifier of an "anchor" block.
Name string `json:"name,omitempty"`
// Items are the entries of a "list" block.
Items []RichBlockListItem `json:"items,omitempty"`
// Blocks are the nested blocks of a blockquote, collage, slideshow or
// details block.
Blocks []RichBlock `json:"blocks,omitempty"`
// Summary is the disclosure summary of a "details" block.
Summary *RichText `json:"summary,omitempty"`
// IsOpen reports whether a "details" block is expanded by default.
IsOpen bool `json:"is_open,omitempty"`
// Credit is the attribution of a blockquote or pullquote block.
Credit *RichText `json:"credit,omitempty"`
// Cells are the rows-by-columns of a "table" block.
Cells [][]RichBlockTableCell `json:"cells,omitempty"`
// IsBordered and IsStriped style a "table" block.
IsBordered bool `json:"is_bordered,omitempty"`
IsStriped bool `json:"is_striped,omitempty"`
// Location, Zoom, Width and Height describe a "map" block.
Location *Location `json:"location,omitempty"`
Zoom int `json:"zoom,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
// Media of the corresponding block type.
Animation *Animation `json:"animation,omitempty"`
Audio *Audio `json:"audio,omitempty"`
Photo []Photo `json:"photo,omitempty"`
Video *Video `json:"video,omitempty"`
VoiceNote *Voice `json:"voice_note,omitempty"`
// HasSpoiler marks an animation, photo or video block as a spoiler.
HasSpoiler bool `json:"has_spoiler,omitempty"`
// Caption is the caption of a collage, slideshow, map, animation, audio,
// photo, video or voice-note block.
Caption *RichBlockCaption `json:"-"`
// TableCaption is the caption of a "table" block (a bare RichText rather
// than a RichBlockCaption).
TableCaption *RichText `json:"-"`
}
// UnmarshalJSON decodes a RichBlock, resolving the polymorphic "caption" field:
// a table carries a bare RichText caption, every other block a RichBlockCaption.
func (rb *RichBlock) UnmarshalJSON(data []byte) error {
type alias RichBlock
aux := struct {
*alias
Caption json.RawMessage `json:"caption,omitempty"`
}{alias: (*alias)(rb)}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
if len(aux.Caption) == 0 || string(aux.Caption) == "null" {
return nil
}
if rb.Type == RichBlockTable {
rb.TableCaption = new(RichText)
return json.Unmarshal(aux.Caption, rb.TableCaption)
}
rb.Caption = new(RichBlockCaption)
return json.Unmarshal(aux.Caption, rb.Caption)
}
// RichBlockCaption is the caption of a media or container rich block.
type RichBlockCaption struct {
// Text of the caption.
Text RichText `json:"text"`
// (Optional) Attribution shown alongside the caption.
Credit *RichText `json:"credit,omitempty"`
}
// RichBlockTableCell is a single cell of a "table" rich block. An omitted Text
// denotes an invisible cell spanned into by a neighbour.
type RichBlockTableCell struct {
Text *RichText `json:"text,omitempty"`
IsHeader bool `json:"is_header,omitempty"`
Colspan int `json:"colspan,omitempty"`
Rowspan int `json:"rowspan,omitempty"`
Align string `json:"align,omitempty"` // "left", "center", "right"
VAlign string `json:"valign,omitempty"` // "top", "middle", "bottom"
}
// RichBlockListItem is a single entry of a "list" rich block.
type RichBlockListItem struct {
// Label is the rendered bullet or number of the item.
Label string `json:"label,omitempty"`
// Blocks is the content of the item.
Blocks []RichBlock `json:"blocks,omitempty"`
// HasCheckbox and IsChecked render a task-list item.
HasCheckbox bool `json:"has_checkbox,omitempty"`
IsChecked bool `json:"is_checked,omitempty"`
// Value is the ordinal of an ordered-list item.
Value int `json:"value,omitempty"`
// Type is the label style of an ordered item ("a", "A", "i", "I", "1").
Type string `json:"type,omitempty"`
}