all repos — telegram-bot-api @ 4c16a90966d12d963e19e4df99e7744c191d0e79

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 string) EditMessageCaptionConfig {
498	return EditMessageCaptionConfig{
499		BaseEdit: BaseEdit{
500			ChatID:    chatID,
501			MessageID: messageID,
502		},
503		Caption: caption,
504	}
505}
506
507// NewEditMessageReplyMarkup allows you to edit the inline
508// keyboard markup.
509func NewEditMessageReplyMarkup(chatID int64, messageID int, replyMarkup InlineKeyboardMarkup) EditMessageReplyMarkupConfig {
510	return EditMessageReplyMarkupConfig{
511		BaseEdit: BaseEdit{
512			ChatID:      chatID,
513			MessageID:   messageID,
514			ReplyMarkup: &replyMarkup,
515		},
516	}
517}
518
519// NewHideKeyboard hides the keyboard, with the option for being selective
520// or hiding for everyone.
521func NewHideKeyboard(selective bool) ReplyKeyboardHide {
522	log.Println("NewHideKeyboard is deprecated, please use NewRemoveKeyboard")
523
524	return ReplyKeyboardHide{
525		HideKeyboard: true,
526		Selective:    selective,
527	}
528}
529
530// NewRemoveKeyboard hides the keyboard, with the option for being selective
531// or hiding for everyone.
532func NewRemoveKeyboard(selective bool) ReplyKeyboardRemove {
533	return ReplyKeyboardRemove{
534		RemoveKeyboard: true,
535		Selective:      selective,
536	}
537}
538
539// NewKeyboardButton creates a regular keyboard button.
540func NewKeyboardButton(text string) KeyboardButton {
541	return KeyboardButton{
542		Text: text,
543	}
544}
545
546// NewKeyboardButtonContact creates a keyboard button that requests
547// user contact information upon click.
548func NewKeyboardButtonContact(text string) KeyboardButton {
549	return KeyboardButton{
550		Text:           text,
551		RequestContact: true,
552	}
553}
554
555// NewKeyboardButtonLocation creates a keyboard button that requests
556// user location information upon click.
557func NewKeyboardButtonLocation(text string) KeyboardButton {
558	return KeyboardButton{
559		Text:            text,
560		RequestLocation: true,
561	}
562}
563
564// NewKeyboardButtonRow creates a row of keyboard buttons.
565func NewKeyboardButtonRow(buttons ...KeyboardButton) []KeyboardButton {
566	var row []KeyboardButton
567
568	row = append(row, buttons...)
569
570	return row
571}
572
573// NewReplyKeyboard creates a new regular keyboard with sane defaults.
574func NewReplyKeyboard(rows ...[]KeyboardButton) ReplyKeyboardMarkup {
575	var keyboard [][]KeyboardButton
576
577	keyboard = append(keyboard, rows...)
578
579	return ReplyKeyboardMarkup{
580		ResizeKeyboard: true,
581		Keyboard:       keyboard,
582	}
583}
584
585// NewInlineKeyboardButtonData creates an inline keyboard button with text
586// and data for a callback.
587func NewInlineKeyboardButtonData(text, data string) InlineKeyboardButton {
588	return InlineKeyboardButton{
589		Text:         text,
590		CallbackData: &data,
591	}
592}
593
594// NewInlineKeyboardButtonURL creates an inline keyboard button with text
595// which goes to a URL.
596func NewInlineKeyboardButtonURL(text, url string) InlineKeyboardButton {
597	return InlineKeyboardButton{
598		Text: text,
599		URL:  &url,
600	}
601}
602
603// NewInlineKeyboardButtonSwitch creates an inline keyboard button with
604// text which allows the user to switch to a chat or return to a chat.
605func NewInlineKeyboardButtonSwitch(text, sw string) InlineKeyboardButton {
606	return InlineKeyboardButton{
607		Text:              text,
608		SwitchInlineQuery: &sw,
609	}
610}
611
612// NewInlineKeyboardRow creates an inline keyboard row with buttons.
613func NewInlineKeyboardRow(buttons ...InlineKeyboardButton) []InlineKeyboardButton {
614	var row []InlineKeyboardButton
615
616	row = append(row, buttons...)
617
618	return row
619}
620
621// NewInlineKeyboardMarkup creates a new inline keyboard.
622func NewInlineKeyboardMarkup(rows ...[]InlineKeyboardButton) InlineKeyboardMarkup {
623	var keyboard [][]InlineKeyboardButton
624
625	keyboard = append(keyboard, rows...)
626
627	return InlineKeyboardMarkup{
628		InlineKeyboard: keyboard,
629	}
630}
631
632// NewCallback creates a new callback message.
633func NewCallback(id, text string) CallbackConfig {
634	return CallbackConfig{
635		CallbackQueryID: id,
636		Text:            text,
637		ShowAlert:       false,
638	}
639}
640
641// NewCallbackWithAlert creates a new callback message that alerts
642// the user.
643func NewCallbackWithAlert(id, text string) CallbackConfig {
644	return CallbackConfig{
645		CallbackQueryID: id,
646		Text:            text,
647		ShowAlert:       true,
648	}
649}
650
651// NewInvoice creates a new Invoice request to the user.
652func NewInvoice(chatID int64, title, description, payload, providerToken, startParameter, currency string, prices *[]LabeledPrice) InvoiceConfig {
653	return InvoiceConfig{
654		BaseChat:       BaseChat{ChatID: chatID},
655		Title:          title,
656		Description:    description,
657		Payload:        payload,
658		ProviderToken:  providerToken,
659		StartParameter: startParameter,
660		Currency:       currency,
661		Prices:         prices}
662}
663
664// NewSetChatPhotoUpload creates a new chat photo uploader.
665//
666// chatID is where to send it, file is a string path to the file,
667// FileReader, or FileBytes.
668//
669// Note that you must send animated GIFs as a document.
670func NewSetChatPhotoUpload(chatID int64, file interface{}) SetChatPhotoConfig {
671	return SetChatPhotoConfig{
672		BaseFile: BaseFile{
673			BaseChat:    BaseChat{ChatID: chatID},
674			File:        file,
675			UseExisting: false,
676		},
677	}
678}
679
680// NewSetChatPhotoShare shares an existing photo.
681// You may use this to reshare an existing photo without reuploading it.
682//
683// chatID is where to send it, fileID is the ID of the file
684// already uploaded.
685func NewSetChatPhotoShare(chatID int64, fileID string) SetChatPhotoConfig {
686	return SetChatPhotoConfig{
687		BaseFile: BaseFile{
688			BaseChat:    BaseChat{ChatID: chatID},
689			FileID:      fileID,
690			UseExisting: true,
691		},
692	}
693}