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