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 int, 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// NewForward creates a new forward.
22//
23// chatID is where to send it, fromChatID is the source chat,
24// and messageID is the ID of the original message.
25func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig {
26 return ForwardConfig{
27 BaseChat: BaseChat{ChatID: chatID},
28 FromChatID: fromChatID,
29 MessageID: messageID,
30 }
31}
32
33// NewPhotoUpload creates a new photo uploader.
34//
35// chatID is where to send it, file is a string path to the file,
36// FileReader, or FileBytes.
37//
38// Note that you must send animated GIFs as a document.
39func NewPhotoUpload(chatID int, file interface{}) PhotoConfig {
40 return PhotoConfig{
41 BaseFile: BaseFile{
42 BaseChat: BaseChat{ChatID: chatID},
43 File: file,
44 UseExisting: false,
45 },
46 }
47}
48
49// NewPhotoShare shares an existing photo.
50// You may use this to reshare an existing photo without reuploading it.
51//
52// chatID is where to send it, fileID is the ID of the file
53// already uploaded.
54func NewPhotoShare(chatID int, fileID string) PhotoConfig {
55 return PhotoConfig{
56 BaseFile: BaseFile{
57 BaseChat: BaseChat{ChatID: chatID},
58 FileID: fileID,
59 UseExisting: true,
60 },
61 }
62}
63
64// NewAudioUpload creates a new audio uploader.
65//
66// chatID is where to send it, file is a string path to the file,
67// FileReader, or FileBytes.
68func NewAudioUpload(chatID int, file interface{}) AudioConfig {
69 return AudioConfig{
70 BaseFile: BaseFile{
71 BaseChat: BaseChat{ChatID: chatID},
72 File: file,
73 UseExisting: false,
74 },
75 }
76}
77
78// NewAudioShare shares an existing audio file.
79// You may use this to reshare an existing audio file without
80// reuploading it.
81//
82// chatID is where to send it, fileID is the ID of the audio
83// already uploaded.
84func NewAudioShare(chatID int, fileID string) AudioConfig {
85 return AudioConfig{
86 BaseFile: BaseFile{
87 BaseChat: BaseChat{ChatID: chatID},
88 FileID: fileID,
89 UseExisting: true,
90 },
91 }
92}
93
94// NewDocumentUpload creates a new document uploader.
95//
96// chatID is where to send it, file is a string path to the file,
97// FileReader, or FileBytes.
98func NewDocumentUpload(chatID int, file interface{}) DocumentConfig {
99 return DocumentConfig{
100 BaseFile: BaseFile{
101 BaseChat: BaseChat{ChatID: chatID},
102 File: file,
103 UseExisting: false,
104 },
105 }
106}
107
108// NewDocumentShare shares an existing document.
109// You may use this to reshare an existing document without
110// reuploading it.
111//
112// chatID is where to send it, fileID is the ID of the document
113// already uploaded.
114func NewDocumentShare(chatID int, fileID string) DocumentConfig {
115 return DocumentConfig{
116 BaseFile: BaseFile{
117 BaseChat: BaseChat{ChatID: chatID},
118 FileID: fileID,
119 UseExisting: true,
120 },
121 }
122}
123
124// NewStickerUpload creates a new sticker uploader.
125//
126// chatID is where to send it, file is a string path to the file,
127// FileReader, or FileBytes.
128func NewStickerUpload(chatID int, file interface{}) StickerConfig {
129 return StickerConfig{
130 BaseFile: BaseFile{
131 BaseChat: BaseChat{ChatID: chatID},
132 File: file,
133 UseExisting: false,
134 },
135 }
136}
137
138// NewStickerShare shares an existing sticker.
139// You may use this to reshare an existing sticker without
140// reuploading it.
141//
142// chatID is where to send it, fileID is the ID of the sticker
143// already uploaded.
144func NewStickerShare(chatID int, fileID string) StickerConfig {
145 return StickerConfig{
146 BaseFile: BaseFile{
147 BaseChat: BaseChat{ChatID: chatID},
148 FileID: fileID,
149 UseExisting: true,
150 },
151 }
152}
153
154// NewVideoUpload creates a new video uploader.
155//
156// chatID is where to send it, file is a string path to the file,
157// FileReader, or FileBytes.
158func NewVideoUpload(chatID int, file interface{}) VideoConfig {
159 return VideoConfig{
160 BaseFile: BaseFile{
161 BaseChat: BaseChat{ChatID: chatID},
162 File: file,
163 UseExisting: false,
164 },
165 }
166}
167
168// NewVideoShare shares an existing video.
169// You may use this to reshare an existing video without reuploading it.
170//
171// chatID is where to send it, fileID is the ID of the video
172// already uploaded.
173func NewVideoShare(chatID int, fileID string) VideoConfig {
174 return VideoConfig{
175 BaseFile: BaseFile{
176 BaseChat: BaseChat{ChatID: chatID},
177 FileID: fileID,
178 UseExisting: true,
179 },
180 }
181}
182
183// NewVoiceUpload creates a new voice uploader.
184//
185// chatID is where to send it, file is a string path to the file,
186// FileReader, or FileBytes.
187func NewVoiceUpload(chatID int, file interface{}) VoiceConfig {
188 return VoiceConfig{
189 BaseFile: BaseFile{
190 BaseChat: BaseChat{ChatID: chatID},
191 File: file,
192 UseExisting: false,
193 },
194 }
195}
196
197// NewVoiceShare shares an existing voice.
198// You may use this to reshare an existing voice without reuploading it.
199//
200// chatID is where to send it, fileID is the ID of the video
201// already uploaded.
202func NewVoiceShare(chatID int, fileID string) VoiceConfig {
203 return VoiceConfig{
204 BaseFile: BaseFile{
205 BaseChat: BaseChat{ChatID: chatID},
206 FileID: fileID,
207 UseExisting: true,
208 },
209 }
210}
211
212// NewLocation shares your location.
213//
214// chatID is where to send it, latitude and longitude are coordinates.
215func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig {
216 return LocationConfig{
217 BaseChat: BaseChat{
218 ChatID: chatID,
219 ReplyToMessageID: 0,
220 ReplyMarkup: nil,
221 },
222 Latitude: latitude,
223 Longitude: longitude,
224 }
225}
226
227// NewChatAction sets a chat action.
228// Actions last for 5 seconds, or until your next action.
229//
230// chatID is where to send it, action should be set via Chat constants.
231func NewChatAction(chatID int, action string) ChatActionConfig {
232 return ChatActionConfig{
233 BaseChat: BaseChat{ChatID: chatID},
234 Action: action,
235 }
236}
237
238// NewUserProfilePhotos gets user profile photos.
239//
240// userID is the ID of the user you wish to get profile photos from.
241func NewUserProfilePhotos(userID int) UserProfilePhotosConfig {
242 return UserProfilePhotosConfig{
243 UserID: userID,
244 Offset: 0,
245 Limit: 0,
246 }
247}
248
249// NewUpdate gets updates since the last Offset.
250//
251// offset is the last Update ID to include.
252// You likely want to set this to the last Update ID plus 1.
253func NewUpdate(offset int) UpdateConfig {
254 return UpdateConfig{
255 Offset: offset,
256 Limit: 0,
257 Timeout: 0,
258 }
259}
260
261// NewWebhook creates a new webhook.
262//
263// link is the url parsable link you wish to get the updates.
264func NewWebhook(link string) WebhookConfig {
265 u, _ := url.Parse(link)
266
267 return WebhookConfig{
268 URL: u,
269 }
270}
271
272// NewWebhookWithCert creates a new webhook with a certificate.
273//
274// link is the url you wish to get webhooks,
275// file contains a string to a file, FileReader, or FileBytes.
276func NewWebhookWithCert(link string, file interface{}) WebhookConfig {
277 u, _ := url.Parse(link)
278
279 return WebhookConfig{
280 URL: u,
281 Certificate: file,
282 }
283}
284
285// NewInlineQueryResultArticle creates a new inline query article.
286func NewInlineQueryResultArticle(id, title, messageText string) InlineQueryResultArticle {
287 return InlineQueryResultArticle{
288 Type: "article",
289 ID: id,
290 Title: title,
291 MessageText: messageText,
292 }
293}
294
295// NewInlineQueryResultGIF creates a new inline query GIF.
296func NewInlineQueryResultGIF(id, url string) InlineQueryResultGIF {
297 return InlineQueryResultGIF{
298 Type: "gif",
299 ID: id,
300 URL: url,
301 }
302}
303
304// NewInlineQueryResultMPEG4GIF creates a new inline query MPEG4 GIF.
305func NewInlineQueryResultMPEG4GIF(id, url string) InlineQueryResultMPEG4GIF {
306 return InlineQueryResultMPEG4GIF{
307 Type: "mpeg4_gif",
308 ID: id,
309 URL: url,
310 }
311}
312
313// NewInlineQueryResultPhoto creates a new inline query photo.
314func NewInlineQueryResultPhoto(id, url string) InlineQueryResultPhoto {
315 return InlineQueryResultPhoto{
316 Type: "photo",
317 ID: id,
318 URL: url,
319 }
320}
321
322// NewInlineQueryResultVideo creates a new inline query video.
323func NewInlineQueryResultVideo(id, url string) InlineQueryResultVideo {
324 return InlineQueryResultVideo{
325 Type: "video",
326 ID: id,
327 URL: url,
328 }
329}