all repos — telegram-bot-api @ f57724f783b167bf0352f706bbaf9d1642572361

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