|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strconv" |
| 6 | +) |
| 7 | + |
| 8 | +// ChatID identifies a target chat. The Bot API accepts either a numeric chat id |
| 9 | +// or an @username; this sealed union represents exactly those two cases, so an |
| 10 | +// illegal "both/neither" state is unrepresentable. |
| 11 | +// |
| 12 | +// Construct with ID or Username. |
| 13 | +type ChatID interface { |
| 14 | + isChatID() |
| 15 | + // json.Marshaler so a ChatID serializes to the bare int or string the wire |
| 16 | + // format expects. |
| 17 | + json.Marshaler |
| 18 | +} |
| 19 | + |
| 20 | +// ChatIDInt is a numeric chat identifier. |
| 21 | +type ChatIDInt int64 |
| 22 | + |
| 23 | +// ChatIDUsername is an @username target (with or without the leading @). |
| 24 | +type ChatIDUsername string |
| 25 | + |
| 26 | +func (ChatIDInt) isChatID() {} |
| 27 | +func (ChatIDUsername) isChatID() {} |
| 28 | + |
| 29 | +// MarshalJSON encodes the id as a JSON number. |
| 30 | +func (c ChatIDInt) MarshalJSON() ([]byte, error) { |
| 31 | + return []byte(strconv.FormatInt(int64(c), 10)), nil |
| 32 | +} |
| 33 | + |
| 34 | +// MarshalJSON encodes the username as a JSON string. |
| 35 | +func (c ChatIDUsername) MarshalJSON() ([]byte, error) { |
| 36 | + return json.Marshal(string(c)) |
| 37 | +} |
| 38 | + |
| 39 | +// ID targets a chat by numeric identifier. |
| 40 | +func ID(id int64) ChatID { return ChatIDInt(id) } |
| 41 | + |
| 42 | +// Username targets a chat by @username. |
| 43 | +func Username(username string) ChatID { return ChatIDUsername(username) } |
| 44 | + |
| 45 | +// InputFile is a sealed union describing a file to send: an existing Telegram |
| 46 | +// file_id, an HTTP URL Telegram fetches, or a local upload. |
| 47 | +// |
| 48 | +// Construct with FileID, FileURL, FileFromPath, FileFromBytes or FileFromReader. |
| 49 | +type InputFile interface { |
| 50 | + isInputFile() |
| 51 | +} |
| 52 | + |
| 53 | +// InputFileID references a file already on Telegram's servers by file_id. |
| 54 | +type InputFileID string |
| 55 | + |
| 56 | +// InputFileURL references a file by HTTP URL for Telegram to fetch. |
| 57 | +type InputFileURL string |
| 58 | + |
| 59 | +// InputFileUpload is a local file to be uploaded. Exactly one source is set; |
| 60 | +// the send path (Phase 3) chooses the uploader accordingly. |
| 61 | +type InputFileUpload struct { |
| 62 | + // Name is the filename reported to Telegram. |
| 63 | + Name string |
| 64 | + // Path, when non-empty, is read from disk. |
| 65 | + Path string |
| 66 | + // Bytes, when non-nil, is the in-memory content. |
| 67 | + Bytes []byte |
| 68 | + // Reader, when non-nil, streams the content. |
| 69 | + Reader interface{ Read([]byte) (int, error) } |
| 70 | +} |
| 71 | + |
| 72 | +func (InputFileID) isInputFile() {} |
| 73 | +func (InputFileURL) isInputFile() {} |
| 74 | +func (*InputFileUpload) isInputFile() {} |
| 75 | + |
| 76 | +// FileID references an existing Telegram file by its file_id. |
| 77 | +func FileID(id string) InputFile { return InputFileID(id) } |
| 78 | + |
| 79 | +// FileURL references a remote file by URL for Telegram to fetch. |
| 80 | +func FileURL(url string) InputFile { return InputFileURL(url) } |
| 81 | + |
| 82 | +// FileFromPath uploads a local file from disk. |
| 83 | +func FileFromPath(path string) InputFile { return &InputFileUpload{Path: path} } |
| 84 | + |
| 85 | +// FileFromBytes uploads in-memory content under the given filename. |
| 86 | +func FileFromBytes(name string, data []byte) InputFile { |
| 87 | + return &InputFileUpload{Name: name, Bytes: data} |
| 88 | +} |
| 89 | + |
| 90 | +// FileFromReader uploads streamed content under the given filename. |
| 91 | +func FileFromReader(name string, r interface{ Read([]byte) (int, error) }) InputFile { |
| 92 | + return &InputFileUpload{Name: name, Reader: r} |
| 93 | +} |
| 94 | + |
| 95 | +// ReactionType is a sealed union of reaction kinds. |
| 96 | +// |
| 97 | +// Concrete variants: ReactionTypeEmoji, ReactionTypeCustomEmoji, ReactionTypePaid. |
| 98 | +type ReactionType interface { |
| 99 | + isReactionType() |
| 100 | +} |
| 101 | + |
| 102 | +// ReactionTypeEmoji is a reaction with a standard emoji. |
| 103 | +type ReactionTypeEmoji struct { |
| 104 | + Type ReactionTypeKind `json:"type"` |
| 105 | + Emoji string `json:"emoji"` |
| 106 | +} |
| 107 | + |
| 108 | +// ReactionTypeCustomEmoji is a reaction with a custom emoji. |
| 109 | +type ReactionTypeCustomEmoji struct { |
| 110 | + Type ReactionTypeKind `json:"type"` |
| 111 | + CustomEmojiID string `json:"custom_emoji_id"` |
| 112 | +} |
| 113 | + |
| 114 | +// ReactionTypePaid is a paid (star) reaction. |
| 115 | +type ReactionTypePaid struct { |
| 116 | + Type ReactionTypeKind `json:"type"` |
| 117 | +} |
| 118 | + |
| 119 | +func (ReactionTypeEmoji) isReactionType() {} |
| 120 | +func (ReactionTypeCustomEmoji) isReactionType() {} |
| 121 | +func (ReactionTypePaid) isReactionType() {} |
| 122 | + |
| 123 | +// Emoji builds a standard-emoji reaction. |
| 124 | +func Emoji(emoji string) ReactionType { |
| 125 | + return ReactionTypeEmoji{Type: ReactionEmoji, Emoji: emoji} |
| 126 | +} |
| 127 | + |
| 128 | +// CustomEmoji builds a custom-emoji reaction. |
| 129 | +func CustomEmoji(id string) ReactionType { |
| 130 | + return ReactionTypeCustomEmoji{Type: ReactionCustomEmoji, CustomEmojiID: id} |
| 131 | +} |
| 132 | + |
| 133 | +// MenuButton is a sealed union describing the bot's menu button. |
| 134 | +// |
| 135 | +// Concrete variants: MenuButtonCommands, MenuButtonWebApp, MenuButtonDefault. |
| 136 | +type MenuButton interface { |
| 137 | + isMenuButton() |
| 138 | +} |
| 139 | + |
| 140 | +// MenuButtonCommands opens the bot's command list. |
| 141 | +type MenuButtonCommands struct { |
| 142 | + Type MenuButtonType `json:"type"` |
| 143 | +} |
| 144 | + |
| 145 | +// MenuButtonWebApp opens a Web App. |
| 146 | +type MenuButtonWebApp struct { |
| 147 | + Type MenuButtonType `json:"type"` |
| 148 | + Text string `json:"text"` |
| 149 | + WebApp WebAppInfo `json:"web_app"` |
| 150 | +} |
| 151 | + |
| 152 | +// MenuButtonDefault is the default menu button. |
| 153 | +type MenuButtonDefault struct { |
| 154 | + Type MenuButtonType `json:"type"` |
| 155 | +} |
| 156 | + |
| 157 | +func (MenuButtonCommands) isMenuButton() {} |
| 158 | +func (MenuButtonWebApp) isMenuButton() {} |
| 159 | +func (MenuButtonDefault) isMenuButton() {} |
0 commit comments