|
| 1 | +package telebot |
| 2 | + |
| 3 | +import "encoding/json" |
| 4 | + |
| 5 | +// RichBlock type discriminators, as carried in the "type" field of a received |
| 6 | +// rich block (Bot API 10.1). |
| 7 | +const ( |
| 8 | + RichBlockParagraph = "paragraph" |
| 9 | + RichBlockHeading = "heading" |
| 10 | + RichBlockPre = "pre" |
| 11 | + RichBlockFooter = "footer" |
| 12 | + RichBlockDivider = "divider" |
| 13 | + RichBlockMath = "mathematical_expression" |
| 14 | + RichBlockAnchor = "anchor" |
| 15 | + RichBlockList = "list" |
| 16 | + RichBlockBlockquote = "blockquote" |
| 17 | + RichBlockPullquote = "pullquote" |
| 18 | + RichBlockCollage = "collage" |
| 19 | + RichBlockSlideshow = "slideshow" |
| 20 | + RichBlockTable = "table" |
| 21 | + RichBlockDetails = "details" |
| 22 | + RichBlockMap = "map" |
| 23 | + RichBlockAnimationType = "animation" |
| 24 | + RichBlockAudioType = "audio" |
| 25 | + RichBlockPhotoType = "photo" |
| 26 | + RichBlockVideoType = "video" |
| 27 | + RichBlockVoiceNote = "voice_note" |
| 28 | + RichBlockThinking = "thinking" |
| 29 | +) |
| 30 | + |
| 31 | +// RichBlock is a single block of a received RichMessage (Bot API 10.1). It is a |
| 32 | +// polymorphic type discriminated by Type; only the fields relevant to a given |
| 33 | +// Type are populated. RichBlock is a received-only type. |
| 34 | +type RichBlock struct { |
| 35 | + // Type is the block discriminator (see the RichBlock* constants). |
| 36 | + Type string `json:"type"` |
| 37 | + |
| 38 | + // Text is the block's formatted text (paragraph, heading, pre, footer, |
| 39 | + // pullquote, thinking). |
| 40 | + Text *RichText `json:"text,omitempty"` |
| 41 | + |
| 42 | + // Size is the heading level, 1 (largest) to 6 (smallest). |
| 43 | + Size int `json:"size,omitempty"` |
| 44 | + |
| 45 | + // Language is the optional source language of a "pre" block. |
| 46 | + Language string `json:"language,omitempty"` |
| 47 | + |
| 48 | + // Expression is the LaTeX of a "mathematical_expression" block. |
| 49 | + Expression string `json:"expression,omitempty"` |
| 50 | + |
| 51 | + // Name is the identifier of an "anchor" block. |
| 52 | + Name string `json:"name,omitempty"` |
| 53 | + |
| 54 | + // Items are the entries of a "list" block. |
| 55 | + Items []RichBlockListItem `json:"items,omitempty"` |
| 56 | + |
| 57 | + // Blocks are the nested blocks of a blockquote, collage, slideshow or |
| 58 | + // details block. |
| 59 | + Blocks []RichBlock `json:"blocks,omitempty"` |
| 60 | + |
| 61 | + // Summary is the disclosure summary of a "details" block. |
| 62 | + Summary *RichText `json:"summary,omitempty"` |
| 63 | + |
| 64 | + // IsOpen reports whether a "details" block is expanded by default. |
| 65 | + IsOpen bool `json:"is_open,omitempty"` |
| 66 | + |
| 67 | + // Credit is the attribution of a blockquote or pullquote block. |
| 68 | + Credit *RichText `json:"credit,omitempty"` |
| 69 | + |
| 70 | + // Cells are the rows-by-columns of a "table" block. |
| 71 | + Cells [][]RichBlockTableCell `json:"cells,omitempty"` |
| 72 | + |
| 73 | + // IsBordered and IsStriped style a "table" block. |
| 74 | + IsBordered bool `json:"is_bordered,omitempty"` |
| 75 | + IsStriped bool `json:"is_striped,omitempty"` |
| 76 | + |
| 77 | + // Location, Zoom, Width and Height describe a "map" block. |
| 78 | + Location *Location `json:"location,omitempty"` |
| 79 | + Zoom int `json:"zoom,omitempty"` |
| 80 | + Width int `json:"width,omitempty"` |
| 81 | + Height int `json:"height,omitempty"` |
| 82 | + |
| 83 | + // Media of the corresponding block type. |
| 84 | + Animation *Animation `json:"animation,omitempty"` |
| 85 | + Audio *Audio `json:"audio,omitempty"` |
| 86 | + Photo []Photo `json:"photo,omitempty"` |
| 87 | + Video *Video `json:"video,omitempty"` |
| 88 | + VoiceNote *Voice `json:"voice_note,omitempty"` |
| 89 | + |
| 90 | + // HasSpoiler marks an animation, photo or video block as a spoiler. |
| 91 | + HasSpoiler bool `json:"has_spoiler,omitempty"` |
| 92 | + |
| 93 | + // Caption is the caption of a collage, slideshow, map, animation, audio, |
| 94 | + // photo, video or voice-note block. |
| 95 | + Caption *RichBlockCaption `json:"-"` |
| 96 | + |
| 97 | + // TableCaption is the caption of a "table" block (a bare RichText rather |
| 98 | + // than a RichBlockCaption). |
| 99 | + TableCaption *RichText `json:"-"` |
| 100 | +} |
| 101 | + |
| 102 | +// UnmarshalJSON decodes a RichBlock, resolving the polymorphic "caption" field: |
| 103 | +// a table carries a bare RichText caption, every other block a RichBlockCaption. |
| 104 | +func (rb *RichBlock) UnmarshalJSON(data []byte) error { |
| 105 | + type alias RichBlock |
| 106 | + aux := struct { |
| 107 | + *alias |
| 108 | + Caption json.RawMessage `json:"caption,omitempty"` |
| 109 | + }{alias: (*alias)(rb)} |
| 110 | + |
| 111 | + if err := json.Unmarshal(data, &aux); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + if len(aux.Caption) == 0 || string(aux.Caption) == "null" { |
| 116 | + return nil |
| 117 | + } |
| 118 | + |
| 119 | + if rb.Type == RichBlockTable { |
| 120 | + rb.TableCaption = new(RichText) |
| 121 | + return json.Unmarshal(aux.Caption, rb.TableCaption) |
| 122 | + } |
| 123 | + |
| 124 | + rb.Caption = new(RichBlockCaption) |
| 125 | + return json.Unmarshal(aux.Caption, rb.Caption) |
| 126 | +} |
| 127 | + |
| 128 | +// RichBlockCaption is the caption of a media or container rich block. |
| 129 | +type RichBlockCaption struct { |
| 130 | + // Text of the caption. |
| 131 | + Text RichText `json:"text"` |
| 132 | + |
| 133 | + // (Optional) Attribution shown alongside the caption. |
| 134 | + Credit *RichText `json:"credit,omitempty"` |
| 135 | +} |
| 136 | + |
| 137 | +// RichBlockTableCell is a single cell of a "table" rich block. An omitted Text |
| 138 | +// denotes an invisible cell spanned into by a neighbour. |
| 139 | +type RichBlockTableCell struct { |
| 140 | + Text *RichText `json:"text,omitempty"` |
| 141 | + IsHeader bool `json:"is_header,omitempty"` |
| 142 | + Colspan int `json:"colspan,omitempty"` |
| 143 | + Rowspan int `json:"rowspan,omitempty"` |
| 144 | + Align string `json:"align,omitempty"` // "left", "center", "right" |
| 145 | + VAlign string `json:"valign,omitempty"` // "top", "middle", "bottom" |
| 146 | +} |
| 147 | + |
| 148 | +// RichBlockListItem is a single entry of a "list" rich block. |
| 149 | +type RichBlockListItem struct { |
| 150 | + // Label is the rendered bullet or number of the item. |
| 151 | + Label string `json:"label,omitempty"` |
| 152 | + |
| 153 | + // Blocks is the content of the item. |
| 154 | + Blocks []RichBlock `json:"blocks,omitempty"` |
| 155 | + |
| 156 | + // HasCheckbox and IsChecked render a task-list item. |
| 157 | + HasCheckbox bool `json:"has_checkbox,omitempty"` |
| 158 | + IsChecked bool `json:"is_checked,omitempty"` |
| 159 | + |
| 160 | + // Value is the ordinal of an ordered-list item. |
| 161 | + Value int `json:"value,omitempty"` |
| 162 | + |
| 163 | + // Type is the label style of an ordered item ("a", "A", "i", "I", "1"). |
| 164 | + Type string `json:"type,omitempty"` |
| 165 | +} |
0 commit comments