configs.go (view raw)
1package tgbotapi
2
3import (
4 "io"
5 "net/url"
6 "strconv"
7 "encoding/json"
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
87 return v, nil
88}
89
90// ForwardConfig contains information about a ForwardMessage request.
91type ForwardConfig struct {
92 Chattable
93 FromChatID int
94 FromChannelUsername string
95 MessageID int
96}
97
98func (config *ForwardConfig) Values() (url.Values, error) {
99 v, _ := config.Chattable.Values()
100
101 if config.FromChannelUsername != "" {
102 v.Add("chat_id", config.FromChannelUsername)
103 } else {
104 v.Add("chat_id", strconv.Itoa(config.FromChatID))
105 }
106 v.Add("message_id", strconv.Itoa(config.MessageID))
107
108 return v, nil
109}
110
111// PhotoConfig contains information about a SendPhoto request.
112type PhotoConfig struct {
113 Chattable
114 Caption string
115 ReplyToMessageID int
116 ReplyMarkup interface{}
117 UseExistingPhoto bool
118 FilePath string
119 File interface{}
120 FileID string
121}
122
123func (config *PhotoConfig) Values() (url.Values, error) {
124 v, _ := config.Chattable.Values()
125
126 v.Add("photo", config.FileID)
127 if config.Caption != "" {
128 v.Add("caption", config.Caption)
129 }
130 if config.ReplyToMessageID != 0 {
131 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
132 }
133 if config.ReplyMarkup != nil {
134 data, err := json.Marshal(config.ReplyMarkup)
135 if err != nil {
136 return v, err
137 }
138
139 v.Add("reply_markup", string(data))
140 }
141
142 return v, nil
143}
144
145// AudioConfig contains information about a SendAudio request.
146type AudioConfig struct {
147 Chattable
148 Duration int
149 Performer string
150 Title string
151 ReplyToMessageID int
152 ReplyMarkup interface{}
153 UseExistingAudio bool
154 FilePath string
155 File interface{}
156 FileID string
157}
158
159func (config *AudioConfig) Values() (url.Values, error) {
160 v, _ := config.Chattable.Values()
161
162 v.Add("audio", config.FileID)
163 if config.ReplyToMessageID != 0 {
164 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
165 }
166 if config.Duration != 0 {
167 v.Add("duration", strconv.Itoa(config.Duration))
168 }
169 if config.ReplyMarkup != nil {
170 data, err := json.Marshal(config.ReplyMarkup)
171 if err != nil {
172 return v, err
173 }
174
175 v.Add("reply_markup", string(data))
176 }
177 if config.Performer != "" {
178 v.Add("performer", config.Performer)
179 }
180 if config.Title != "" {
181 v.Add("title", config.Title)
182 }
183
184 return v, nil
185}
186
187// DocumentConfig contains information about a SendDocument request.
188type DocumentConfig struct {
189 Chattable
190 ReplyToMessageID int
191 ReplyMarkup interface{}
192 UseExistingDocument bool
193 FilePath string
194 File interface{}
195 FileID string
196}
197
198func (config *DocumentConfig) Values() (url.Values, error) {
199 v, _ := config.Chattable.Values()
200
201 v.Add("document", config.FileID)
202 if config.ReplyToMessageID != 0 {
203 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
204 }
205 if config.ReplyMarkup != nil {
206 data, err := json.Marshal(config.ReplyMarkup)
207 if err != nil {
208 return v, err
209 }
210
211 v.Add("reply_markup", string(data))
212 }
213
214 return v, nil
215}
216
217// StickerConfig contains information about a SendSticker request.
218type StickerConfig struct {
219 Chattable
220 ReplyToMessageID int
221 ReplyMarkup interface{}
222 UseExistingSticker bool
223 FilePath string
224 File interface{}
225 FileID string
226}
227
228func (config *StickerConfig) Values() (url.Values, error) {
229 v, _ := config.Chattable.Values()
230
231 v.Add("sticker", config.FileID)
232 if config.ReplyToMessageID != 0 {
233 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
234 }
235 if config.ReplyMarkup != nil {
236 data, err := json.Marshal(config.ReplyMarkup)
237 if err != nil {
238 return v, err
239 }
240
241 v.Add("reply_markup", string(data))
242 }
243
244 return v, nil
245}
246
247// VideoConfig contains information about a SendVideo request.
248type VideoConfig struct {
249 Chattable
250 Duration int
251 Caption string
252 ReplyToMessageID int
253 ReplyMarkup interface{}
254 UseExistingVideo bool
255 FilePath string
256 File interface{}
257 FileID string
258}
259
260func (config *VideoConfig) Values() (url.Values, error) {
261 v, _ := config.Chattable.Values()
262
263 v.Add("video", config.FileID)
264 if config.ReplyToMessageID != 0 {
265 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
266 }
267 if config.Duration != 0 {
268 v.Add("duration", strconv.Itoa(config.Duration))
269 }
270 if config.Caption != "" {
271 v.Add("caption", config.Caption)
272 }
273 if config.ReplyMarkup != nil {
274 data, err := json.Marshal(config.ReplyMarkup)
275 if err != nil {
276 return v, err
277 }
278
279 v.Add("reply_markup", string(data))
280 }
281
282 return v, nil
283}
284
285// VoiceConfig contains information about a SendVoice request.
286type VoiceConfig struct {
287 Chattable
288 Duration int
289 ReplyToMessageID int
290 ReplyMarkup interface{}
291 UseExistingVoice bool
292 FilePath string
293 File interface{}
294 FileID string
295}
296
297func (config *VoiceConfig) Values() (url.Values, error) {
298 v, _ := config.Chattable.Values()
299
300 v.Add("voice", config.FileID)
301 if config.ReplyToMessageID != 0 {
302 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
303 }
304 if config.Duration != 0 {
305 v.Add("duration", strconv.Itoa(config.Duration))
306 }
307 if config.ReplyMarkup != nil {
308 data, err := json.Marshal(config.ReplyMarkup)
309 if err != nil {
310 return v, err
311 }
312
313 v.Add("reply_markup", string(data))
314 }
315
316 return v, nil
317}
318
319// LocationConfig contains information about a SendLocation request.
320type LocationConfig struct {
321 Chattable
322 Latitude float64
323 Longitude float64
324 ReplyToMessageID int
325 ReplyMarkup interface{}
326}
327
328func (config *LocationConfig) Values() (url.Values, error) {
329 v, _ := config.Chattable.Values()
330
331 v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
332 v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
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}