configs.go (view raw)
1package tgbotapi
2
3import (
4 "encoding/json"
5 "io"
6 "net/url"
7 "strconv"
8)
9
10// Telegram constants
11const (
12 // APIEndpoint is the endpoint for all API methods, with formatting for Sprintf
13 APIEndpoint = "https://api.telegram.org/bot%s/%s"
14 // FileEndpoint is the endpoint for downloading a file from Telegram
15 FileEndpoint = "https://api.telegram.org/file/bot%s/%s"
16)
17
18// Constant values for ChatActions
19const (
20 ChatTyping = "typing"
21 ChatUploadPhoto = "upload_photo"
22 ChatRecordVideo = "record_video"
23 ChatUploadVideo = "upload_video"
24 ChatRecordAudio = "record_audio"
25 ChatUploadAudio = "upload_audio"
26 ChatUploadDocument = "upload_document"
27 ChatFindLocation = "find_location"
28)
29
30// API errors
31const (
32 // APIForbidden happens when a token is bad
33 APIForbidden = "forbidden"
34)
35
36// Constant values for ParseMode in MessageConfig
37const (
38 ModeMarkdown = "Markdown"
39)
40
41// Base struct for all chat event(Message, Photo and so on)
42type Chattable struct {
43 ChatID int
44 ChannelUsername string
45}
46
47func (chattable *Chattable) Values() (url.Values, error) {
48 v := url.Values{}
49 if chattable.ChannelUsername != "" {
50 v.Add("chat_id", chattable.ChannelUsername)
51 } else {
52 v.Add("chat_id", strconv.Itoa(chattable.ChatID))
53 }
54 return v, nil
55}
56
57// MessageConfig contains information about a SendMessage request.
58type MessageConfig struct {
59 Chattable
60 Text string
61 ParseMode string
62 DisableWebPagePreview bool
63 ReplyToMessageID int
64 ReplyMarkup interface{}
65}
66
67func (config *MessageConfig) Values() (url.Values, error) {
68 v, _ := config.Chattable.Values()
69 v.Add("text", config.Text)
70 v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
71 if config.ParseMode != "" {
72 v.Add("parse_mode", config.ParseMode)
73 }
74 if config.ReplyToMessageID != 0 {
75 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
76 }
77 if config.ReplyMarkup != nil {
78 data, err := json.Marshal(config.ReplyMarkup)
79 if err != nil {
80 return v, err
81 }
82
83 v.Add("reply_markup", string(data))
84 }
85
86 return v, nil
87}
88
89// ForwardConfig contains information about a ForwardMessage request.
90type ForwardConfig struct {
91 Chattable
92 FromChatID int
93 FromChannelUsername string
94 MessageID int
95}
96
97func (config *ForwardConfig) Values() (url.Values, error) {
98 v, _ := config.Chattable.Values()
99
100 if config.FromChannelUsername != "" {
101 v.Add("chat_id", config.FromChannelUsername)
102 } else {
103 v.Add("chat_id", strconv.Itoa(config.FromChatID))
104 }
105 v.Add("message_id", strconv.Itoa(config.MessageID))
106
107 return v, nil
108}
109
110// PhotoConfig contains information about a SendPhoto request.
111type PhotoConfig struct {
112 Chattable
113 Caption string
114 ReplyToMessageID int
115 ReplyMarkup interface{}
116 UseExistingPhoto bool
117 FilePath string
118 File interface{}
119 FileID string
120}
121
122func (config *PhotoConfig) Values() (url.Values, error) {
123 v, _ := config.Chattable.Values()
124
125 v.Add("photo", config.FileID)
126 if config.Caption != "" {
127 v.Add("caption", config.Caption)
128 }
129 if config.ReplyToMessageID != 0 {
130 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
131 }
132 if config.ReplyMarkup != nil {
133 data, err := json.Marshal(config.ReplyMarkup)
134 if err != nil {
135 return v, err
136 }
137
138 v.Add("reply_markup", string(data))
139 }
140
141 return v, nil
142}
143
144// AudioConfig contains information about a SendAudio request.
145type AudioConfig struct {
146 Chattable
147 Duration int
148 Performer string
149 Title string
150 ReplyToMessageID int
151 ReplyMarkup interface{}
152 UseExistingAudio bool
153 FilePath string
154 File interface{}
155 FileID string
156}
157
158func (config *AudioConfig) Values() (url.Values, error) {
159 v, _ := config.Chattable.Values()
160
161 v.Add("audio", config.FileID)
162 if config.ReplyToMessageID != 0 {
163 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
164 }
165 if config.Duration != 0 {
166 v.Add("duration", strconv.Itoa(config.Duration))
167 }
168 if config.ReplyMarkup != nil {
169 data, err := json.Marshal(config.ReplyMarkup)
170 if err != nil {
171 return v, err
172 }
173
174 v.Add("reply_markup", string(data))
175 }
176 if config.Performer != "" {
177 v.Add("performer", config.Performer)
178 }
179 if config.Title != "" {
180 v.Add("title", config.Title)
181 }
182
183 return v, nil
184}
185
186// DocumentConfig contains information about a SendDocument request.
187type DocumentConfig struct {
188 Chattable
189 ReplyToMessageID int
190 ReplyMarkup interface{}
191 UseExistingDocument bool
192 FilePath string
193 File interface{}
194 FileID string
195}
196
197func (config *DocumentConfig) Values() (url.Values, error) {
198 v, _ := config.Chattable.Values()
199
200 v.Add("document", config.FileID)
201 if config.ReplyToMessageID != 0 {
202 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
203 }
204 if config.ReplyMarkup != nil {
205 data, err := json.Marshal(config.ReplyMarkup)
206 if err != nil {
207 return v, err
208 }
209
210 v.Add("reply_markup", string(data))
211 }
212
213 return v, nil
214}
215
216// StickerConfig contains information about a SendSticker request.
217type StickerConfig struct {
218 Chattable
219 ReplyToMessageID int
220 ReplyMarkup interface{}
221 UseExistingSticker bool
222 FilePath string
223 File interface{}
224 FileID string
225}
226
227func (config *StickerConfig) Values() (url.Values, error) {
228 v, _ := config.Chattable.Values()
229
230 v.Add("sticker", config.FileID)
231 if config.ReplyToMessageID != 0 {
232 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
233 }
234 if config.ReplyMarkup != nil {
235 data, err := json.Marshal(config.ReplyMarkup)
236 if err != nil {
237 return v, err
238 }
239
240 v.Add("reply_markup", string(data))
241 }
242
243 return v, nil
244}
245
246// VideoConfig contains information about a SendVideo request.
247type VideoConfig struct {
248 Chattable
249 Duration int
250 Caption string
251 ReplyToMessageID int
252 ReplyMarkup interface{}
253 UseExistingVideo bool
254 FilePath string
255 File interface{}
256 FileID string
257}
258
259func (config *VideoConfig) Values() (url.Values, error) {
260 v, _ := config.Chattable.Values()
261
262 v.Add("video", config.FileID)
263 if config.ReplyToMessageID != 0 {
264 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
265 }
266 if config.Duration != 0 {
267 v.Add("duration", strconv.Itoa(config.Duration))
268 }
269 if config.Caption != "" {
270 v.Add("caption", config.Caption)
271 }
272 if config.ReplyMarkup != nil {
273 data, err := json.Marshal(config.ReplyMarkup)
274 if err != nil {
275 return v, err
276 }
277
278 v.Add("reply_markup", string(data))
279 }
280
281 return v, nil
282}
283
284// VoiceConfig contains information about a SendVoice request.
285type VoiceConfig struct {
286 Chattable
287 Duration int
288 ReplyToMessageID int
289 ReplyMarkup interface{}
290 UseExistingVoice bool
291 FilePath string
292 File interface{}
293 FileID string
294}
295
296func (config *VoiceConfig) Values() (url.Values, error) {
297 v, _ := config.Chattable.Values()
298
299 v.Add("voice", config.FileID)
300 if config.ReplyToMessageID != 0 {
301 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
302 }
303 if config.Duration != 0 {
304 v.Add("duration", strconv.Itoa(config.Duration))
305 }
306 if config.ReplyMarkup != nil {
307 data, err := json.Marshal(config.ReplyMarkup)
308 if err != nil {
309 return v, err
310 }
311
312 v.Add("reply_markup", string(data))
313 }
314
315 return v, nil
316}
317
318// LocationConfig contains information about a SendLocation request.
319type LocationConfig struct {
320 Chattable
321 Latitude float64
322 Longitude float64
323 ReplyToMessageID int
324 ReplyMarkup interface{}
325}
326
327func (config *LocationConfig) Values() (url.Values, error) {
328 v, _ := config.Chattable.Values()
329
330 v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
331 v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
332
333 if config.ReplyToMessageID != 0 {
334 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
335 }
336 if config.ReplyMarkup != nil {
337 data, err := json.Marshal(config.ReplyMarkup)
338 if err != nil {
339 return v, err
340 }
341
342 v.Add("reply_markup", string(data))
343 }
344
345 return v, nil
346}
347
348// ChatActionConfig contains information about a SendChatAction request.
349type ChatActionConfig struct {
350 Chattable
351 Action string
352}
353
354func (config *ChatActionConfig) Values() (url.Values, error) {
355 v, _ := config.Chattable.Values()
356 v.Add("action", config.Action)
357 return v, nil
358}
359
360// UserProfilePhotosConfig contains information about a GetUserProfilePhotos request.
361type UserProfilePhotosConfig struct {
362 UserID int
363 Offset int
364 Limit int
365}
366
367// FileConfig has information about a file hosted on Telegram
368type FileConfig struct {
369 FileID string
370}
371
372// UpdateConfig contains information about a GetUpdates request.
373type UpdateConfig struct {
374 Offset int
375 Limit int
376 Timeout int
377}
378
379// WebhookConfig contains information about a SetWebhook request.
380type WebhookConfig struct {
381 Clear bool
382 URL *url.URL
383 Certificate interface{}
384}
385
386// FileBytes contains information about a set of bytes to upload as a File.
387type FileBytes struct {
388 Name string
389 Bytes []byte
390}
391
392// FileReader contains information about a reader to upload as a File.
393// If Size is -1, it will read the entire Reader into memory to calculate a Size.
394type FileReader struct {
395 Name string
396 Reader io.Reader
397 Size int64
398}