all repos — telegram-bot-api @ a1204e7ea14d507197409f0d784f66b853b175aa

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