-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
594 lines (527 loc) · 19.9 KB
/
types.go
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
package telegram
import (
"encoding/json"
"io"
"net/url"
"strconv"
)
// Getting updates
// https://core.telegram.org/bots/api#getting-updates
// https://core.telegram.org/bots/api#update
type Update struct {
UpdateID int `json:"update_id"`
Message *Message `json:"message"`
EditedMessage *Message `json:"edited_message"`
ChannelPost *Message `json:"channel_post"`
EditedChannelPost *Message `json:"edited_channel_post"`
// InlineQuery
// ChosenInlineResult
CallbackQuery *CallbackQuery `json:"callback_query"`
// ShippingQuery
// PreCheckoutQuery
}
// https://core.telegram.org/bots/api#webhookinfo
type WebhookInfo struct {
URL string `json:"url"`
HasCustomCertificate bool `json:"has_custom_certificate"`
PendingUpdateCount int `json:"pending_update_count"`
LastErrorDate int `json:"last_error_date"`
LastErrorMessage string `json:"last_error_message"`
MaxConnections int `json:"max_connections"`
AllowedUpdates []string `json:"allowed_updates"`
}
// Available types
// https://core.telegram.org/bots/api#available-types
// https://core.telegram.org/bots/api#user
type User struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName *string `json:"last_name"`
Username *string `json:"username"`
LanguageCode *string `json:"language_code"`
}
// https://core.telegram.org/bots/api#chat
type Chat struct {
ID int64 `json:"id"`
Type string `json:"type"`
Title *string `json:"title"`
Username *string `json:"username"`
FirstName *string `json:"first_name"`
LastName *string `json:"last_name"`
AllMembersAreAdministrators *bool `json:"all_members_are_administrators"`
ChatPhoto *ChatPhoto `json:"chat_photo"`
Description *string `json:"description"`
InviteLink *string `json:"invite_link"`
}
func (c *Chat) IsPrivate() bool { return c.Type == "private" }
func (c *Chat) IsGroup() bool { return c.Type == "group" }
func (c *Chat) IsSupergroup() bool { return c.Type == "supergroup" }
func (c *Chat) IsChannel() bool { return c.Type == "channel" }
// https://core.telegram.org/bots/api#message
type Message struct {
MessageID int `json:"message_id"`
From *User `json:"from"`
Date int `json:"date"`
Chat Chat `json:"chat"`
ForwardFrom *User `json:"forward_from"`
ForwardFromChat *Chat `json:"forward_from_chat"`
ForwardDate *int `json:"forward_date"`
ReplyToMessage *Message `json:"reply_to_message"`
EditDate *int `json:"edit_date"`
Text *string `json:"text"`
Entities []*MessageEntity `json:"entities"`
Audio *Audio `json:"audio"`
Document *Document `json:"document"`
// Game
Photo []*PhotoSize `json:"photo"`
Sticker *Sticker `json:"sticker"`
Video *Video `json:"video"`
Voice *Voice `json:"voice"`
VideoNote *VideoNote `json:"video_note"`
NewChatMembers []*User `json:"new_chat_members"`
Caption *string `json:"caption"`
Contact *Contact `json:"contact"`
Location *Location `json:"location"`
Venue *Venue `json:"venue"`
NewChatMember *User `json:"new_chat_member"`
LeftChatMember *User `json:"left_chat_member"`
NewChatTitle *string `json:"new_chat_title"`
NewChatPhoto []*PhotoSize `json:"new_chat_photo"`
DeleteChatPhoto *bool `json:"delete_chat_photo"`
GroupChatCreated *bool `json:"group_chat_created"`
SupergroupChatCreated *bool `json:"supergroup_chat_created"`
ChannelChatCreated *bool `json:"channel_chat_created"`
MigrateToChatID *int64 `json:"migrate_to_chat_id"`
MigrateFromChatID *int64 `json:"migrate_from_chat_id"`
PinnedMessage *Message `json:"pinned_message"`
// Invoice
// SuccessfulPayment
}
// https://core.telegram.org/bots/api#messageentity
type MessageEntity struct {
Type string `json:"type"`
Offset int `json:"offset"`
Length int `json:"length"`
URL *string `json:"url"`
User *User `json:"user"`
}
func (e *MessageEntity) IsMention() bool { return e.Type == "mention" }
func (e *MessageEntity) IsHashtag() bool { return e.Type == "hastag" }
func (e *MessageEntity) IsBotCommand() bool { return e.Type == "bot_command" }
func (e *MessageEntity) IsURL() bool { return e.Type == "url" }
func (e *MessageEntity) IsEmail() bool { return e.Type == "email" }
// https://core.telegram.org/bots/api#photosize
type PhotoSize struct {
FileID string `json:"file_id"`
Width int `json:"width"`
Height int `json:"height"`
FileSize *int `json:"file_size"`
}
// https://core.telegram.org/bots/api#audio
type Audio struct {
FileID string `json:"file_id"`
Duration int `json:"duration"`
Performer *string `json:"performer"`
Title *string `json:"title"`
MimeType *string `json:"mime_type"`
FileSize *int `json:"file_size"`
}
// https://core.telegram.org/bots/api#document
type Document struct {
FileID string `json:"file_id"`
Thumb *PhotoSize `json:"thumb"`
FileName *string `json:"file_name"`
MimeType *string `json:"mime_type"`
FileSize *int `json:"file_size"`
}
// https://core.telegram.org/bots/api#video
type Video struct {
FileID string `json:"file_id"`
Width int `json:"width"`
Height int `json:"height"`
Duration int `json:"duration"`
Thumb *PhotoSize `json:"thumb"`
MimeType *string `json:"mime_type"`
FileSize *int `json:"file_size"`
}
// https://core.telegram.org/bots/api#voice
type Voice struct {
FileID string `json:"file_id"`
Duration int `json:"duration"`
MimeType *string `json:"mime_type"`
FileSize *int `json:"file_size"`
}
// https://core.telegram.org/bots/api#videonote
type VideoNote struct {
FileID string `json:"file_id"`
Length int `json:"length"`
Duration int `json:"duration"`
Thumb *PhotoSize `json:"thumb"`
FileSize *int `json:"file_size"`
}
// https://core.telegram.org/bots/api#contact
type Contact struct {
PhoneNumber string
FirstName string
LastName *string
UserID *int
}
// https://core.telegram.org/bots/api#location
type Location struct {
Longitude float32 `json:"longitude"`
Latitude float32 `json:"latitude"`
}
// https://core.telegram.org/bots/api#venue
type Venue struct {
Location Location `json:"location"`
Title string `json:"title"`
Address string `json:"address"`
FoursquareID *string `json:"foursquare_id"`
}
// https://core.telegram.org/bots/api#userprofilephotos
type UserProfilePhotos struct {
TotalCount int `json:"total_count"`
Photos [][]*PhotoSize `json:"photos"`
}
// https://core.telegram.org/bots/api#file
type File struct {
FileID string `json:"file_id"`
FileSize *int `json:"file_size"`
FilePath *string `json:"file_path"`
}
// https://core.telegram.org/bots/api#replykeyboardmarkup
type ReplyKeyboardMarkup struct {
Keyboard [][]*KeyboardButton `json:"keyboard"`
ResizeKeyboard bool `json:"resize_keyboard,omitempty"`
OneTimeKeyboard bool `json:"one_time_keyboard,omitempty"`
Selective bool `json:"selective,omitempty"`
}
// https://core.telegram.org/bots/api#keyboardbutton
type KeyboardButton struct {
Text string `json:"text"`
RequestContact bool `json:"request_contact,omitempty"`
RequestLocation bool `json:"request_location,omitempty"`
}
// https://core.telegram.org/bots/api#replykeyboardremove
type ReplyKeyboardRemove struct {
RemoveKeyboard bool `json:"remove_keyboard,omitempty"`
Selective bool `json:"selective,omitempty"`
}
// https://core.telegram.org/bots/api#inlinekeyboardmarkup
type InlineKeyboardMarkup struct {
InlineKeyboard [][]*InlineKeyboardButton `json:"inline_keyboard"`
}
// https://core.telegram.org/bots/api#inlinekeyboardbutton
type InlineKeyboardButton struct {
Text string `json:"text"`
URL string `json:"url,omitempty"`
CallbackData string `json:"callback_data,omitempty"`
SwitchInlineQuery string `json:"switch_inline_query,omitempty"`
SwitchInlineQueryCurrentChat string `json:"switch_inline_query_current_chat,omitempty"`
// CallbackGame
// Pay
}
// https://core.telegram.org/bots/api#callbackquery
type CallbackQuery struct {
ID string `json:"id"`
From User `json:"from"`
Message *Message `json:"message"`
InlineMessageID *string `json:"inline_message_id"`
ChatInstance string `json:"chat_instance"`
Data *string `json:"data"`
// GameShortName *string
}
// https://core.telegram.org/bots/api#forcereply
type ForceReply struct {
ForeceReply bool `json:"force_reply"`
Selective bool `json:"selective"`
}
// https://core.telegram.org/bots/api#chatphoto
type ChatPhoto struct {
SmallFileID string `json:"small_file_id"`
BigFileID string `json:"big_file_id"`
}
// https://core.telegram.org/bots/api#chatmember
type ChatMember struct {
User User `json:"user"`
Status string `json:"status"`
UntilDate *int `json:"until_date"`
// Administrators only.
CanBeEdited *bool `json:"can_be_edited"`
CanChangeInfo *bool `json:"can_change_info"`
CanPostMessages *bool `json:"can_post_messages"`
CanEditMessages *bool `json:"can_edit_messages"`
CanDeleteMessages *bool `json:"can_delete_messages"`
CanInviteUsers *bool `json:"can_invite_users"`
CanRestrictMembers *bool `json:"can_restrict_members"`
CanPinMessages *bool `json:"can_pin_messages"`
CanPromoteMembers *bool `json:"can_promote_members"`
CanSendMessages *bool `json:"can_send_messages"`
CanSendMediaMessages *bool `json:"can_send_media_messages"`
CanSendOtherMessages *bool `json:"can_send_other_messages"`
CanAddWebPagePreviews *bool `json:"can_add_web_page_previews"`
}
// https://core.telegram.org/bots/api#responseparameters
type ResponseParameters struct {
MigrateToChatID *int64 `json:"migrate_to_chat_id"`
RetryAfter *int `json:"retry_after"`
}
// https://core.telegram.org/bots/api#inputfile
type InputFile interface {
io.Reader
Name() string
}
// Parse modes.
const (
ModeDefault ParseMode = 0
ModeMarkdown = 1
ModeHTML = 2
)
type ParseMode int
// MarshalJSON implements json.Marshaler interface.
func (m ParseMode) MarshalJSON() (b []byte, err error) {
switch m {
case ModeDefault:
// ModeDefault should be evaluated as empty by json package and skipped
// in message marshalling. But empty string was a simplest solution to
// leave ParseMode a simple type.
b = []byte(`""`)
case ModeMarkdown:
b = []byte(`"Markdown"`)
case ModeHTML:
b = []byte(`"HTML"`)
}
return
}
// https://core.telegram.org/bots/api#sendmessage
type TextMessage struct {
ChatID int64 `json:"chat_id"`
Text string `json:"text"`
ParseMode ParseMode `json:"parse_mode,omitempty"`
DisableWebPagePreview bool `json:"disable_web_page_preview,omitempty"`
DisableNotification bool `json:"disable_notification,omitempty"`
ReplyToMessageID int `json:"reply_to_message_id,omitempty"`
ReplyMarkup Markup `json:"reply_markup,omitempty"`
}
type Markup interface {
json.Marshaler
json.Unmarshaler
}
var _ Markup = (*ReplyKeyboardMarkup)(nil)
var _ Markup = (*ReplyKeyboardRemove)(nil)
var _ Markup = (*InlineKeyboardMarkup)(nil)
var _ Markup = (*ForceReply)(nil)
// https://core.telegram.org/bots/api#forwardmessage
type ForwardedMessage struct {
ChatID int64 `json:"chat_id"`
FromChatID int64 `json:"from_chat_id"`
DisableNotification bool `json:"disable_notification,omitempty"`
MessageID int `json:"message_id"`
}
// https://core.telegram.org/bots/api#sendphoto
type PhotoMessage struct {
ChatID int64 `json:"chat_id"`
Photo InputFile `json:"-"`
PhotoID string `json:"photo,omitempty"`
Caption string `json:"caption,omitempty"`
DisableNotification bool `json:"disable_notification,omitempty"`
ReplyToMessageID int `json:"reply_to_message_id,omitempty"`
}
// Multipart implements Multiparter interface.
func (m *PhotoMessage) Multipart() *Multipart {
if m.Photo == nil {
return nil
}
return &Multipart{
Files: map[string]InputFile{"photo": m.Photo},
// TODO: Do not include optional fields.
Form: url.Values{
"chat_id": {strconv.FormatInt(m.ChatID, 10)},
"caption": {m.Caption},
"disable_notification": {strconv.FormatBool(m.DisableNotification)},
"reply_to_message_id": {strconv.FormatInt(int64(m.ReplyToMessageID), 10)},
},
}
}
// https://core.telegram.org/bots/api#sendaudio
type AudioMessage struct {
ChatID int64 `json:"chat_id"`
Audio InputFile `json:"-"`
AudioID string `json:"audio"`
Caption string `json:"caption,omitempty"`
Duration int `json:"duration,omitempty"`
Performer string `json:"performer,omitempty"`
Title string `json:"title,omitempty"`
DisableNotification bool `json:"disable_notification,omitempty"`
ReplyToMessageID int `json:"reply_to_message_id,omitempty"`
ReplyMarkup Markup `json:"reply_markup,omitempty"`
}
// Multipart implements Multiparter interface.
func (m *AudioMessage) Multipart() *Multipart {
if m.Audio == nil {
return nil
}
var markup string
if m.ReplyMarkup != nil {
if b, err := m.ReplyMarkup.MarshalJSON(); err == nil {
markup = string(b)
}
}
return &Multipart{
Files: map[string]InputFile{"audio": m.Audio},
// TODO: Do not include optional fields.
Form: url.Values{
"chat_id": {strconv.FormatInt(m.ChatID, 10)},
"caption": {m.Caption},
"duration": {strconv.FormatInt(int64(m.Duration), 10)},
"performer": {m.Performer},
"title": {m.Title},
"disable_notification": {strconv.FormatBool(m.DisableNotification)},
"reply_to_message_id": {strconv.FormatInt(int64(m.ReplyToMessageID), 10)},
"reply_markup": {markup},
},
}
}
// https://core.telegram.org/bots/api#senddocument
type DocumentMessage struct {
ChatID int64 `json:"chat_id"`
Document InputFile `json:"document"`
Caption string `json:"caption,omitempty"`
DisableNotification bool `json:"disable_notification,omitempty"`
ReplyToMessageID int `json:"reply_to_message_id,omitempty"`
ReplyMarkup Markup `json:"reply_markup,omitempty"`
}
// Multipart implements Multiparter interface.
func (m *DocumentMessage) Multipart() *Multipart {
if m.Document == nil {
return nil
}
var markup string
if m.ReplyMarkup != nil {
if b, err := m.ReplyMarkup.MarshalJSON(); err == nil {
markup = string(b)
}
}
return &Multipart{
Files: map[string]InputFile{"document": m.Document},
// TODO: Do not include optional fields.
Form: url.Values{
"chat_id": {strconv.FormatInt(m.ChatID, 10)},
"caption": {m.Caption},
"disable_notification": {strconv.FormatBool(m.DisableNotification)},
"reply_to_message_id": {strconv.FormatInt(int64(m.ReplyToMessageID), 10)},
"reply_markup": {markup},
},
}
}
// VideoMessage
// VoiceMessage
// VideoNoteMessage
var _ = (Multiparter)((*PhotoMessage)(nil))
var _ = (Multiparter)((*AudioMessage)(nil))
var _ = (Multiparter)((*DocumentMessage)(nil))
// var _ = (Multiparter)((*VideoMessage)(nil))
// var _ = (Multiparter)((*VoiceMessage)(nil))
// var _ = (Multiparter)((*VideoNoteMessage)(nil))
// LocationMessage
// VenueMessage
// ContactMessage
// ChatActionMessage
// UserProfilePhotosMessage
// KickChatMemberMessage
// UnbanChatMemberMessage
// RestrictChatMemberMessage
// PromoteChatMemberMessage
// ExportChatInviteLinkMessage
// SetChatPhotoMessage
// DeleteChatPhotoMessage
// SetChatTitleMessage
// SetChatDescriptionMessage
// PinChatMessageMessage
// UnpinChatMessageMessage
// LeaveChatMessage
// getChat
// getChatAdministrators
// getChatMembersCount
// getChatMember
// TODO: Replace
type ChatID int64
// https://core.telegram.org/bots/api#answercallbackquery
type CallbackQueryAnswer struct {
CallbackQueryID string `json:"callback_query_id"`
Text string `json:"text,omitempty"`
ShowAlert bool `json:"show_alert,omitempty"`
URL string `json:"url,omitempty"`
CacheTime int `json:"cache_time,omitempty"`
}
// Updating messages
// https://core.telegram.org/bots/api#updating-messages
// https://core.telegram.org/bots/api#editmessagetext
type MessageText struct {
ChatID int64 `json:"chat_id,omitempty"`
MessageID int `json:"message_id,omitempty"`
InlineMessageID int `json:"inline_message_id,omitempty"`
Text string `json:"text"`
ParseMode ParseMode `json:"parse_mode"`
DisableWebPagePreview bool `json:"disable_web_page_preview"`
ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
}
// https://core.telegram.org/bots/api#editmessagecaption
type MessageCaption struct {
ChatID int64 `json:"chat_id,omitempty"`
MessageID int `json:"message_id,omitempty"`
InlineMessageID int `json:"inline_message_id,omitempty"`
Caption string `json:"caption,omitempty"`
ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
}
// https://core.telegram.org/bots/api#editmessagereplymarkup
type MessageReplyMarkup struct {
ChatID int64 `json:"chat_id,omitempty"`
MessageID int `json:"message_id,omitempty"`
InlineMessageID int `json:"inline_message_id,omitempty"`
ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
}
// https://core.telegram.org/bots/api#deletemessage
type DeletedMessage struct {
ChatID int64 `json:"chat_id"`
MessageID int `json:"message_id"`
}
// Stickers
// https://core.telegram.org/bots/api#stickers
// https://core.telegram.org/bots/api#sticker
type Sticker struct {
FileID string `json:"file_id"`
Width int `json:"width"`
Height int `json:"height"`
Thumb *PhotoSize `json:"thumb"`
Emoji *string `json:"emoji"`
SetName *string `json:"set_name"`
MaskPosition *MaskPosition `json:"mask_position"`
FileSize *int `json:"file_size"`
}
// https://core.telegram.org/bots/api#stickerset
type StickerSet struct {
Name string `json:"name"`
Title string `json:"title"`
ContainsMasks bool `json:"contains_masks"`
Stickers []*Sticker `json:"stickers"`
}
// https://core.telegram.org/bots/api#maskposition
type MaskPosition struct {
Point string `json:"point"`
XShift float32 `json:"x_shift"`
YShift float32 `json:"y_shift"`
Scale float32 `json:"scale"`
}
// https://core.telegram.org/bots/api#sendsticker
type StickerMessage struct {
ChatID int64 `json:"chat_id"`
Sticker InputFile `json:"-"`
StickerID string `json:"sticker"`
DisableNotification bool `json:"disalbe_notification,omitempty"`
ReplyToMessageID int `json:"reply_to_message_id,omitempty"`
ReplyMarkup Markup `json:"reply_markup,omitempty"`
}
// Inline mode
// https://core.telegram.org/bots/api#inline-mode
// TODO: Add types and methods.