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