all repos — telegram-bot-api @ 7ff5871e2875951ebf0fbdea7c174337c9600136

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