File: telegram_json.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.8.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,332 kB
  • sloc: sh: 61; makefile: 5
file content (234 lines) | stat: -rw-r--r-- 8,248 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package telegram

import (
	"fmt"
	"html"
	"strconv"
	"strings"
)

// SendMessagePayload is the notification payload for the telegram notification service.
type SendMessagePayload struct {
	Text                string       `json:"text"`
	ID                  string       `json:"chat_id"`
	MessageThreadID     *int         `json:"message_thread_id,omitempty"`
	ParseMode           string       `json:"parse_mode,omitempty"`
	DisablePreview      bool         `json:"disable_web_page_preview"`
	DisableNotification bool         `json:"disable_notification"`
	ReplyMarkup         *replyMarkup `json:"reply_markup,omitempty"`
	Entities            []entity     `json:"entities,omitempty"`
	ReplyTo             int64        `json:"reply_to_message_id"`
	MessageID           int64        `json:"message_id,omitempty"`
}

// Message represents one chat message.
type Message struct {
	MessageID int64  `json:"message_id"`
	Text      string `json:"text"`
	From      *User  `json:"from"`
	Chat      *Chat  `json:"chat"`
}

type messageResponse struct {
	OK     bool     `json:"ok"`
	Result *Message `json:"result"`
}

type responseError struct {
	OK          bool   `json:"ok"`
	ErrorCode   int    `json:"error_code"`
	Description string `json:"description"`
}

type userResponse struct {
	OK     bool `json:"ok"`
	Result User `json:"result"`
}

// User contains information about a telegram user or bot.
type User struct {
	//	Unique identifier for this User or bot
	ID int64 `json:"id"`
	// True, if this User is a bot
	IsBot bool `json:"is_bot"`
	// User's or bot's first name
	FirstName string `json:"first_name"`
	//	Optional. User's or bot's last name
	LastName string `json:"last_name"`
	// Optional. User's or bot's username
	Username string `json:"username"`
	// Optional. IETF language tag of the User's language
	LanguageCode string `json:"language_code"`
	// 	Optional. True, if the bot can be invited to groups. Returned only in getMe.
	CanJoinGroups bool `json:"can_join_groups"`
	// 	Optional. True, if privacy mode is disabled for the bot. Returned only in getMe.
	CanReadAllGroupMessages bool `json:"can_read_all_group_messages"`
	// 	Optional. True, if the bot supports inline queries. Returned only in getMe.
	SupportsInlineQueries bool `json:"supports_inline_queries"`
}

type updatesRequest struct {
	Offset         int      `json:"offset"`
	Limit          int      `json:"limit"`
	Timeout        int      `json:"timeout"`
	AllowedUpdates []string `json:"allowed_updates"`
}

type updatesResponse struct {
	OK     bool     `json:"ok"`
	Result []Update `json:"result"`
}

type inlineQuery struct {
	// Unique identifier for this query
	ID string `json:"id"`
	// Sender
	From User `json:"from"`
	// Text of the query (up to 256 characters)
	Query string `json:"query"`
	// Offset of the results to be returned, can be controlled by the bot
	Offset string `json:"offset"`
}

type chosenInlineResult struct{}

// Update contains state changes since the previous Update.
type Update struct {
	// 	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 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 be chosen randomly instead of sequentially.
	UpdateID int `json:"update_id"`
	// 	Optional. New incoming Message of any kind — text, photo, sticker, etc.
	Message *Message `json:"Message"`
	// 	Optional. New version of a Message that is known to the bot and was edited
	EditedMessage *Message `json:"edited_message"`
	// 	Optional. New incoming channel post of any kind — text, photo, sticker, etc.
	ChannelPost *Message `json:"channel_post"`
	// 	Optional. New version of a channel post that is known to the bot and was edited
	EditedChannelPost *Message `json:"edited_channel_post"`
	// 	Optional. New incoming inline query
	InlineQuery *inlineQuery `json:"inline_query"`
	//// 	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 updates for your bot.
	ChosenInlineResult *chosenInlineResult `json:"chosen_inline_result"`
	//// 	Optional. New incoming callback query
	CallbackQuery *callbackQuery `json:"callback_query"`

	// API fields that are not used by the client has been commented out

	//// 	Optional. New incoming shipping query. Only for invoices with flexible price
	// ShippingQuery	`json:"shipping_query"`
	//// 	Optional. New incoming pre-checkout query. Contains full information about checkout
	// PreCheckoutQuery	`json:"pre_checkout_query"`
	//// 	Optional. New poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot
	// Poll	`json:"poll"`
	//// 	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.
	// Poll_answer	PollAnswer `json:"poll_answer"`

	ChatMemberUpdate *ChatMemberUpdate `json:"my_chat_member"`
}

// Chat represents a telegram conversation.
type Chat struct {
	ID       int64  `json:"id"`
	Type     string `json:"type"`
	Title    string `json:"title"`
	Username string `json:"username"`
}

type inlineKey struct {
	Text                     string `json:"text"`
	URL                      string `json:"url"`
	LoginURL                 string `json:"login_url"`
	CallbackData             string `json:"callback_data"`
	SwitchInlineQuery        string `json:"switch_inline_query"`
	SwitchInlineQueryCurrent string `json:"switch_inline_query_current_chat"`
}

type replyMarkup struct {
	InlineKeyboard [][]inlineKey `json:"inline_keyboard,omitempty"`
}

type entity struct {
	Type   string `json:"type"`
	Offset int    `json:"offset"`
	Length int    `json:"length"`
}

type callbackQuery struct {
	ID      string   `json:"id"`
	From    *User    `json:"from"`
	Message *Message `json:"Message"`
	Data    string   `json:"data"`
}

// ChatMemberUpdate represents a member update in a telegram chat.
type ChatMemberUpdate struct {
	// Chat the user belongs to
	Chat *Chat `json:"chat"`
	// Performer of the action, which resulted in the change
	From *User `json:"from"`
	// Date the change was done in Unix time
	Date int `json:"date"`
	// Previous information about the chat member
	OldChatMember *ChatMember `json:"old_chat_member"`
	// New information about the chat member
	NewChatMember *ChatMember `json:"new_chat_member"`
	// Optional. Chat invite link, which was used by the user to join the chat; for joining by invite link events only.
	// invite_link ChatInviteLink
}

// ChatMember represents the membership state for a user in a telegram chat.
type ChatMember struct {
	//	The member's status in the chat
	Status string `json:"status"`
	// Information about the user
	User *User `json:"user"`
}

func (e *responseError) Error() string {
	return e.Description
}

// Name returns the name of the channel based on its type.
func (c *Chat) Name() string {
	if c.Type == "private" || c.Type == "channel" && c.Username != "" {
		return "@" + c.Username
	}

	return c.Title
}

func createSendMessagePayload(message string, channel string, config *Config) SendMessagePayload {
	var threadID *int

	chatID, thread, ok := strings.Cut(channel, ":")
	if ok {
		if parsed, err := strconv.Atoi(thread); err == nil {
			threadID = &parsed
		}
	}

	payload := SendMessagePayload{
		Text:                message,
		ID:                  chatID,
		MessageThreadID:     threadID,
		DisableNotification: !config.Notification,
		DisablePreview:      !config.Preview,
	}

	parseMode := config.ParseMode
	if config.ParseMode == ParseModes.None && config.Title != "" {
		parseMode = ParseModes.HTML
		// no parse mode has been provided, treat message as unescaped HTML
		message = html.EscapeString(message)
	}

	if parseMode != ParseModes.None {
		payload.ParseMode = parseMode.String()
	}

	// only HTML parse mode is supported for titles
	if parseMode == ParseModes.HTML {
		payload.Text = fmt.Sprintf("<b>%v</b>\n%v", html.EscapeString(config.Title), message)
	}

	return payload
}