all repos — telegram-bot-api @ 65947daaab88cefc8f87b936cb1fafb60d09115d

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