helpers.go (view raw)
1package tgbotapi
2
3import (
4 "log"
5 "net/url"
6)
7
8// NewMessage creates a new Message.
9//
10// chatID is where to send it, text is the message text.
11func NewMessage(chatID int64, text string) MessageConfig {
12 return MessageConfig{
13 BaseChat: BaseChat{
14 ChatID: chatID,
15 ReplyToMessageID: 0,
16 },
17 Text: text,
18 DisableWebPagePreview: false,
19 }
20}
21
22func NewDeleteMessage(chatID int64, messageID int) DeleteMessageConfig {
23 return DeleteMessageConfig{
24 ChatID: chatID,
25 MessageID: messageID,
26 }
27}
28
29// NewMessageToChannel creates a new Message that is sent to a channel
30// by username.
31//
32// username is the username of the channel, text is the message text.
33func NewMessageToChannel(username string, text string) MessageConfig {
34 return MessageConfig{
35 BaseChat: BaseChat{
36 ChannelUsername: username,
37 },
38 Text: text,
39 }
40}
41
42// NewForward creates a new forward.
43//
44// chatID is where to send it, fromChatID is the source chat,
45// and messageID is the ID of the original message.
46func NewForward(chatID int64, fromChatID int64, messageID int) ForwardConfig {
47 return ForwardConfig{
48 BaseChat: BaseChat{ChatID: chatID},
49 FromChatID: fromChatID,
50 MessageID: messageID,
51 }
52}
53
54// NewPhotoUpload creates a new photo uploader.
55//
56// chatID is where to send it, file is a string path to the file,
57// FileReader, or FileBytes.
58//
59// Note that you must send animated GIFs as a document.
60func NewPhotoUpload(chatID int64, file interface{}) PhotoConfig {
61 return PhotoConfig{
62 BaseFile: BaseFile{
63 BaseChat: BaseChat{ChatID: chatID},
64 File: file,
65 UseExisting: false,
66 },
67 }
68}
69
70// NewPhotoShare shares an existing photo.
71// You may use this to reshare an existing photo without reuploading it.
72//
73// chatID is where to send it, fileID is the ID of the file
74// already uploaded.
75func NewPhotoShare(chatID int64, fileID string) PhotoConfig {
76 return PhotoConfig{
77 BaseFile: BaseFile{
78 BaseChat: BaseChat{ChatID: chatID},
79 FileID: fileID,
80 UseExisting: true,
81 },
82 }
83}
84
85// NewAudioUpload creates a new audio uploader.
86//
87// chatID is where to send it, file is a string path to the file,
88// FileReader, or FileBytes.
89func NewAudioUpload(chatID int64, file interface{}) AudioConfig {
90 return AudioConfig{
91 BaseFile: BaseFile{
92 BaseChat: BaseChat{ChatID: chatID},
93 File: file,
94 UseExisting: false,
95 },
96 }
97}
98
99// NewAudioShare shares an existing audio file.
100// You may use this to reshare an existing audio file without
101// reuploading it.
102//
103// chatID is where to send it, fileID is the ID of the audio
104// already uploaded.
105func NewAudioShare(chatID int64, fileID string) AudioConfig {
106 return AudioConfig{
107 BaseFile: BaseFile{
108 BaseChat: BaseChat{ChatID: chatID},
109 FileID: fileID,
110 UseExisting: true,
111 },
112 }
113}
114
115// NewDocumentUpload creates a new document uploader.
116//
117// chatID is where to send it, file is a string path to the file,
118// FileReader, or FileBytes.
119func NewDocumentUpload(chatID int64, file interface{}) DocumentConfig {
120 return DocumentConfig{
121 BaseFile: BaseFile{
122 BaseChat: BaseChat{ChatID: chatID},
123 File: file,
124 UseExisting: false,
125 },
126 }
127}
128
129// NewDocumentShare shares an existing document.
130// You may use this to reshare an existing document without
131// reuploading it.
132//
133// chatID is where to send it, fileID is the ID of the document
134// already uploaded.
135func NewDocumentShare(chatID int64, fileID string) DocumentConfig {
136 return DocumentConfig{
137 BaseFile: BaseFile{
138 BaseChat: BaseChat{ChatID: chatID},
139 FileID: fileID,
140 UseExisting: true,
141 },
142 }
143}
144
145// NewStickerUpload creates a new sticker uploader.
146//
147// chatID is where to send it, file is a string path to the file,
148// FileReader, or FileBytes.
149func NewStickerUpload(chatID int64, file interface{}) StickerConfig {
150 return StickerConfig{
151 BaseFile: BaseFile{
152 BaseChat: BaseChat{ChatID: chatID},
153 File: file,
154 UseExisting: false,
155 },
156 }
157}
158
159// NewStickerShare shares an existing sticker.
160// You may use this to reshare an existing sticker without
161// reuploading it.
162//
163// chatID is where to send it, fileID is the ID of the sticker
164// already uploaded.
165func NewStickerShare(chatID int64, fileID string) StickerConfig {
166 return StickerConfig{
167 BaseFile: BaseFile{
168 BaseChat: BaseChat{ChatID: chatID},
169 FileID: fileID,
170 UseExisting: true,
171 },
172 }
173}
174
175// NewVideoUpload creates a new video uploader.
176//
177// chatID is where to send it, file is a string path to the file,
178// FileReader, or FileBytes.
179func NewVideoUpload(chatID int64, file interface{}) VideoConfig {
180 return VideoConfig{
181 BaseFile: BaseFile{
182 BaseChat: BaseChat{ChatID: chatID},
183 File: file,
184 UseExisting: false,
185 },
186 }
187}
188
189// NewVideoShare shares an existing video.
190// You may use this to reshare an existing video without reuploading it.
191//
192// chatID is where to send it, fileID is the ID of the video
193// already uploaded.
194func NewVideoShare(chatID int64, fileID string) VideoConfig {
195 return VideoConfig{
196 BaseFile: BaseFile{
197 BaseChat: BaseChat{ChatID: chatID},
198 FileID: fileID,
199 UseExisting: true,
200 },
201 }
202}
203
204// NewVideoNoteUpload creates a new video note uploader.
205//
206// chatID is where to send it, file is a string path to the file,
207// FileReader, or FileBytes.
208func NewVideoNoteUpload(chatID int64, length int, file interface{}) VideoNoteConfig {
209 return VideoNoteConfig{
210 BaseFile: BaseFile{
211 BaseChat: BaseChat{ChatID: chatID},
212 File: file,
213 UseExisting: false,
214 },
215 Length: length,
216 }
217}
218
219// NewVideoNoteShare shares an existing video.
220// You may use this to reshare an existing video without reuploading it.
221//
222// chatID is where to send it, fileID is the ID of the video
223// already uploaded.
224func NewVideoNoteShare(chatID int64, length int, fileID string) VideoNoteConfig {
225 return VideoNoteConfig{
226 BaseFile: BaseFile{
227 BaseChat: BaseChat{ChatID: chatID},
228 FileID: fileID,
229 UseExisting: true,
230 },
231 Length: length,
232 }
233}
234
235// NewVoiceUpload creates a new voice uploader.
236//
237// chatID is where to send it, file is a string path to the file,
238// FileReader, or FileBytes.
239func NewVoiceUpload(chatID int64, file interface{}) VoiceConfig {
240 return VoiceConfig{
241 BaseFile: BaseFile{
242 BaseChat: BaseChat{ChatID: chatID},
243 File: file,
244 UseExisting: false,
245 },
246 }
247}
248
249// NewVoiceShare shares an existing voice.
250// You may use this to reshare an existing voice without reuploading it.
251//
252// chatID is where to send it, fileID is the ID of the video
253// already uploaded.
254func NewVoiceShare(chatID int64, fileID string) VoiceConfig {
255 return VoiceConfig{
256 BaseFile: BaseFile{
257 BaseChat: BaseChat{ChatID: chatID},
258 FileID: fileID,
259 UseExisting: true,
260 },
261 }
262}
263
264// NewContact allows you to send a shared contact.
265func NewContact(chatID int64, phoneNumber, firstName string) ContactConfig {
266 return ContactConfig{
267 BaseChat: BaseChat{
268 ChatID: chatID,
269 },
270 PhoneNumber: phoneNumber,
271 FirstName: firstName,
272 }
273}
274
275// NewLocation shares your location.
276//
277// chatID is where to send it, latitude and longitude are coordinates.
278func NewLocation(chatID int64, latitude float64, longitude float64) LocationConfig {
279 return LocationConfig{
280 BaseChat: BaseChat{
281 ChatID: chatID,
282 },
283 Latitude: latitude,
284 Longitude: longitude,
285 }
286}
287
288// NewVenue allows you to send a venue and its location.
289func NewVenue(chatID int64, title, address string, latitude, longitude float64) VenueConfig {
290 return VenueConfig{
291 BaseChat: BaseChat{
292 ChatID: chatID,
293 },
294 Title: title,
295 Address: address,
296 Latitude: latitude,
297 Longitude: longitude,
298 }
299}
300
301// NewChatAction sets a chat action.
302// Actions last for 5 seconds, or until your next action.
303//
304// chatID is where to send it, action should be set via Chat constants.
305func NewChatAction(chatID int64, action string) ChatActionConfig {
306 return ChatActionConfig{
307 BaseChat: BaseChat{ChatID: chatID},
308 Action: action,
309 }
310}
311
312// NewUserProfilePhotos gets user profile photos.
313//
314// userID is the ID of the user you wish to get profile photos from.
315func NewUserProfilePhotos(userID int) UserProfilePhotosConfig {
316 return UserProfilePhotosConfig{
317 UserID: userID,
318 Offset: 0,
319 Limit: 0,
320 }
321}
322
323// NewUpdate gets updates since the last Offset.
324//
325// offset is the last Update ID to include.
326// You likely want to set this to the last Update ID plus 1.
327func NewUpdate(offset int) UpdateConfig {
328 return UpdateConfig{
329 Offset: offset,
330 Limit: 0,
331 Timeout: 0,
332 }
333}
334
335// NewWebhook creates a new webhook.
336//
337// link is the url parsable link you wish to get the updates.
338func NewWebhook(link string) WebhookConfig {
339 u, _ := url.Parse(link)
340
341 return WebhookConfig{
342 URL: u,
343 }
344}
345
346// NewWebhookWithCert creates a new webhook with a certificate.
347//
348// link is the url you wish to get webhooks,
349// file contains a string to a file, FileReader, or FileBytes.
350func NewWebhookWithCert(link string, file interface{}) WebhookConfig {
351 u, _ := url.Parse(link)
352
353 return WebhookConfig{
354 URL: u,
355 Certificate: file,
356 }
357}
358
359// NewInlineQueryResultArticle creates a new inline query article.
360func NewInlineQueryResultArticle(id, title, messageText string) InlineQueryResultArticle {
361 return InlineQueryResultArticle{
362 Type: "article",
363 ID: id,
364 Title: title,
365 InputMessageContent: InputTextMessageContent{
366 Text: messageText,
367 },
368 }
369}
370
371// NewInlineQueryResultArticleMarkdown creates a new inline query article with Markdown parsing.
372func NewInlineQueryResultArticleMarkdown(id, title, messageText string) InlineQueryResultArticle {
373 return InlineQueryResultArticle{
374 Type: "article",
375 ID: id,
376 Title: title,
377 InputMessageContent: InputTextMessageContent{
378 Text: messageText,
379 ParseMode: "Markdown",
380 },
381 }
382}
383
384// NewInlineQueryResultArticleHTML creates a new inline query article with HTML parsing.
385func NewInlineQueryResultArticleHTML(id, title, messageText string) InlineQueryResultArticle {
386 return InlineQueryResultArticle{
387 Type: "article",
388 ID: id,
389 Title: title,
390 InputMessageContent: InputTextMessageContent{
391 Text: messageText,
392 ParseMode: "HTML",
393 },
394 }
395}
396
397// NewInlineQueryResultGIF creates a new inline query GIF.
398func NewInlineQueryResultGIF(id, url string) InlineQueryResultGIF {
399 return InlineQueryResultGIF{
400 Type: "gif",
401 ID: id,
402 URL: url,
403 }
404}
405
406// NewInlineQueryResultCachedGIF create a new inline query with cached photo.
407func NewInlineQueryResultCachedGIF(id, gifID string) InlineQueryResultCachedGIF {
408 return InlineQueryResultCachedGIF{
409 Type: "gif",
410 ID: id,
411 GifID: gifID,
412 }
413}
414
415// NewInlineQueryResultMPEG4GIF creates a new inline query MPEG4 GIF.
416func NewInlineQueryResultMPEG4GIF(id, url string) InlineQueryResultMPEG4GIF {
417 return InlineQueryResultMPEG4GIF{
418 Type: "mpeg4_gif",
419 ID: id,
420 URL: url,
421 }
422}
423
424// NewInlineQueryResultCachedPhoto create a new inline query with cached photo.
425func NewInlineQueryResultCachedMPEG4GIF(id, MPEG4GifID string) InlineQueryResultCachedMpeg4Gif {
426 return InlineQueryResultCachedMpeg4Gif{
427 Type: "mpeg4_gif",
428 ID: id,
429 MGifID: MPEG4GifID,
430 }
431}
432
433// NewInlineQueryResultPhoto creates a new inline query photo.
434func NewInlineQueryResultPhoto(id, url string) InlineQueryResultPhoto {
435 return InlineQueryResultPhoto{
436 Type: "photo",
437 ID: id,
438 URL: url,
439 }
440}
441
442// NewInlineQueryResultPhotoWithThumb creates a new inline query photo.
443func NewInlineQueryResultPhotoWithThumb(id, url, thumb string) InlineQueryResultPhoto {
444 return InlineQueryResultPhoto{
445 Type: "photo",
446 ID: id,
447 URL: url,
448 ThumbURL: thumb,
449 }
450}
451
452// NewInlineQueryResultCachedPhoto create a new inline query with cached photo.
453func NewInlineQueryResultCachedPhoto(id, photoID string) InlineQueryResultCachedPhoto {
454 return InlineQueryResultCachedPhoto{
455 Type: "photo",
456 ID: id,
457 PhotoID: photoID,
458 }
459}
460
461// NewInlineQueryResultVideo creates a new inline query video.
462func NewInlineQueryResultVideo(id, url string) InlineQueryResultVideo {
463 return InlineQueryResultVideo{
464 Type: "video",
465 ID: id,
466 URL: url,
467 }
468}
469
470// NewInlineQueryResultCachedVideo create a new inline query with cached video.
471func NewInlineQueryResultCachedVideo(id, videoID, title string) InlineQueryResultCachedVideo {
472 return InlineQueryResultCachedVideo{
473 Type: "video",
474 ID: id,
475 VideoID: videoID,
476 Title: title,
477 }
478}
479
480// NewInlineQueryResultAudio creates a new inline query audio.
481func NewInlineQueryResultAudio(id, url, title string) InlineQueryResultAudio {
482 return InlineQueryResultAudio{
483 Type: "audio",
484 ID: id,
485 URL: url,
486 Title: title,
487 }
488}
489
490// NewInlineQueryResultCachedAudio create a new inline query with cached photo.
491func NewInlineQueryResultCachedAudio(id, audioID string) InlineQueryResultCachedAudio {
492 return InlineQueryResultCachedAudio{
493 Type: "audio",
494 ID: id,
495 AudioID: audioID,
496 }
497}
498
499// NewInlineQueryResultVoice creates a new inline query voice.
500func NewInlineQueryResultVoice(id, url, title string) InlineQueryResultVoice {
501 return InlineQueryResultVoice{
502 Type: "voice",
503 ID: id,
504 URL: url,
505 Title: title,
506 }
507}
508
509// NewInlineQueryResultCachedVoice create a new inline query with cached photo.
510func NewInlineQueryResultCachedVoice(id, voiceID, title string) InlineQueryResultCachedVoice {
511 return InlineQueryResultCachedVoice{
512 Type: "voice",
513 ID: id,
514 VoiceID: voiceID,
515 Title: title,
516 }
517}
518
519// NewInlineQueryResultDocument creates a new inline query document.
520func NewInlineQueryResultDocument(id, url, title, mimeType string) InlineQueryResultDocument {
521 return InlineQueryResultDocument{
522 Type: "document",
523 ID: id,
524 URL: url,
525 Title: title,
526 MimeType: mimeType,
527 }
528}
529
530// NewInlineQueryResultCachedDocument create a new inline query with cached photo.
531func NewInlineQueryResultCachedDocument(id, documentID, title string) InlineQueryResultCachedDocument {
532 return InlineQueryResultCachedDocument{
533 Type: "document",
534 ID: id,
535 DocumentID: documentID,
536 Title: title,
537 }
538}
539
540// NewInlineQueryResultLocation creates a new inline query location.
541func NewInlineQueryResultLocation(id, title string, latitude, longitude float64) InlineQueryResultLocation {
542 return InlineQueryResultLocation{
543 Type: "location",
544 ID: id,
545 Title: title,
546 Latitude: latitude,
547 Longitude: longitude,
548 }
549}
550
551// NewEditMessageText allows you to edit the text of a message.
552func NewEditMessageText(chatID int64, messageID int, text string) EditMessageTextConfig {
553 return EditMessageTextConfig{
554 BaseEdit: BaseEdit{
555 ChatID: chatID,
556 MessageID: messageID,
557 },
558 Text: text,
559 }
560}
561
562// NewEditMessageCaption allows you to edit the caption of a message.
563func NewEditMessageCaption(chatID int64, messageID int, caption, parseMode string) EditMessageCaptionConfig {
564 return EditMessageCaptionConfig{
565 BaseEdit: BaseEdit{
566 ChatID: chatID,
567 MessageID: messageID,
568 },
569 Caption: caption,
570 ParseMode: parseMode,
571 }
572}
573
574// NewEditMessageReplyMarkup allows you to edit the inline
575// keyboard markup.
576func NewEditMessageReplyMarkup(chatID int64, messageID int, replyMarkup InlineKeyboardMarkup) EditMessageReplyMarkupConfig {
577 return EditMessageReplyMarkupConfig{
578 BaseEdit: BaseEdit{
579 ChatID: chatID,
580 MessageID: messageID,
581 ReplyMarkup: &replyMarkup,
582 },
583 }
584}
585
586// NewHideKeyboard hides the keyboard, with the option for being selective
587// or hiding for everyone.
588func NewHideKeyboard(selective bool) ReplyKeyboardHide {
589 log.Println("NewHideKeyboard is deprecated, please use NewRemoveKeyboard")
590
591 return ReplyKeyboardHide{
592 HideKeyboard: true,
593 Selective: selective,
594 }
595}
596
597// NewRemoveKeyboard hides the keyboard, with the option for being selective
598// or hiding for everyone.
599func NewRemoveKeyboard(selective bool) ReplyKeyboardRemove {
600 return ReplyKeyboardRemove{
601 RemoveKeyboard: true,
602 Selective: selective,
603 }
604}
605
606// NewKeyboardButton creates a regular keyboard button.
607func NewKeyboardButton(text string) KeyboardButton {
608 return KeyboardButton{
609 Text: text,
610 }
611}
612
613// NewKeyboardButtonContact creates a keyboard button that requests
614// user contact information upon click.
615func NewKeyboardButtonContact(text string) KeyboardButton {
616 return KeyboardButton{
617 Text: text,
618 RequestContact: true,
619 }
620}
621
622// NewKeyboardButtonLocation creates a keyboard button that requests
623// user location information upon click.
624func NewKeyboardButtonLocation(text string) KeyboardButton {
625 return KeyboardButton{
626 Text: text,
627 RequestLocation: true,
628 }
629}
630
631// NewKeyboardButtonRow creates a row of keyboard buttons.
632func NewKeyboardButtonRow(buttons ...KeyboardButton) []KeyboardButton {
633 var row []KeyboardButton
634
635 row = append(row, buttons...)
636
637 return row
638}
639
640// NewReplyKeyboard creates a new regular keyboard with sane defaults.
641func NewReplyKeyboard(rows ...[]KeyboardButton) ReplyKeyboardMarkup {
642 var keyboard [][]KeyboardButton
643
644 keyboard = append(keyboard, rows...)
645
646 return ReplyKeyboardMarkup{
647 ResizeKeyboard: true,
648 Keyboard: keyboard,
649 }
650}
651
652// NewInlineKeyboardButtonData creates an inline keyboard button with text
653// and data for a callback.
654func NewInlineKeyboardButtonData(text, data string) InlineKeyboardButton {
655 return InlineKeyboardButton{
656 Text: text,
657 CallbackData: &data,
658 }
659}
660
661// NewInlineKeyboardButtonURL creates an inline keyboard button with text
662// which goes to a URL.
663func NewInlineKeyboardButtonURL(text, url string) InlineKeyboardButton {
664 return InlineKeyboardButton{
665 Text: text,
666 URL: &url,
667 }
668}
669
670// NewInlineKeyboardButtonSwitch creates an inline keyboard button with
671// text which allows the user to switch to a chat or return to a chat.
672func NewInlineKeyboardButtonSwitch(text, sw string) InlineKeyboardButton {
673 return InlineKeyboardButton{
674 Text: text,
675 SwitchInlineQuery: &sw,
676 }
677}
678
679// NewInlineKeyboardRow creates an inline keyboard row with buttons.
680func NewInlineKeyboardRow(buttons ...InlineKeyboardButton) []InlineKeyboardButton {
681 var row []InlineKeyboardButton
682
683 row = append(row, buttons...)
684
685 return row
686}
687
688// NewInlineKeyboardMarkup creates a new inline keyboard.
689func NewInlineKeyboardMarkup(rows ...[]InlineKeyboardButton) InlineKeyboardMarkup {
690 var keyboard [][]InlineKeyboardButton
691
692 keyboard = append(keyboard, rows...)
693
694 return InlineKeyboardMarkup{
695 InlineKeyboard: keyboard,
696 }
697}
698
699// NewCallback creates a new callback message.
700func NewCallback(id, text string) CallbackConfig {
701 return CallbackConfig{
702 CallbackQueryID: id,
703 Text: text,
704 ShowAlert: false,
705 }
706}
707
708// NewCallbackWithAlert creates a new callback message that alerts
709// the user.
710func NewCallbackWithAlert(id, text string) CallbackConfig {
711 return CallbackConfig{
712 CallbackQueryID: id,
713 Text: text,
714 ShowAlert: true,
715 }
716}
717
718// NewInvoice creates a new Invoice request to the user.
719func NewInvoice(chatID int64, title, description, payload, providerToken, startParameter, currency string, prices *[]LabeledPrice) InvoiceConfig {
720 return InvoiceConfig{
721 BaseChat: BaseChat{ChatID: chatID},
722 Title: title,
723 Description: description,
724 Payload: payload,
725 ProviderToken: providerToken,
726 StartParameter: startParameter,
727 Currency: currency,
728 Prices: prices}
729}
730
731// NewSetChatPhotoUpload creates a new chat photo uploader.
732//
733// chatID is where to send it, file is a string path to the file,
734// FileReader, or FileBytes.
735//
736// Note that you must send animated GIFs as a document.
737func NewSetChatPhotoUpload(chatID int64, file interface{}) SetChatPhotoConfig {
738 return SetChatPhotoConfig{
739 BaseFile: BaseFile{
740 BaseChat: BaseChat{ChatID: chatID},
741 File: file,
742 UseExisting: false,
743 },
744 }
745}
746
747// NewSetChatPhotoShare shares an existing photo.
748// You may use this to reshare an existing photo without reuploading it.
749//
750// chatID is where to send it, fileID is the ID of the file
751// already uploaded.
752func NewSetChatPhotoShare(chatID int64, fileID string) SetChatPhotoConfig {
753 return SetChatPhotoConfig{
754 BaseFile: BaseFile{
755 BaseChat: BaseChat{ChatID: chatID},
756 FileID: fileID,
757 UseExisting: true,
758 },
759 }
760}