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