all repos — telegram-bot-api @ f90493fac6ee3a4cc3918d3edb4da1f7999acc38

Golang bindings for the Telegram Bot API

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// NewInlineQueryResultMPEG4GIF creates a new inline query MPEG4 GIF.
407func NewInlineQueryResultMPEG4GIF(id, url string) InlineQueryResultMPEG4GIF {
408	return InlineQueryResultMPEG4GIF{
409		Type: "mpeg4_gif",
410		ID:   id,
411		URL:  url,
412	}
413}
414
415// NewInlineQueryResultPhoto creates a new inline query photo.
416func NewInlineQueryResultPhoto(id, url string) InlineQueryResultPhoto {
417	return InlineQueryResultPhoto{
418		Type: "photo",
419		ID:   id,
420		URL:  url,
421	}
422}
423
424// NewInlineQueryResultPhotoWithThumb creates a new inline query photo.
425func NewInlineQueryResultPhotoWithThumb(id, url, thumb string) InlineQueryResultPhoto {
426	return InlineQueryResultPhoto{
427		Type:     "photo",
428		ID:       id,
429		URL:      url,
430		ThumbURL: thumb,
431	}
432}
433
434// NewInlineQueryResultVideo creates a new inline query video.
435func NewInlineQueryResultVideo(id, url string) InlineQueryResultVideo {
436	return InlineQueryResultVideo{
437		Type: "video",
438		ID:   id,
439		URL:  url,
440	}
441}
442
443// NewInlineQueryResultAudio creates a new inline query audio.
444func NewInlineQueryResultAudio(id, url, title string) InlineQueryResultAudio {
445	return InlineQueryResultAudio{
446		Type:  "audio",
447		ID:    id,
448		URL:   url,
449		Title: title,
450	}
451}
452
453// NewInlineQueryResultVoice creates a new inline query voice.
454func NewInlineQueryResultVoice(id, url, title string) InlineQueryResultVoice {
455	return InlineQueryResultVoice{
456		Type:  "voice",
457		ID:    id,
458		URL:   url,
459		Title: title,
460	}
461}
462
463// NewInlineQueryResultDocument creates a new inline query document.
464func NewInlineQueryResultDocument(id, url, title, mimeType string) InlineQueryResultDocument {
465	return InlineQueryResultDocument{
466		Type:     "document",
467		ID:       id,
468		URL:      url,
469		Title:    title,
470		MimeType: mimeType,
471	}
472}
473
474// NewInlineQueryResultLocation creates a new inline query location.
475func NewInlineQueryResultLocation(id, title string, latitude, longitude float64) InlineQueryResultLocation {
476	return InlineQueryResultLocation{
477		Type:      "location",
478		ID:        id,
479		Title:     title,
480		Latitude:  latitude,
481		Longitude: longitude,
482	}
483}
484
485// NewEditMessageText allows you to edit the text of a message.
486func NewEditMessageText(chatID int64, messageID int, text string) EditMessageTextConfig {
487	return EditMessageTextConfig{
488		BaseEdit: BaseEdit{
489			ChatID:    chatID,
490			MessageID: messageID,
491		},
492		Text: text,
493	}
494}
495
496// NewEditMessageCaption allows you to edit the caption of a message.
497func NewEditMessageCaption(chatID int64, messageID int, caption, parseMode string) EditMessageCaptionConfig {
498	return EditMessageCaptionConfig{
499		BaseEdit: BaseEdit{
500			ChatID:    chatID,
501			MessageID: messageID,
502		},
503		Caption:   caption,
504		ParseMode: parseMode,
505	}
506}
507
508// NewEditMessageReplyMarkup allows you to edit the inline
509// keyboard markup.
510func NewEditMessageReplyMarkup(chatID int64, messageID int, replyMarkup InlineKeyboardMarkup) EditMessageReplyMarkupConfig {
511	return EditMessageReplyMarkupConfig{
512		BaseEdit: BaseEdit{
513			ChatID:      chatID,
514			MessageID:   messageID,
515			ReplyMarkup: &replyMarkup,
516		},
517	}
518}
519
520// NewHideKeyboard hides the keyboard, with the option for being selective
521// or hiding for everyone.
522func NewHideKeyboard(selective bool) ReplyKeyboardHide {
523	log.Println("NewHideKeyboard is deprecated, please use NewRemoveKeyboard")
524
525	return ReplyKeyboardHide{
526		HideKeyboard: true,
527		Selective:    selective,
528	}
529}
530
531// NewRemoveKeyboard hides the keyboard, with the option for being selective
532// or hiding for everyone.
533func NewRemoveKeyboard(selective bool) ReplyKeyboardRemove {
534	return ReplyKeyboardRemove{
535		RemoveKeyboard: true,
536		Selective:      selective,
537	}
538}
539
540// NewKeyboardButton creates a regular keyboard button.
541func NewKeyboardButton(text string) KeyboardButton {
542	return KeyboardButton{
543		Text: text,
544	}
545}
546
547// NewKeyboardButtonContact creates a keyboard button that requests
548// user contact information upon click.
549func NewKeyboardButtonContact(text string) KeyboardButton {
550	return KeyboardButton{
551		Text:           text,
552		RequestContact: true,
553	}
554}
555
556// NewKeyboardButtonLocation creates a keyboard button that requests
557// user location information upon click.
558func NewKeyboardButtonLocation(text string) KeyboardButton {
559	return KeyboardButton{
560		Text:            text,
561		RequestLocation: true,
562	}
563}
564
565// NewKeyboardButtonRow creates a row of keyboard buttons.
566func NewKeyboardButtonRow(buttons ...KeyboardButton) []KeyboardButton {
567	var row []KeyboardButton
568
569	row = append(row, buttons...)
570
571	return row
572}
573
574// NewReplyKeyboard creates a new regular keyboard with sane defaults.
575func NewReplyKeyboard(rows ...[]KeyboardButton) ReplyKeyboardMarkup {
576	var keyboard [][]KeyboardButton
577
578	keyboard = append(keyboard, rows...)
579
580	return ReplyKeyboardMarkup{
581		ResizeKeyboard: true,
582		Keyboard:       keyboard,
583	}
584}
585
586// NewInlineKeyboardButtonData creates an inline keyboard button with text
587// and data for a callback.
588func NewInlineKeyboardButtonData(text, data string) InlineKeyboardButton {
589	return InlineKeyboardButton{
590		Text:         text,
591		CallbackData: &data,
592	}
593}
594
595// NewInlineKeyboardButtonURL creates an inline keyboard button with text
596// which goes to a URL.
597func NewInlineKeyboardButtonURL(text, url string) InlineKeyboardButton {
598	return InlineKeyboardButton{
599		Text: text,
600		URL:  &url,
601	}
602}
603
604// NewInlineKeyboardButtonSwitch creates an inline keyboard button with
605// text which allows the user to switch to a chat or return to a chat.
606func NewInlineKeyboardButtonSwitch(text, sw string) InlineKeyboardButton {
607	return InlineKeyboardButton{
608		Text:              text,
609		SwitchInlineQuery: &sw,
610	}
611}
612
613// NewInlineKeyboardRow creates an inline keyboard row with buttons.
614func NewInlineKeyboardRow(buttons ...InlineKeyboardButton) []InlineKeyboardButton {
615	var row []InlineKeyboardButton
616
617	row = append(row, buttons...)
618
619	return row
620}
621
622// NewInlineKeyboardMarkup creates a new inline keyboard.
623func NewInlineKeyboardMarkup(rows ...[]InlineKeyboardButton) InlineKeyboardMarkup {
624	var keyboard [][]InlineKeyboardButton
625
626	keyboard = append(keyboard, rows...)
627
628	return InlineKeyboardMarkup{
629		InlineKeyboard: keyboard,
630	}
631}
632
633// NewCallback creates a new callback message.
634func NewCallback(id, text string) CallbackConfig {
635	return CallbackConfig{
636		CallbackQueryID: id,
637		Text:            text,
638		ShowAlert:       false,
639	}
640}
641
642// NewCallbackWithAlert creates a new callback message that alerts
643// the user.
644func NewCallbackWithAlert(id, text string) CallbackConfig {
645	return CallbackConfig{
646		CallbackQueryID: id,
647		Text:            text,
648		ShowAlert:       true,
649	}
650}
651
652// NewInvoice creates a new Invoice request to the user.
653func NewInvoice(chatID int64, title, description, payload, providerToken, startParameter, currency string, prices *[]LabeledPrice) InvoiceConfig {
654	return InvoiceConfig{
655		BaseChat:       BaseChat{ChatID: chatID},
656		Title:          title,
657		Description:    description,
658		Payload:        payload,
659		ProviderToken:  providerToken,
660		StartParameter: startParameter,
661		Currency:       currency,
662		Prices:         prices}
663}
664
665// NewSetChatPhotoUpload creates a new chat photo uploader.
666//
667// chatID is where to send it, file is a string path to the file,
668// FileReader, or FileBytes.
669//
670// Note that you must send animated GIFs as a document.
671func NewSetChatPhotoUpload(chatID int64, file interface{}) SetChatPhotoConfig {
672	return SetChatPhotoConfig{
673		BaseFile: BaseFile{
674			BaseChat:    BaseChat{ChatID: chatID},
675			File:        file,
676			UseExisting: false,
677		},
678	}
679}
680
681// NewSetChatPhotoShare shares an existing photo.
682// You may use this to reshare an existing photo without reuploading it.
683//
684// chatID is where to send it, fileID is the ID of the file
685// already uploaded.
686func NewSetChatPhotoShare(chatID int64, fileID string) SetChatPhotoConfig {
687	return SetChatPhotoConfig{
688		BaseFile: BaseFile{
689			BaseChat:    BaseChat{ChatID: chatID},
690			FileID:      fileID,
691			UseExisting: true,
692		},
693	}
694}