| 1 | package telegram |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "html" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // SendMessagePayload is the notification payload for the telegram notification service. |
| 11 | type SendMessagePayload struct { |
| 12 | Text string `json:"text"` |
| 13 | ID string `json:"chat_id"` |
| 14 | MessageThreadID *int `json:"message_thread_id,omitempty"` |
| 15 | ParseMode string `json:"parse_mode,omitempty"` |
| 16 | DisablePreview bool `json:"disable_web_page_preview"` |
| 17 | DisableNotification bool `json:"disable_notification"` |
| 18 | ReplyMarkup *replyMarkup `json:"reply_markup,omitempty"` |
| 19 | Entities []entity `json:"entities,omitempty"` |
| 20 | ReplyTo int64 `json:"reply_to_message_id"` |
| 21 | MessageID int64 `json:"message_id,omitempty"` |
| 22 | } |
| 23 | |
| 24 | // Message represents one chat message. |
| 25 | type Message struct { |
| 26 | MessageID int64 `json:"message_id"` |
| 27 | Text string `json:"text"` |
| 28 | From *User `json:"from"` |
| 29 | Chat *Chat `json:"chat"` |
| 30 | } |
| 31 | |
| 32 | type messageResponse struct { |
| 33 | OK bool `json:"ok"` |
| 34 | Result *Message `json:"result"` |
| 35 | } |
| 36 | |
| 37 | type responseError struct { |
| 38 | OK bool `json:"ok"` |
| 39 | ErrorCode int `json:"error_code"` |
| 40 | Description string `json:"description"` |
| 41 | } |
| 42 | |
| 43 | type userResponse struct { |
| 44 | OK bool `json:"ok"` |
| 45 | Result User `json:"result"` |
| 46 | } |
| 47 | |
| 48 | // User contains information about a telegram user or bot. |
| 49 | type User struct { |
| 50 | // Unique identifier for this User or bot |
| 51 | ID int64 `json:"id"` |
| 52 | // True, if this User is a bot |
| 53 | IsBot bool `json:"is_bot"` |
| 54 | // User's or bot's first name |
| 55 | FirstName string `json:"first_name"` |
| 56 | // Optional. User's or bot's last name |
| 57 | LastName string `json:"last_name"` |
| 58 | // Optional. User's or bot's username |
| 59 | Username string `json:"username"` |
| 60 | // Optional. IETF language tag of the User's language |
| 61 | LanguageCode string `json:"language_code"` |
| 62 | // Optional. True, if the bot can be invited to groups. Returned only in getMe. |
| 63 | CanJoinGroups bool `json:"can_join_groups"` |
| 64 | // Optional. True, if privacy mode is disabled for the bot. Returned only in getMe. |
| 65 | CanReadAllGroupMessages bool `json:"can_read_all_group_messages"` |
| 66 | // Optional. True, if the bot supports inline queries. Returned only in getMe. |
| 67 | SupportsInlineQueries bool `json:"supports_inline_queries"` |
| 68 | } |
| 69 | |
| 70 | type updatesRequest struct { |
| 71 | Offset int `json:"offset"` |
| 72 | Limit int `json:"limit"` |
| 73 | Timeout int `json:"timeout"` |
| 74 | AllowedUpdates []string `json:"allowed_updates"` |
| 75 | } |
| 76 | |
| 77 | type updatesResponse struct { |
| 78 | OK bool `json:"ok"` |
| 79 | Result []Update `json:"result"` |
| 80 | } |
| 81 | |
| 82 | type inlineQuery struct { |
| 83 | // Unique identifier for this query |
| 84 | ID string `json:"id"` |
| 85 | // Sender |
| 86 | From User `json:"from"` |
| 87 | // Text of the query (up to 256 characters) |
| 88 | Query string `json:"query"` |
| 89 | // Offset of the results to be returned, can be controlled by the bot |
| 90 | Offset string `json:"offset"` |
| 91 | } |
| 92 | |
| 93 | type chosenInlineResult struct{} |
| 94 | |
| 95 | // Update contains state changes since the previous Update. |
| 96 | type Update struct { |
| 97 | // The Update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you're using Webhooks, since it allows |
| 98 | // you to ignore repeated updates or to restore the correct Update sequence, should they get out of order. If there are no new updates for at least a week, then identifier of the next Update will |
| 99 | // be chosen randomly instead of sequentially. |
| 100 | UpdateID int `json:"update_id"` |
| 101 | // Optional. New incoming Message of any kind — text, photo, sticker, etc. |
| 102 | Message *Message `json:"Message"` |
| 103 | // Optional. New version of a Message that is known to the bot and was edited |
| 104 | EditedMessage *Message `json:"edited_message"` |
| 105 | // Optional. New incoming channel post of any kind — text, photo, sticker, etc. |
| 106 | ChannelPost *Message `json:"channel_post"` |
| 107 | // Optional. New version of a channel post that is known to the bot and was edited |
| 108 | EditedChannelPost *Message `json:"edited_channel_post"` |
| 109 | // Optional. New incoming inline query |
| 110 | InlineQuery *inlineQuery `json:"inline_query"` |
| 111 | // // Optional. The result of an inline query that was chosen by a User and sent to their chat partner. Please see our documentation on the feedback collecting for details on how to enable these |
| 112 | // updates for your bot. |
| 113 | ChosenInlineResult *chosenInlineResult `json:"chosen_inline_result"` |
| 114 | //// Optional. New incoming callback query |
| 115 | CallbackQuery *callbackQuery `json:"callback_query"` |
| 116 | |
| 117 | // API fields that are not used by the client has been commented out |
| 118 | |
| 119 | //// Optional. New incoming shipping query. Only for invoices with flexible price |
| 120 | // ShippingQuery `json:"shipping_query"` |
| 121 | //// Optional. New incoming pre-checkout query. Contains full information about checkout |
| 122 | // PreCheckoutQuery `json:"pre_checkout_query"` |
| 123 | //// Optional. New poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot |
| 124 | // Poll `json:"poll"` |
| 125 | //// Optional. A User changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself. |
| 126 | // Poll_answer PollAnswer `json:"poll_answer"` |
| 127 | |
| 128 | ChatMemberUpdate *ChatMemberUpdate `json:"my_chat_member"` |
| 129 | } |
| 130 | |
| 131 | // Chat represents a telegram conversation. |
| 132 | type Chat struct { |
| 133 | ID int64 `json:"id"` |
| 134 | Type string `json:"type"` |
| 135 | Title string `json:"title"` |
| 136 | Username string `json:"username"` |
| 137 | } |
| 138 | |
| 139 | type inlineKey struct { |
| 140 | Text string `json:"text"` |
| 141 | URL string `json:"url"` |
| 142 | LoginURL string `json:"login_url"` |
| 143 | CallbackData string `json:"callback_data"` |
| 144 | SwitchInlineQuery string `json:"switch_inline_query"` |
| 145 | SwitchInlineQueryCurrent string `json:"switch_inline_query_current_chat"` |
| 146 | } |
| 147 | |
| 148 | type replyMarkup struct { |
| 149 | InlineKeyboard [][]inlineKey `json:"inline_keyboard,omitempty"` |
| 150 | } |
| 151 | |
| 152 | type entity struct { |
| 153 | Type string `json:"type"` |
| 154 | Offset int `json:"offset"` |
| 155 | Length int `json:"length"` |
| 156 | } |
| 157 | |
| 158 | type callbackQuery struct { |
| 159 | ID string `json:"id"` |
| 160 | From *User `json:"from"` |
| 161 | Message *Message `json:"Message"` |
| 162 | Data string `json:"data"` |
| 163 | } |
| 164 | |
| 165 | // ChatMemberUpdate represents a member update in a telegram chat. |
| 166 | type ChatMemberUpdate struct { |
| 167 | // Chat the user belongs to |
| 168 | Chat *Chat `json:"chat"` |
| 169 | // Performer of the action, which resulted in the change |
| 170 | From *User `json:"from"` |
| 171 | // Date the change was done in Unix time |
| 172 | Date int `json:"date"` |
| 173 | // Previous information about the chat member |
| 174 | OldChatMember *ChatMember `json:"old_chat_member"` |
| 175 | // New information about the chat member |
| 176 | NewChatMember *ChatMember `json:"new_chat_member"` |
| 177 | // Optional. Chat invite link, which was used by the user to join the chat; for joining by invite link events only. |
| 178 | // invite_link ChatInviteLink |
| 179 | } |
| 180 | |
| 181 | // ChatMember represents the membership state for a user in a telegram chat. |
| 182 | type ChatMember struct { |
| 183 | // The member's status in the chat |
| 184 | Status string `json:"status"` |
| 185 | // Information about the user |
| 186 | User *User `json:"user"` |
| 187 | } |
| 188 | |
| 189 | func (e *responseError) Error() string { |
| 190 | return e.Description |
| 191 | } |
| 192 | |
| 193 | // Name returns the name of the channel based on its type. |
| 194 | func (c *Chat) Name() string { |
| 195 | if c.Type == "private" || c.Type == "channel" && c.Username != "" { |
| 196 | return "@" + c.Username |
| 197 | } |
| 198 | |
| 199 | return c.Title |
| 200 | } |
| 201 | |
| 202 | func createSendMessagePayload(message, channel string, config *Config) SendMessagePayload { |
| 203 | var threadID *int |
| 204 | |
| 205 | chatID, thread, ok := strings.Cut(channel, ":") |
| 206 | if ok { |
| 207 | if parsed, err := strconv.Atoi(thread); err == nil { |
| 208 | threadID = &parsed |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | payload := SendMessagePayload{ |
| 213 | Text: message, |
| 214 | ID: chatID, |
| 215 | MessageThreadID: threadID, |
| 216 | DisableNotification: !config.Notification, |
| 217 | DisablePreview: !config.Preview, |
| 218 | } |
| 219 | |
| 220 | parseMode := config.ParseMode |
| 221 | if config.ParseMode == ParseModes.None && config.Title != "" { |
| 222 | parseMode = ParseModes.HTML |
| 223 | // no parse mode has been provided, treat message as unescaped HTML |
| 224 | message = html.EscapeString(message) |
| 225 | } |
| 226 | |
| 227 | if parseMode != ParseModes.None { |
| 228 | payload.ParseMode = parseMode.String() |
| 229 | } |
| 230 | |
| 231 | // only HTML parse mode is supported for titles |
| 232 | if parseMode == ParseModes.HTML { |
| 233 | payload.Text = fmt.Sprintf("<b>%v</b>\n%v", html.EscapeString(config.Title), message) |
| 234 | } |
| 235 | |
| 236 | return payload |
| 237 | } |