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