all repos — telegram-bot-api @ 03815bf5bd2b255ccbca2e4beefb563a65ce945e

Golang bindings for the Telegram Bot API

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
 21// NewDeleteMessage creates a request to delete a message.
 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// NewAnimationUpload creates a new animation uploader.
205//
206// chatID is where to send it, file is a string path to the file,
207// FileReader, or FileBytes.
208func NewAnimationUpload(chatID int64, file interface{}) AnimationConfig {
209	return AnimationConfig{
210		BaseFile: BaseFile{
211			BaseChat:    BaseChat{ChatID: chatID},
212			File:        file,
213			UseExisting: false,
214		},
215	}
216}
217
218// NewAnimationShare shares an existing animation.
219// You may use this to reshare an existing animation without reuploading it.
220//
221// chatID is where to send it, fileID is the ID of the animation
222// already uploaded.
223func NewAnimationShare(chatID int64, fileID string) AnimationConfig {
224	return AnimationConfig{
225		BaseFile: BaseFile{
226			BaseChat:    BaseChat{ChatID: chatID},
227			FileID:      fileID,
228			UseExisting: true,
229		},
230	}
231}
232
233// NewVideoNoteUpload creates a new video note uploader.
234//
235// chatID is where to send it, file is a string path to the file,
236// FileReader, or FileBytes.
237func NewVideoNoteUpload(chatID int64, length int, file interface{}) VideoNoteConfig {
238	return VideoNoteConfig{
239		BaseFile: BaseFile{
240			BaseChat:    BaseChat{ChatID: chatID},
241			File:        file,
242			UseExisting: false,
243		},
244		Length: length,
245	}
246}
247
248// NewVideoNoteShare shares an existing video.
249// You may use this to reshare an existing video without reuploading it.
250//
251// chatID is where to send it, fileID is the ID of the video
252// already uploaded.
253func NewVideoNoteShare(chatID int64, length int, fileID string) VideoNoteConfig {
254	return VideoNoteConfig{
255		BaseFile: BaseFile{
256			BaseChat:    BaseChat{ChatID: chatID},
257			FileID:      fileID,
258			UseExisting: true,
259		},
260		Length: length,
261	}
262}
263
264// NewVoiceUpload creates a new voice uploader.
265//
266// chatID is where to send it, file is a string path to the file,
267// FileReader, or FileBytes.
268func NewVoiceUpload(chatID int64, file interface{}) VoiceConfig {
269	return VoiceConfig{
270		BaseFile: BaseFile{
271			BaseChat:    BaseChat{ChatID: chatID},
272			File:        file,
273			UseExisting: false,
274		},
275	}
276}
277
278// NewVoiceShare shares an existing voice.
279// You may use this to reshare an existing voice without reuploading it.
280//
281// chatID is where to send it, fileID is the ID of the video
282// already uploaded.
283func NewVoiceShare(chatID int64, fileID string) VoiceConfig {
284	return VoiceConfig{
285		BaseFile: BaseFile{
286			BaseChat:    BaseChat{ChatID: chatID},
287			FileID:      fileID,
288			UseExisting: true,
289		},
290	}
291}
292
293// NewMediaGroup creates a new media group. Files should be an array of
294// two to ten InputMediaPhoto or InputMediaVideo.
295func NewMediaGroup(chatID int64, files []interface{}) MediaGroupConfig {
296	return MediaGroupConfig{
297		ChatID: chatID,
298		Media:  files,
299	}
300}
301
302// NewInputMediaPhoto creates a new InputMediaPhoto.
303func NewInputMediaPhoto(media string) InputMediaPhoto {
304	return InputMediaPhoto{
305		Type:  "photo",
306		Media: media,
307	}
308}
309
310// NewInputMediaVideo creates a new InputMediaVideo.
311func NewInputMediaVideo(media string) InputMediaVideo {
312	return InputMediaVideo{
313		Type:  "video",
314		Media: media,
315	}
316}
317
318// NewContact allows you to send a shared contact.
319func NewContact(chatID int64, phoneNumber, firstName string) ContactConfig {
320	return ContactConfig{
321		BaseChat: BaseChat{
322			ChatID: chatID,
323		},
324		PhoneNumber: phoneNumber,
325		FirstName:   firstName,
326	}
327}
328
329// NewLocation shares your location.
330//
331// chatID is where to send it, latitude and longitude are coordinates.
332func NewLocation(chatID int64, latitude float64, longitude float64) LocationConfig {
333	return LocationConfig{
334		BaseChat: BaseChat{
335			ChatID: chatID,
336		},
337		Latitude:  latitude,
338		Longitude: longitude,
339	}
340}
341
342// NewVenue allows you to send a venue and its location.
343func NewVenue(chatID int64, title, address string, latitude, longitude float64) VenueConfig {
344	return VenueConfig{
345		BaseChat: BaseChat{
346			ChatID: chatID,
347		},
348		Title:     title,
349		Address:   address,
350		Latitude:  latitude,
351		Longitude: longitude,
352	}
353}
354
355// NewChatAction sets a chat action.
356// Actions last for 5 seconds, or until your next action.
357//
358// chatID is where to send it, action should be set via Chat constants.
359func NewChatAction(chatID int64, action string) ChatActionConfig {
360	return ChatActionConfig{
361		BaseChat: BaseChat{ChatID: chatID},
362		Action:   action,
363	}
364}
365
366// NewUserProfilePhotos gets user profile photos.
367//
368// userID is the ID of the user you wish to get profile photos from.
369func NewUserProfilePhotos(userID int) UserProfilePhotosConfig {
370	return UserProfilePhotosConfig{
371		UserID: userID,
372		Offset: 0,
373		Limit:  0,
374	}
375}
376
377// NewUpdate gets updates since the last Offset.
378//
379// offset is the last Update ID to include.
380// You likely want to set this to the last Update ID plus 1.
381func NewUpdate(offset int) UpdateConfig {
382	return UpdateConfig{
383		Offset:  offset,
384		Limit:   0,
385		Timeout: 0,
386	}
387}
388
389// NewWebhook creates a new webhook.
390//
391// link is the url parsable link you wish to get the updates.
392func NewWebhook(link string) WebhookConfig {
393	u, _ := url.Parse(link)
394
395	return WebhookConfig{
396		URL: u,
397	}
398}
399
400// NewWebhookWithCert creates a new webhook with a certificate.
401//
402// link is the url you wish to get webhooks,
403// file contains a string to a file, FileReader, or FileBytes.
404func NewWebhookWithCert(link string, file interface{}) WebhookConfig {
405	u, _ := url.Parse(link)
406
407	return WebhookConfig{
408		URL:         u,
409		Certificate: file,
410	}
411}
412
413// NewInlineQueryResultArticle creates a new inline query article.
414func NewInlineQueryResultArticle(id, title, messageText string) InlineQueryResultArticle {
415	return InlineQueryResultArticle{
416		Type:  "article",
417		ID:    id,
418		Title: title,
419		InputMessageContent: InputTextMessageContent{
420			Text: messageText,
421		},
422	}
423}
424
425// NewInlineQueryResultArticleMarkdown creates a new inline query article with Markdown parsing.
426func NewInlineQueryResultArticleMarkdown(id, title, messageText string) InlineQueryResultArticle {
427	return InlineQueryResultArticle{
428		Type:  "article",
429		ID:    id,
430		Title: title,
431		InputMessageContent: InputTextMessageContent{
432			Text:      messageText,
433			ParseMode: "Markdown",
434		},
435	}
436}
437
438// NewInlineQueryResultArticleHTML creates a new inline query article with HTML parsing.
439func NewInlineQueryResultArticleHTML(id, title, messageText string) InlineQueryResultArticle {
440	return InlineQueryResultArticle{
441		Type:  "article",
442		ID:    id,
443		Title: title,
444		InputMessageContent: InputTextMessageContent{
445			Text:      messageText,
446			ParseMode: "HTML",
447		},
448	}
449}
450
451// NewInlineQueryResultGIF creates a new inline query GIF.
452func NewInlineQueryResultGIF(id, url string) InlineQueryResultGIF {
453	return InlineQueryResultGIF{
454		Type: "gif",
455		ID:   id,
456		URL:  url,
457	}
458}
459
460// NewInlineQueryResultMPEG4GIF creates a new inline query MPEG4 GIF.
461func NewInlineQueryResultMPEG4GIF(id, url string) InlineQueryResultMPEG4GIF {
462	return InlineQueryResultMPEG4GIF{
463		Type: "mpeg4_gif",
464		ID:   id,
465		URL:  url,
466	}
467}
468
469// NewInlineQueryResultPhoto creates a new inline query photo.
470func NewInlineQueryResultPhoto(id, url string) InlineQueryResultPhoto {
471	return InlineQueryResultPhoto{
472		Type: "photo",
473		ID:   id,
474		URL:  url,
475	}
476}
477
478// NewInlineQueryResultPhotoWithThumb creates a new inline query photo.
479func NewInlineQueryResultPhotoWithThumb(id, url, thumb string) InlineQueryResultPhoto {
480	return InlineQueryResultPhoto{
481		Type:     "photo",
482		ID:       id,
483		URL:      url,
484		ThumbURL: thumb,
485	}
486}
487
488// NewInlineQueryResultVideo creates a new inline query video.
489func NewInlineQueryResultVideo(id, url string) InlineQueryResultVideo {
490	return InlineQueryResultVideo{
491		Type: "video",
492		ID:   id,
493		URL:  url,
494	}
495}
496
497// NewInlineQueryResultAudio creates a new inline query audio.
498func NewInlineQueryResultAudio(id, url, title string) InlineQueryResultAudio {
499	return InlineQueryResultAudio{
500		Type:  "audio",
501		ID:    id,
502		URL:   url,
503		Title: title,
504	}
505}
506
507// NewInlineQueryResultVoice creates a new inline query voice.
508func NewInlineQueryResultVoice(id, url, title string) InlineQueryResultVoice {
509	return InlineQueryResultVoice{
510		Type:  "voice",
511		ID:    id,
512		URL:   url,
513		Title: title,
514	}
515}
516
517// NewInlineQueryResultDocument creates a new inline query document.
518func NewInlineQueryResultDocument(id, url, title, mimeType string) InlineQueryResultDocument {
519	return InlineQueryResultDocument{
520		Type:     "document",
521		ID:       id,
522		URL:      url,
523		Title:    title,
524		MimeType: mimeType,
525	}
526}
527
528// NewInlineQueryResultLocation creates a new inline query location.
529func NewInlineQueryResultLocation(id, title string, latitude, longitude float64) InlineQueryResultLocation {
530	return InlineQueryResultLocation{
531		Type:      "location",
532		ID:        id,
533		Title:     title,
534		Latitude:  latitude,
535		Longitude: longitude,
536	}
537}
538
539// NewEditMessageText allows you to edit the text of a message.
540func NewEditMessageText(chatID int64, messageID int, text string) EditMessageTextConfig {
541	return EditMessageTextConfig{
542		BaseEdit: BaseEdit{
543			ChatID:    chatID,
544			MessageID: messageID,
545		},
546		Text: text,
547	}
548}
549
550// NewEditMessageCaption allows you to edit the caption of a message.
551func NewEditMessageCaption(chatID int64, messageID int, caption string) EditMessageCaptionConfig {
552	return EditMessageCaptionConfig{
553		BaseEdit: BaseEdit{
554			ChatID:    chatID,
555			MessageID: messageID,
556		},
557		Caption: caption,
558	}
559}
560
561// NewEditMessageReplyMarkup allows you to edit the inline
562// keyboard markup.
563func NewEditMessageReplyMarkup(chatID int64, messageID int, replyMarkup InlineKeyboardMarkup) EditMessageReplyMarkupConfig {
564	return EditMessageReplyMarkupConfig{
565		BaseEdit: BaseEdit{
566			ChatID:      chatID,
567			MessageID:   messageID,
568			ReplyMarkup: &replyMarkup,
569		},
570	}
571}
572
573// NewHideKeyboard hides the keyboard, with the option for being selective
574// or hiding for everyone.
575func NewHideKeyboard(selective bool) ReplyKeyboardHide {
576	log.Println("NewHideKeyboard is deprecated, please use NewRemoveKeyboard")
577
578	return ReplyKeyboardHide{
579		HideKeyboard: true,
580		Selective:    selective,
581	}
582}
583
584// NewRemoveKeyboard hides the keyboard, with the option for being selective
585// or hiding for everyone.
586func NewRemoveKeyboard(selective bool) ReplyKeyboardRemove {
587	return ReplyKeyboardRemove{
588		RemoveKeyboard: true,
589		Selective:      selective,
590	}
591}
592
593// NewKeyboardButton creates a regular keyboard button.
594func NewKeyboardButton(text string) KeyboardButton {
595	return KeyboardButton{
596		Text: text,
597	}
598}
599
600// NewKeyboardButtonContact creates a keyboard button that requests
601// user contact information upon click.
602func NewKeyboardButtonContact(text string) KeyboardButton {
603	return KeyboardButton{
604		Text:           text,
605		RequestContact: true,
606	}
607}
608
609// NewKeyboardButtonLocation creates a keyboard button that requests
610// user location information upon click.
611func NewKeyboardButtonLocation(text string) KeyboardButton {
612	return KeyboardButton{
613		Text:            text,
614		RequestLocation: true,
615	}
616}
617
618// NewKeyboardButtonRow creates a row of keyboard buttons.
619func NewKeyboardButtonRow(buttons ...KeyboardButton) []KeyboardButton {
620	var row []KeyboardButton
621
622	row = append(row, buttons...)
623
624	return row
625}
626
627// NewReplyKeyboard creates a new regular keyboard with sane defaults.
628func NewReplyKeyboard(rows ...[]KeyboardButton) ReplyKeyboardMarkup {
629	var keyboard [][]KeyboardButton
630
631	keyboard = append(keyboard, rows...)
632
633	return ReplyKeyboardMarkup{
634		ResizeKeyboard: true,
635		Keyboard:       keyboard,
636	}
637}
638
639// NewInlineKeyboardButtonData creates an inline keyboard button with text
640// and data for a callback.
641func NewInlineKeyboardButtonData(text, data string) InlineKeyboardButton {
642	return InlineKeyboardButton{
643		Text:         text,
644		CallbackData: &data,
645	}
646}
647
648// NewInlineKeyboardButtonURL creates an inline keyboard button with text
649// which goes to a URL.
650func NewInlineKeyboardButtonURL(text, url string) InlineKeyboardButton {
651	return InlineKeyboardButton{
652		Text: text,
653		URL:  &url,
654	}
655}
656
657// NewInlineKeyboardButtonSwitch creates an inline keyboard button with
658// text which allows the user to switch to a chat or return to a chat.
659func NewInlineKeyboardButtonSwitch(text, sw string) InlineKeyboardButton {
660	return InlineKeyboardButton{
661		Text:              text,
662		SwitchInlineQuery: &sw,
663	}
664}
665
666// NewInlineKeyboardRow creates an inline keyboard row with buttons.
667func NewInlineKeyboardRow(buttons ...InlineKeyboardButton) []InlineKeyboardButton {
668	var row []InlineKeyboardButton
669
670	row = append(row, buttons...)
671
672	return row
673}
674
675// NewInlineKeyboardMarkup creates a new inline keyboard.
676func NewInlineKeyboardMarkup(rows ...[]InlineKeyboardButton) InlineKeyboardMarkup {
677	var keyboard [][]InlineKeyboardButton
678
679	keyboard = append(keyboard, rows...)
680
681	return InlineKeyboardMarkup{
682		InlineKeyboard: keyboard,
683	}
684}
685
686// NewCallback creates a new callback message.
687func NewCallback(id, text string) CallbackConfig {
688	return CallbackConfig{
689		CallbackQueryID: id,
690		Text:            text,
691		ShowAlert:       false,
692	}
693}
694
695// NewCallbackWithAlert creates a new callback message that alerts
696// the user.
697func NewCallbackWithAlert(id, text string) CallbackConfig {
698	return CallbackConfig{
699		CallbackQueryID: id,
700		Text:            text,
701		ShowAlert:       true,
702	}
703}
704
705// NewInvoice creates a new Invoice request to the user.
706func NewInvoice(chatID int64, title, description, payload, providerToken, startParameter, currency string, prices *[]LabeledPrice) InvoiceConfig {
707	return InvoiceConfig{
708		BaseChat:       BaseChat{ChatID: chatID},
709		Title:          title,
710		Description:    description,
711		Payload:        payload,
712		ProviderToken:  providerToken,
713		StartParameter: startParameter,
714		Currency:       currency,
715		Prices:         prices}
716}
717
718// NewSetChatPhotoUpload creates a new chat photo uploader.
719//
720// chatID is where to send it, file is a string path to the file,
721// FileReader, or FileBytes.
722//
723// Note that you must send animated GIFs as a document.
724func NewSetChatPhotoUpload(chatID int64, file interface{}) SetChatPhotoConfig {
725	return SetChatPhotoConfig{
726		BaseFile: BaseFile{
727			BaseChat:    BaseChat{ChatID: chatID},
728			File:        file,
729			UseExisting: false,
730		},
731	}
732}
733
734// NewSetChatPhotoShare shares an existing photo.
735// You may use this to reshare an existing photo without reuploading it.
736//
737// chatID is where to send it, fileID is the ID of the file
738// already uploaded.
739func NewSetChatPhotoShare(chatID int64, fileID string) SetChatPhotoConfig {
740	return SetChatPhotoConfig{
741		BaseFile: BaseFile{
742			BaseChat:    BaseChat{ChatID: chatID},
743			FileID:      fileID,
744			UseExisting: true,
745		},
746	}
747}
748
749// NewChatTitle allows you to update the title of a chat.
750func NewChatTitle(chatID int64, title string) SetChatTitleConfig {
751	return SetChatTitleConfig{
752		ChatID: chatID,
753		Title:  title,
754	}
755}
756
757// NewChatDescription allows you to update the description of a chat.
758func NewChatDescription(chatID int64, description string) SetChatDescriptionConfig {
759	return SetChatDescriptionConfig{
760		ChatID:      chatID,
761		Description: description,
762	}
763}
764
765// NewChatPhoto allows you to update the photo for a chat.
766func NewChatPhoto(chatID int64, photo interface{}) SetChatPhotoConfig {
767	return SetChatPhotoConfig{
768		BaseFile: BaseFile{
769			BaseChat: BaseChat{
770				ChatID: chatID,
771			},
772			File: photo,
773		},
774	}
775}
776
777// NewDeleteChatPhoto allows you to delete the photo for a chat.
778func NewDeleteChatPhoto(chatID int64, photo interface{}) DeleteChatPhotoConfig {
779	return DeleteChatPhotoConfig{
780		ChatID: chatID,
781	}
782}