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// NewInlineQueryResultVideo creates a new inline query video.
385func NewInlineQueryResultVideo(id, url string) InlineQueryResultVideo {
386 return InlineQueryResultVideo{
387 Type: "video",
388 ID: id,
389 URL: url,
390 }
391}
392
393// NewInlineQueryResultAudio creates a new inline query audio.
394func NewInlineQueryResultAudio(id, url, title string) InlineQueryResultAudio {
395 return InlineQueryResultAudio{
396 Type: "audio",
397 ID: id,
398 URL: url,
399 Title: title,
400 }
401}
402
403// NewInlineQueryResultVoice creates a new inline query voice.
404func NewInlineQueryResultVoice(id, url, title string) InlineQueryResultVoice {
405 return InlineQueryResultVoice{
406 Type: "voice",
407 ID: id,
408 URL: url,
409 Title: title,
410 }
411}
412
413// NewInlineQueryResultDocument creates a new inline query document.
414func NewInlineQueryResultDocument(id, url, title, mimeType string) InlineQueryResultDocument {
415 return InlineQueryResultDocument{
416 Type: "document",
417 ID: id,
418 URL: url,
419 Title: title,
420 MimeType: mimeType,
421 }
422}
423
424// NewInlineQueryResultLocation creates a new inline query location.
425func NewInlineQueryResultLocation(id, title string, latitude, longitude float64) InlineQueryResultLocation {
426 return InlineQueryResultLocation{
427 Type: "location",
428 ID: id,
429 Title: title,
430 Latitude: latitude,
431 Longitude: longitude,
432 }
433}
434
435// NewEditMessageText allows you to edit the text of a message.
436func NewEditMessageText(chatID int64, messageID int, text string) EditMessageTextConfig {
437 return EditMessageTextConfig{
438 BaseEdit: BaseEdit{
439 ChatID: chatID,
440 MessageID: messageID,
441 },
442 Text: text,
443 }
444}
445
446// NewEditMessageCaption allows you to edit the caption of a message.
447func NewEditMessageCaption(chatID int64, messageID int, caption string) EditMessageCaptionConfig {
448 return EditMessageCaptionConfig{
449 BaseEdit: BaseEdit{
450 ChatID: chatID,
451 MessageID: messageID,
452 },
453 Caption: caption,
454 }
455}
456
457// NewEditMessageReplyMarkup allows you to edit the inline
458// keyboard markup.
459func NewEditMessageReplyMarkup(chatID int64, messageID int, replyMarkup InlineKeyboardMarkup) EditMessageReplyMarkupConfig {
460 return EditMessageReplyMarkupConfig{
461 BaseEdit: BaseEdit{
462 ChatID: chatID,
463 MessageID: messageID,
464 ReplyMarkup: &replyMarkup,
465 },
466 }
467}
468
469// NewHideKeyboard hides the keyboard, with the option for being selective
470// or hiding for everyone.
471func NewHideKeyboard(selective bool) ReplyKeyboardHide {
472 return ReplyKeyboardHide{
473 HideKeyboard: true,
474 Selective: selective,
475 }
476}
477
478// NewKeyboardButton creates a regular keyboard button.
479func NewKeyboardButton(text string) KeyboardButton {
480 return KeyboardButton{
481 Text: text,
482 }
483}
484
485// NewKeyboardButtonContact creates a keyboard button that requests
486// user contact information upon click.
487func NewKeyboardButtonContact(text string) KeyboardButton {
488 return KeyboardButton{
489 Text: text,
490 RequestContact: true,
491 }
492}
493
494// NewKeyboardButtonLocation creates a keyboard button that requests
495// user location information upon click.
496func NewKeyboardButtonLocation(text string) KeyboardButton {
497 return KeyboardButton{
498 Text: text,
499 RequestLocation: true,
500 }
501}
502
503// NewKeyboardButtonRow creates a row of keyboard buttons.
504func NewKeyboardButtonRow(buttons ...KeyboardButton) []KeyboardButton {
505 var row []KeyboardButton
506
507 row = append(row, buttons...)
508
509 return row
510}
511
512// NewReplyKeyboard creates a new regular keyboard with sane defaults.
513func NewReplyKeyboard(rows ...[]KeyboardButton) ReplyKeyboardMarkup {
514 var keyboard [][]KeyboardButton
515
516 keyboard = append(keyboard, rows...)
517
518 return ReplyKeyboardMarkup{
519 ResizeKeyboard: true,
520 Keyboard: keyboard,
521 }
522}
523
524// NewInlineKeyboardButtonData creates an inline keyboard button with text
525// and data for a callback.
526func NewInlineKeyboardButtonData(text, data string) InlineKeyboardButton {
527 return InlineKeyboardButton{
528 Text: text,
529 CallbackData: &data,
530 }
531}
532
533// NewInlineKeyboardButtonURL creates an inline keyboard button with text
534// which goes to a URL.
535func NewInlineKeyboardButtonURL(text, url string) InlineKeyboardButton {
536 return InlineKeyboardButton{
537 Text: text,
538 URL: &url,
539 }
540}
541
542// NewInlineKeyboardButtonSwitch creates an inline keyboard button with
543// text which allows the user to switch to a chat or return to a chat.
544func NewInlineKeyboardButtonSwitch(text, sw string) InlineKeyboardButton {
545 return InlineKeyboardButton{
546 Text: text,
547 SwitchInlineQuery: &sw,
548 }
549}
550
551// NewInlineKeyboardRow creates an inline keyboard row with buttons.
552func NewInlineKeyboardRow(buttons ...InlineKeyboardButton) []InlineKeyboardButton {
553 var row []InlineKeyboardButton
554
555 row = append(row, buttons...)
556
557 return row
558}
559
560// NewInlineKeyboardMarkup creates a new inline keyboard.
561func NewInlineKeyboardMarkup(rows ...[]InlineKeyboardButton) InlineKeyboardMarkup {
562 var keyboard [][]InlineKeyboardButton
563
564 keyboard = append(keyboard, rows...)
565
566 return InlineKeyboardMarkup{
567 InlineKeyboard: keyboard,
568 }
569}
570
571// NewCallback creates a new callback message.
572func NewCallback(id, text string) CallbackConfig {
573 return CallbackConfig{
574 CallbackQueryID: id,
575 Text: text,
576 ShowAlert: false,
577 }
578}
579
580// NewCallbackWithAlert creates a new callback message that alerts
581// the user.
582func NewCallbackWithAlert(id, text string) CallbackConfig {
583 return CallbackConfig{
584 CallbackQueryID: id,
585 Text: text,
586 ShowAlert: true,
587 }
588}