all repos — telegram-bot-api @ 3ed6b6ac1677d7c6b8fd1718a0212632d522a028

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// NewInlineQueryResultGIF creates a new inline query GIF.
332func NewInlineQueryResultGIF(id, url string) InlineQueryResultGIF {
333	return InlineQueryResultGIF{
334		Type: "gif",
335		ID:   id,
336		URL:  url,
337	}
338}
339
340// NewInlineQueryResultMPEG4GIF creates a new inline query MPEG4 GIF.
341func NewInlineQueryResultMPEG4GIF(id, url string) InlineQueryResultMPEG4GIF {
342	return InlineQueryResultMPEG4GIF{
343		Type: "mpeg4_gif",
344		ID:   id,
345		URL:  url,
346	}
347}
348
349// NewInlineQueryResultPhoto creates a new inline query photo.
350func NewInlineQueryResultPhoto(id, url string) InlineQueryResultPhoto {
351	return InlineQueryResultPhoto{
352		Type: "photo",
353		ID:   id,
354		URL:  url,
355	}
356}
357
358// NewInlineQueryResultVideo creates a new inline query video.
359func NewInlineQueryResultVideo(id, url string) InlineQueryResultVideo {
360	return InlineQueryResultVideo{
361		Type: "video",
362		ID:   id,
363		URL:  url,
364	}
365}
366
367// NewInlineQueryResultAudio creates a new inline query audio.
368func NewInlineQueryResultAudio(id, url, title string) InlineQueryResultAudio {
369	return InlineQueryResultAudio{
370		Type:  "audio",
371		ID:    id,
372		URL:   url,
373		Title: title,
374	}
375}
376
377// NewInlineQueryResultVoice creates a new inline query voice.
378func NewInlineQueryResultVoice(id, url, title string) InlineQueryResultVoice {
379	return InlineQueryResultVoice{
380		Type:  "voice",
381		ID:    id,
382		URL:   url,
383		Title: title,
384	}
385}
386
387// NewInlineQueryResultDocument creates a new inline query document.
388func NewInlineQueryResultDocument(id, url, title, mimeType string) InlineQueryResultDocument {
389	return InlineQueryResultDocument{
390		Type:     "document",
391		ID:       id,
392		URL:      url,
393		Title:    title,
394		MimeType: mimeType,
395	}
396}
397
398// NewInlineQueryResultLocation creates a new inline query location.
399func NewInlineQueryResultLocation(id, title string, latitude, longitude float64) InlineQueryResultLocation {
400	return InlineQueryResultLocation{
401		Type:      "location",
402		ID:        id,
403		Title:     title,
404		Latitude:  latitude,
405		Longitude: longitude,
406	}
407}
408
409// NewEditMessageText allows you to edit the text of a message.
410func NewEditMessageText(chatID int64, messageID int, text string) EditMessageTextConfig {
411	return EditMessageTextConfig{
412		BaseEdit: BaseEdit{
413			ChatID:    chatID,
414			MessageID: messageID,
415		},
416		Text: text,
417	}
418}
419
420// NewEditMessageCaption allows you to edit the caption of a message.
421func NewEditMessageCaption(chatID int64, messageID int, caption string) EditMessageCaptionConfig {
422	return EditMessageCaptionConfig{
423		BaseEdit: BaseEdit{
424			ChatID:    chatID,
425			MessageID: messageID,
426		},
427		Caption: caption,
428	}
429}
430
431// NewEditMessageReplyMarkup allows you to edit the inline
432// keyboard markup.
433func NewEditMessageReplyMarkup(chatID int64, messageID int, replyMarkup InlineKeyboardMarkup) EditMessageReplyMarkupConfig {
434	return EditMessageReplyMarkupConfig{
435		BaseEdit: BaseEdit{
436			ChatID:      chatID,
437			MessageID:   messageID,
438			ReplyMarkup: &replyMarkup,
439		},
440	}
441}
442
443// NewHideKeyboard hides the keyboard, with the option for being selective
444// or hiding for everyone.
445func NewHideKeyboard(selective bool) ReplyKeyboardHide {
446	return ReplyKeyboardHide{
447		HideKeyboard: true,
448		Selective:    selective,
449	}
450}
451
452// NewKeyboardButton creates a regular keyboard button.
453func NewKeyboardButton(text string) KeyboardButton {
454	return KeyboardButton{
455		Text: text,
456	}
457}
458
459// NewKeyboardButtonContact creates a keyboard button that requests
460// user contact information upon click.
461func NewKeyboardButtonContact(text string) KeyboardButton {
462	return KeyboardButton{
463		Text:           text,
464		RequestContact: true,
465	}
466}
467
468// NewKeyboardButtonLocation creates a keyboard button that requests
469// user location information upon click.
470func NewKeyboardButtonLocation(text string) KeyboardButton {
471	return KeyboardButton{
472		Text:            text,
473		RequestLocation: true,
474	}
475}
476
477// NewKeyboardButtonRow creates a row of keyboard buttons.
478func NewKeyboardButtonRow(buttons ...KeyboardButton) []KeyboardButton {
479	var row []KeyboardButton
480
481	row = append(row, buttons...)
482
483	return row
484}
485
486// NewReplyKeyboard creates a new regular keyboard with sane defaults.
487func NewReplyKeyboard(rows ...[]KeyboardButton) ReplyKeyboardMarkup {
488	var keyboard [][]KeyboardButton
489
490	keyboard = append(keyboard, rows...)
491
492	return ReplyKeyboardMarkup{
493		ResizeKeyboard: true,
494		Keyboard:       keyboard,
495	}
496}
497
498// NewInlineKeyboardButtonData creates an inline keyboard button with text
499// and data for a callback.
500func NewInlineKeyboardButtonData(text, data string) InlineKeyboardButton {
501	return InlineKeyboardButton{
502		Text:         text,
503		CallbackData: &data,
504	}
505}
506
507// NewInlineKeyboardButtonURL creates an inline keyboard button with text
508// which goes to a URL.
509func NewInlineKeyboardButtonURL(text, url string) InlineKeyboardButton {
510	return InlineKeyboardButton{
511		Text: text,
512		URL:  &url,
513	}
514}
515
516// NewInlineKeyboardButtonSwitch creates an inline keyboard button with
517// text which allows the user to switch to a chat or return to a chat.
518func NewInlineKeyboardButtonSwitch(text, sw string) InlineKeyboardButton {
519	return InlineKeyboardButton{
520		Text:              text,
521		SwitchInlineQuery: &sw,
522	}
523}
524
525// NewInlineKeyboardRow creates an inline keyboard row with buttons.
526func NewInlineKeyboardRow(buttons ...InlineKeyboardButton) []InlineKeyboardButton {
527	var row []InlineKeyboardButton
528
529	row = append(row, buttons...)
530
531	return row
532}
533
534// NewInlineKeyboardMarkup creates a new inline keyboard.
535func NewInlineKeyboardMarkup(rows ...[]InlineKeyboardButton) InlineKeyboardMarkup {
536	var keyboard [][]InlineKeyboardButton
537
538	keyboard = append(keyboard, rows...)
539
540	return InlineKeyboardMarkup{
541		InlineKeyboard: keyboard,
542	}
543}
544
545// NewCallback creates a new callback message.
546func NewCallback(id, text string) CallbackConfig {
547	return CallbackConfig{
548		CallbackQueryID: id,
549		Text:            text,
550		ShowAlert:       false,
551	}
552}
553
554// NewCallbackWithAlert creates a new callback message that alerts
555// the user.
556func NewCallbackWithAlert(id, text string) CallbackConfig {
557	return CallbackConfig{
558		CallbackQueryID: id,
559		Text:            text,
560		ShowAlert:       true,
561	}
562}