all repos — telegram-bot-api @ 369364b8c6c2bc563d8c3cef8fa47b40249f204b

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