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