-
Notifications
You must be signed in to change notification settings - Fork 6
/
types_gen_ext.go
603 lines (499 loc) 路 16.8 KB
/
types_gen_ext.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
595
596
597
598
599
600
601
602
603
package tg
import (
"encoding/json"
"fmt"
"strconv"
)
type ChatID int64
var _ PeerID = (ChatID)(0)
func (id ChatID) PeerID() string {
return strconv.FormatInt(int64(id), 10)
}
// ChatType represents enum of possible chat types.
type ChatType int8
const (
// ChatTypePrivate represents one-to-one chat.
ChatTypePrivate ChatType = iota + 1
// ChatTypeGroup represents group chats.
ChatTypeGroup
// ChatTypeSupergroup supergroup chats.
ChatTypeSupergroup
// ChatTypeChannel represents channels
ChatTypeChannel
// ChatTypeSender for a private chat with the inline query sender
ChatTypeSender
)
func (chatType ChatType) String() string {
if chatType < ChatTypePrivate || chatType > ChatTypeSender {
return "unknown"
}
return [...]string{"private", "group", "supergroup", "channel", "sender"}[chatType-1]
}
func (chatType ChatType) MarshalJSON() ([]byte, error) {
return json.Marshal(chatType.String())
}
func (chatType *ChatType) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch s {
case "private":
*chatType = ChatTypePrivate
case "group":
*chatType = ChatTypeGroup
case "supergroup":
*chatType = ChatTypeSupergroup
case "channel":
*chatType = ChatTypeChannel
case "sender":
*chatType = ChatTypeSender
default:
return fmt.Errorf("unknown chat type: %s", s)
}
return nil
}
// ChatAction type of action to broadcast via sendChatAction.
type ChatAction int8
const (
ChatActionTyping ChatAction = iota + 1
ChatActionUploadPhoto
ChatActionRecordVideo
ChatActionUploadVideo
ChatActionRecordVoice
ChatActionUploadVoice
ChatActionUploadDocument
ChatActionChooseSticker
ChatActionFindLocation
ChatActionRecordVideoNote
ChatActionUploadVideoNote
)
func (action ChatAction) String() string {
if action < ChatActionTyping || action > ChatActionUploadVideoNote {
return "unknown"
}
return [...]string{
"typing",
"upload_photo",
"record_video",
"upload_video",
"record_voice",
"upload_voice",
"upload_document",
"choose_sticker",
"find_location",
"record_video_note",
"upload_video_note",
}[action-1]
}
// UserID it's unique identifier for Telegram user or bot.
type UserID int64
var _ PeerID = (UserID)(0)
func (id UserID) PeerID() string {
return strconv.FormatInt(int64(id), 10)
}
type Username string
func (un Username) PeerID() string {
return "@" + string(un)
}
type PeerID interface {
PeerID() string
}
// MessageID it's unique identifier for a message in a chat.
type MessageID int
type FileID string
type FileArg struct {
FileID FileID
Upload InputFile
}
//go:generate go run github.com/mr-linch/go-tg-gen@latest -types-output types_gen.go
func (chat Chat) PeerID() string {
return chat.ID.PeerID()
}
type InputMedia struct {
}
type BotCommandScope struct {
}
type CallbackGame struct{}
// ReplyMarkup represents a custom keyboard.
type ReplyMarkup interface {
isReplyMarkup()
}
var _ ReplyMarkup = (*InlineKeyboardMarkup)(nil)
// NewInlineKeyboardMarkup creates a new InlineKeyboardMarkup.
func NewInlineKeyboardMarkup(rows ...[]InlineKeyboardButton) InlineKeyboardMarkup {
return InlineKeyboardMarkup{
InlineKeyboard: rows,
}
}
func (markup InlineKeyboardMarkup) Ptr() *InlineKeyboardMarkup {
return &markup
}
// NewInlineButtonURL create inline button
// with http(s):// or tg:// URL to be opened when the button is pressed.
func NewInlineKeyboardButtonURL(text string, url string) InlineKeyboardButton {
return InlineKeyboardButton{
Text: text,
URL: url,
}
}
// NewInlineKeyboardButtonCallback creates a new InlineKeyboardButton with specified callback data.
// Query should have length 1-64 bytes.
func NewInlineKeyboardButtonCallback(text string, query string) InlineKeyboardButton {
return InlineKeyboardButton{
Text: text,
CallbackData: query,
}
}
// NewInlineKeyboardButtonWebApp creates a button that open a web app.
func NewInlineKeyboardButtonWebApp(text string, webApp WebAppInfo) InlineKeyboardButton {
return InlineKeyboardButton{
Text: text,
WebApp: &webApp,
}
}
// InlineKeyboardMarkup represents button that open web page with auth data.
func NewInlineKeyboardButtonLoginURL(text string, loginURL LoginUrl) InlineKeyboardButton {
return InlineKeyboardButton{
Text: text,
LoginURL: &loginURL,
}
}
// NewInlineKeyboardButtonSwitchInlineQuery represents button that
// will prompt the user to select one of their chats,
// open that chat and insert the bot's username and the specified inline query in the input field.
func NewInlineKeyboardButtonSwitchInlineQuery(text string, query string) InlineKeyboardButton {
return InlineKeyboardButton{
Text: text,
SwitchInlineQuery: query,
}
}
// NewInlineKeyboardButtonSwitchInlineQueryCurrentChat represents button that
// will insert the bot's username and the specified inline query in the current chat's input field
func NewInlineKeyboardButtonSwitchInlineQueryCurrentChat(text string, query string) InlineKeyboardButton {
return InlineKeyboardButton{
Text: text,
SwitchInlineQueryCurrentChat: query,
}
}
// NewInlineKeyboardButtonCallbackGame represents the button which open a game.
func NewInlineKeyboardButtonCallbackGame(text string) InlineKeyboardButton {
return InlineKeyboardButton{
Text: text,
CallbackGame: &CallbackGame{},
}
}
// NewInlineKeyboardButtonPay represents a Pay button.
// NOTE: This type of button must always be the first button in the first row and can only be used in invoice messages
func NewInlineKeyboardButtonPay(text string) InlineKeyboardButton {
return InlineKeyboardButton{
Text: text,
Pay: true,
}
}
func (markup InlineKeyboardMarkup) isReplyMarkup() {}
// NewReplyKeyboardMarkup creates a new ReplyKeyboardMarkup.
func NewReplyKeyboardMarkup(rows ...[]KeyboardButton) *ReplyKeyboardMarkup {
return &ReplyKeyboardMarkup{
Keyboard: rows,
}
}
var _ ReplyMarkup = (*ReplyKeyboardMarkup)(nil)
// WithResizeKeyboard requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons).
// Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
func (markup *ReplyKeyboardMarkup) WithResizeKeyboardMarkup() *ReplyKeyboardMarkup {
markup.ResizeKeyboard = true
return markup
}
// WithOneTimeKeyboard requests clients to hide the keyboard as soon as it's been used.
// The keyboard will still be available, but clients will automatically display the
// usual letter-keyboard in the chat - the user can press a special button in
// the input field to see the custom keyboard again.
// Defaults to false.
func (markup *ReplyKeyboardMarkup) WithOneTimeKeyboardMarkup() *ReplyKeyboardMarkup {
markup.OneTimeKeyboard = true
return markup
}
// WithInputFieldPlaceholder sets the placeholder to be shown in the input field when the keyboard is active;
// 1-64 characters
func (markup *ReplyKeyboardMarkup) WithInputFieldPlaceholder(placeholder string) *ReplyKeyboardMarkup {
markup.InputFieldPlaceholder = placeholder
return markup
}
// Use this parameter if you want to show the keyboard to specific users only.
func (markup *ReplyKeyboardMarkup) WithSelective() *ReplyKeyboardMarkup {
markup.Selective = true
return markup
}
// NewKeyboardButton creates a plain reply keyboard button.
func NewKeyboardButton(text string) KeyboardButton {
return KeyboardButton{
Text: text,
}
}
// NewKeyboardButtonRequestContact creates a reply keyboard button that request a contact from user.
// Available in private chats only.
func NewKeyboardButtonRequestContact(text string) KeyboardButton {
return KeyboardButton{
Text: text,
RequestContact: true,
}
}
// NewKeyboardButtonRequestLocation creates a reply keyboard button that request a location from user.
// Available in private chats only.
func NewKeyboardButtonRequestLocation(text string) KeyboardButton {
return KeyboardButton{
Text: text,
RequestLocation: true,
}
}
// NewKeyboardButtonRequestPoll creates a reply keyboard button that request a poll from user.
// Available in private chats only.
func NewKeyboardButtonRequestPoll(text string, poll KeyboardButtonPollType) KeyboardButton {
return KeyboardButton{
Text: text,
RequestPoll: &poll,
}
}
// NewKeyboardButtonWebApp create a reply keyboard button that open a web app.
func NewKeyboardButtonWebApp(text string, webApp WebAppInfo) KeyboardButton {
return KeyboardButton{
Text: text,
WebApp: &webApp,
}
}
func (markup ReplyKeyboardMarkup) isReplyMarkup() {}
var _ ReplyMarkup = (*ReplyKeyboardRemove)(nil)
// NewReplyKeyboardRemove creates a new ReplyKeyboardRemove.
func NewReplyKeyboardRemove() *ReplyKeyboardRemove {
return &ReplyKeyboardRemove{
RemoveKeyboard: true,
}
}
// WithSelective set it if you want to remove the keyboard for specific users only.
func (markup *ReplyKeyboardRemove) WithSelective() *ReplyKeyboardRemove {
markup.Selective = true
return markup
}
func (markup ReplyKeyboardRemove) isReplyMarkup() {}
var _ ReplyMarkup = (*ForceReply)(nil)
// NewForceReply creates a new ForceReply.
func NewForceReply() *ForceReply {
return &ForceReply{
ForceReply: true,
}
}
// WithSelective set it if you want to force reply for specific users only.
func (markup *ForceReply) WithSelective() *ForceReply {
markup.Selective = true
return markup
}
// WithInputFieldPlaceholder sets the placeholder to be shown in the input field when the reply is active;
// 1-64 characters
func (markup *ForceReply) WithInputFieldPlaceholder(placeholder string) *ForceReply {
markup.InputFieldPlaceholder = placeholder
return markup
}
func (markup ForceReply) isReplyMarkup() {}
// NewButtonRow it's generic helper for create keyboards in functional way.
func NewButtonRow[T Button](buttons ...T) []T {
return buttons
}
// Button define generic button interface
type Button interface {
InlineKeyboardButton | KeyboardButton
}
// ButtonLayout it's build for fixed width keyboards.
type ButtonLayout[T Button] struct {
buttons [][]T
rowWidth int
}
// NewButtonColumn returns keyboard from a single column of Button.
func NewButtonColumn[T Button](buttons ...T) [][]T {
result := make([][]T, 0, len(buttons))
for _, button := range buttons {
result = append(result, []T{button})
}
return result
}
// NewButtonLayout creates layout with specified width.
// Buttons will be added via Insert method.
func NewButtonLayout[T Button](rowWidth int, buttons ...T) *ButtonLayout[T] {
layout := &ButtonLayout[T]{
rowWidth: rowWidth,
buttons: make([][]T, 0),
}
return layout.Insert(buttons...)
}
// Keyboard returns result of building.
func (layout *ButtonLayout[T]) Keyboard() [][]T {
return layout.buttons
}
// Insert buttons to last row if possible, or create new and insert.
func (layout *ButtonLayout[T]) Insert(buttons ...T) *ButtonLayout[T] {
for _, button := range buttons {
layout.insert(button)
}
return layout
}
func (layout *ButtonLayout[T]) insert(button T) *ButtonLayout[T] {
if len(layout.buttons) > 0 && len(layout.buttons[len(layout.buttons)-1]) < layout.rowWidth {
layout.buttons[len(layout.buttons)-1] = append(layout.buttons[len(layout.buttons)-1], button)
} else {
layout.buttons = append(layout.buttons, []T{button})
}
return layout
}
// Add accepts any number of buttons,
// always starts adding from a new row
// and adds a row when it reaches the set width.
func (layout *ButtonLayout[T]) Add(buttons ...T) *ButtonLayout[T] {
row := make([]T, 0, layout.rowWidth)
for _, button := range buttons {
if len(row) == layout.rowWidth {
layout.buttons = append(layout.buttons, row)
row = make([]T, 0, layout.rowWidth)
}
row = append(row, button)
}
if len(row) > 0 {
layout.buttons = append(layout.buttons, row)
}
return layout
}
// Row add new row with no respect for row width
func (layout *ButtonLayout[T]) Row(buttons ...T) *ButtonLayout[T] {
layout.buttons = append(layout.buttons, buttons)
return layout
}
type InlineQueryResult interface {
isInlineQueryResult()
}
func (InlineQueryResultCachedAudio) isInlineQueryResult() {}
func (result InlineQueryResultCachedAudio) MarshalJSON() ([]byte, error) {
result.Type = "audio"
type alias InlineQueryResultCachedAudio
return json.Marshal(alias(result))
}
func (InlineQueryResultCachedDocument) isInlineQueryResult() {}
func (result InlineQueryResultCachedDocument) MarshalJSON() ([]byte, error) {
result.Type = "document"
type alias InlineQueryResultCachedDocument
return json.Marshal(alias(result))
}
func (InlineQueryResultCachedGif) isInlineQueryResult() {}
func (result InlineQueryResultCachedGif) MarshalJSON() ([]byte, error) {
result.Type = "gif"
type alias InlineQueryResultCachedGif
return json.Marshal(alias(result))
}
func (InlineQueryResultCachedMpeg4Gif) isInlineQueryResult() {}
func (result InlineQueryResultCachedMpeg4Gif) MarshalJSON() ([]byte, error) {
result.Type = "mpeg4_gif"
type alias InlineQueryResultCachedMpeg4Gif
return json.Marshal(alias(result))
}
func (InlineQueryResultCachedPhoto) isInlineQueryResult() {}
func (result InlineQueryResultCachedPhoto) MarshalJSON() ([]byte, error) {
result.Type = "photo"
type alias InlineQueryResultCachedPhoto
return json.Marshal(alias(result))
}
func (InlineQueryResultCachedSticker) isInlineQueryResult() {}
func (result InlineQueryResultCachedSticker) MarshalJSON() ([]byte, error) {
result.Type = "sticker"
type alias InlineQueryResultCachedSticker
return json.Marshal(alias(result))
}
func (InlineQueryResultCachedVideo) isInlineQueryResult() {}
func (result InlineQueryResultCachedVideo) MarshalJSON() ([]byte, error) {
result.Type = "video"
type alias InlineQueryResultCachedVideo
return json.Marshal(alias(result))
}
func (InlineQueryResultCachedVoice) isInlineQueryResult() {}
func (result InlineQueryResultCachedVoice) MarshalJSON() ([]byte, error) {
result.Type = "voice"
type alias InlineQueryResultCachedVoice
return json.Marshal(alias(result))
}
func (InlineQueryResultAudio) isInlineQueryResult() {}
func (result InlineQueryResultAudio) MarshalJSON() ([]byte, error) {
result.Type = "audio"
type alias InlineQueryResultAudio
return json.Marshal(alias(result))
}
func (InlineQueryResultDocument) isInlineQueryResult() {}
func (result InlineQueryResultDocument) MarshalJSON() ([]byte, error) {
result.Type = "document"
type alias InlineQueryResultDocument
return json.Marshal(alias(result))
}
func (InlineQueryResultGif) isInlineQueryResult() {}
func (result InlineQueryResultGif) MarshalJSON() ([]byte, error) {
result.Type = "gif"
type alias InlineQueryResultGif
return json.Marshal(alias(result))
}
func (InlineQueryResultMpeg4Gif) isInlineQueryResult() {}
func (result InlineQueryResultMpeg4Gif) MarshalJSON() ([]byte, error) {
result.Type = "mpeg4_gif"
type alias InlineQueryResultMpeg4Gif
return json.Marshal(alias(result))
}
func (InlineQueryResultPhoto) isInlineQueryResult() {}
func (result InlineQueryResultPhoto) MarshalJSON() ([]byte, error) {
result.Type = "photo"
type alias InlineQueryResultPhoto
return json.Marshal(alias(result))
}
func (InlineQueryResultVideo) isInlineQueryResult() {}
func (result InlineQueryResultVideo) MarshalJSON() ([]byte, error) {
result.Type = "video"
type alias InlineQueryResultVideo
return json.Marshal(alias(result))
}
func (InlineQueryResultVoice) isInlineQueryResult() {}
func (result InlineQueryResultVoice) MarshalJSON() ([]byte, error) {
result.Type = "voice"
type alias InlineQueryResultVoice
return json.Marshal(alias(result))
}
func (InlineQueryResultArticle) isInlineQueryResult() {}
func (result InlineQueryResultArticle) MarshalJSON() ([]byte, error) {
result.Type = "article"
type alias InlineQueryResultArticle
return json.Marshal(alias(result))
}
func (InlineQueryResultContact) isInlineQueryResult() {}
func (result InlineQueryResultContact) MarshalJSON() ([]byte, error) {
result.Type = "contact"
type alias InlineQueryResultContact
return json.Marshal(alias(result))
}
func (InlineQueryResultGame) isInlineQueryResult() {}
func (result InlineQueryResultGame) MarshalJSON() ([]byte, error) {
result.Type = "game"
type alias InlineQueryResultGame
return json.Marshal(alias(result))
}
func (InlineQueryResultLocation) isInlineQueryResult() {}
func (result InlineQueryResultLocation) MarshalJSON() ([]byte, error) {
result.Type = "location"
type alias InlineQueryResultLocation
return json.Marshal(alias(result))
}
func (InlineQueryResultVenue) isInlineQueryResult() {}
func (result InlineQueryResultVenue) MarshalJSON() ([]byte, error) {
result.Type = "venue"
type alias InlineQueryResultVenue
return json.Marshal(alias(result))
}
type InputMessageContent interface {
isInputMessageContent()
}
func (content InputTextMessageContent) isInputMessageContent() {}
func (content InputLocationMessageContent) isInputMessageContent() {}
func (content InputVenueMessageContent) isInputMessageContent() {}
func (content InputContactMessageContent) isInputMessageContent() {}
func (content InputInvoiceMessageContent) isInputMessageContent() {}