all repos — telegram-bot-api @ 9361631c6d3a7a10da24e4172d471026560b6e97

Golang bindings for the Telegram Bot API

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
 41type Chattable interface {
 42	Values() (url.Values, error)
 43}
 44
 45type Fileable interface {
 46	Chattable
 47	Params() (map[string]string, error)
 48	GetFile() interface{}
 49}
 50
 51// Base struct for all chat event(Message, Photo and so on)
 52type BaseChat struct {
 53	ChatID          int
 54	ChannelUsername string
 55}
 56
 57func (chat *BaseChat) Values() (url.Values, error) {
 58	v := url.Values{}
 59	if chat.ChannelUsername != "" {
 60		v.Add("chat_id", chat.ChannelUsername)
 61	} else {
 62		v.Add("chat_id", strconv.Itoa(chat.ChatID))
 63	}
 64	return v, nil
 65}
 66
 67type BaseFile struct {
 68	BaseChat
 69	FilePath    string
 70	File        interface{}
 71	FileID      string
 72	UseExisting bool
 73}
 74
 75func (file BaseFile) Params() (map[string]string, error) {
 76	params := make(map[string]string)
 77
 78	if file.ChannelUsername != "" {
 79		params["chat_id"] = file.ChannelUsername
 80	} else {
 81		params["chat_id"] = strconv.Itoa(file.ChatID)
 82	}
 83
 84	return params, nil
 85}
 86
 87func (file BaseFile) GetFile() interface{} {
 88	var result interface{}
 89	if file.FilePath == "" {
 90		result = file.File
 91	} else {
 92		result = file.FilePath
 93	}
 94
 95	return result
 96}
 97
 98// MessageConfig contains information about a SendMessage request.
 99type MessageConfig struct {
100	BaseChat
101	Text                  string
102	ParseMode             string
103	DisableWebPagePreview bool
104	ReplyToMessageID      int
105	ReplyMarkup           interface{}
106}
107
108func (config MessageConfig) Values() (url.Values, error) {
109	v, _ := config.BaseChat.Values()
110	v.Add("text", config.Text)
111	v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
112	if config.ParseMode != "" {
113		v.Add("parse_mode", config.ParseMode)
114	}
115	if config.ReplyToMessageID != 0 {
116		v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
117	}
118	if config.ReplyMarkup != nil {
119		data, err := json.Marshal(config.ReplyMarkup)
120		if err != nil {
121			return v, err
122		}
123
124		v.Add("reply_markup", string(data))
125	}
126
127	return v, nil
128}
129
130// ForwardConfig contains information about a ForwardMessage request.
131type ForwardConfig struct {
132	BaseChat
133	FromChatID          int
134	FromChannelUsername string
135	MessageID           int
136}
137
138func (config ForwardConfig) Values() (url.Values, error) {
139	v, _ := config.BaseChat.Values()
140
141	if config.FromChannelUsername != "" {
142		v.Add("chat_id", config.FromChannelUsername)
143	} else {
144		v.Add("chat_id", strconv.Itoa(config.FromChatID))
145	}
146	v.Add("message_id", strconv.Itoa(config.MessageID))
147
148	return v, nil
149}
150
151// PhotoConfig contains information about a SendPhoto request.
152type PhotoConfig struct {
153	BaseFile
154	Caption          string
155	ReplyToMessageID int
156	ReplyMarkup      interface{}
157}
158
159func (config PhotoConfig) Params() (map[string]string, error) {
160	params, _ := config.BaseFile.Params()
161
162	if config.Caption != "" {
163		params["caption"] = config.Caption
164	}
165	if config.ReplyToMessageID != 0 {
166		params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
167	}
168	if config.ReplyMarkup != nil {
169		data, err := json.Marshal(config.ReplyMarkup)
170		if err != nil {
171			return params, err
172		}
173
174		params["reply_markup"] = string(data)
175	}
176
177	return params, nil
178}
179
180func (config PhotoConfig) Values() (url.Values, error) {
181	v, _ := config.BaseChat.Values()
182
183	v.Add("photo", config.FileID)
184	if config.Caption != "" {
185		v.Add("caption", config.Caption)
186	}
187	if config.ReplyToMessageID != 0 {
188		v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
189	}
190	if config.ReplyMarkup != nil {
191		data, err := json.Marshal(config.ReplyMarkup)
192		if err != nil {
193			return v, err
194		}
195
196		v.Add("reply_markup", string(data))
197	}
198
199	return v, nil
200}
201
202// AudioConfig contains information about a SendAudio request.
203type AudioConfig struct {
204	BaseFile
205	Duration         int
206	Performer        string
207	Title            string
208	ReplyToMessageID int
209	ReplyMarkup      interface{}
210}
211
212func (config AudioConfig) Values() (url.Values, error) {
213	v, _ := config.BaseChat.Values()
214
215	v.Add("audio", config.FileID)
216	if config.ReplyToMessageID != 0 {
217		v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
218	}
219	if config.Duration != 0 {
220		v.Add("duration", strconv.Itoa(config.Duration))
221	}
222	if config.ReplyMarkup != nil {
223		data, err := json.Marshal(config.ReplyMarkup)
224		if err != nil {
225			return v, err
226		}
227
228		v.Add("reply_markup", string(data))
229	}
230	if config.Performer != "" {
231		v.Add("performer", config.Performer)
232	}
233	if config.Title != "" {
234		v.Add("title", config.Title)
235	}
236
237	return v, nil
238}
239
240func (config AudioConfig) Params() (map[string]string, error) {
241	params, _ := config.BaseFile.Params()
242
243	if config.ReplyToMessageID != 0 {
244		params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
245	}
246	if config.Duration != 0 {
247		params["duration"] = strconv.Itoa(config.Duration)
248	}
249	if config.ReplyMarkup != nil {
250		data, err := json.Marshal(config.ReplyMarkup)
251		if err != nil {
252			return params, err
253		}
254
255		params["reply_markup"] = string(data)
256	}
257	if config.Performer != "" {
258		params["performer"] = config.Performer
259	}
260	if config.Title != "" {
261		params["title"] = config.Title
262	}
263
264	return params, nil
265}
266
267// DocumentConfig contains information about a SendDocument request.
268type DocumentConfig struct {
269	BaseFile
270	ReplyToMessageID int
271	ReplyMarkup      interface{}
272}
273
274func (config DocumentConfig) Values() (url.Values, error) {
275	v, _ := config.BaseChat.Values()
276
277	v.Add("document", config.FileID)
278	if config.ReplyToMessageID != 0 {
279		v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
280	}
281	if config.ReplyMarkup != nil {
282		data, err := json.Marshal(config.ReplyMarkup)
283		if err != nil {
284			return v, err
285		}
286
287		v.Add("reply_markup", string(data))
288	}
289
290	return v, nil
291}
292
293func (config DocumentConfig) Params() (map[string]string, error) {
294	params, _ := config.BaseFile.Params()
295
296	if config.ReplyToMessageID != 0 {
297		params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
298	}
299	if config.ReplyMarkup != nil {
300		data, err := json.Marshal(config.ReplyMarkup)
301		if err != nil {
302			return params, err
303		}
304
305		params["reply_markup"] = string(data)
306	}
307
308	return params, nil
309}
310
311// StickerConfig contains information about a SendSticker request.
312type StickerConfig struct {
313	BaseFile
314	ReplyToMessageID int
315	ReplyMarkup      interface{}
316}
317
318func (config StickerConfig) Values() (url.Values, error) {
319	v, _ := config.BaseChat.Values()
320
321	v.Add("sticker", config.FileID)
322	if config.ReplyToMessageID != 0 {
323		v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
324	}
325	if config.ReplyMarkup != nil {
326		data, err := json.Marshal(config.ReplyMarkup)
327		if err != nil {
328			return v, err
329		}
330
331		v.Add("reply_markup", string(data))
332	}
333
334	return v, nil
335}
336
337func (config StickerConfig) Params() (map[string]string, error) {
338	params, _ := config.BaseFile.Params()
339
340	if config.ReplyToMessageID != 0 {
341		params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
342	}
343	if config.ReplyMarkup != nil {
344		data, err := json.Marshal(config.ReplyMarkup)
345		if err != nil {
346			return params, err
347		}
348
349		params["reply_markup"] = string(data)
350	}
351
352	return params, nil
353}
354
355// VideoConfig contains information about a SendVideo request.
356type VideoConfig struct {
357	BaseFile
358	Duration         int
359	Caption          string
360	ReplyToMessageID int
361	ReplyMarkup      interface{}
362}
363
364func (config VideoConfig) Values() (url.Values, error) {
365	v, _ := config.BaseChat.Values()
366
367	v.Add("video", config.FileID)
368	if config.ReplyToMessageID != 0 {
369		v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
370	}
371	if config.Duration != 0 {
372		v.Add("duration", strconv.Itoa(config.Duration))
373	}
374	if config.Caption != "" {
375		v.Add("caption", config.Caption)
376	}
377	if config.ReplyMarkup != nil {
378		data, err := json.Marshal(config.ReplyMarkup)
379		if err != nil {
380			return v, err
381		}
382
383		v.Add("reply_markup", string(data))
384	}
385
386	return v, nil
387}
388
389func (config VideoConfig) Params() (map[string]string, error) {
390	params, _ := config.BaseFile.Params()
391
392	if config.ReplyToMessageID != 0 {
393		params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
394	}
395	if config.ReplyMarkup != nil {
396		data, err := json.Marshal(config.ReplyMarkup)
397		if err != nil {
398			return params, err
399		}
400
401		params["reply_markup"] = string(data)
402	}
403
404	return params, nil
405}
406
407// VoiceConfig contains information about a SendVoice request.
408type VoiceConfig struct {
409	BaseFile
410	Duration         int
411	ReplyToMessageID int
412	ReplyMarkup      interface{}
413}
414
415func (config VoiceConfig) Values() (url.Values, error) {
416	v, _ := config.BaseChat.Values()
417
418	v.Add("voice", config.FileID)
419	if config.ReplyToMessageID != 0 {
420		v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
421	}
422	if config.Duration != 0 {
423		v.Add("duration", strconv.Itoa(config.Duration))
424	}
425	if config.ReplyMarkup != nil {
426		data, err := json.Marshal(config.ReplyMarkup)
427		if err != nil {
428			return v, err
429		}
430
431		v.Add("reply_markup", string(data))
432	}
433
434	return v, nil
435}
436
437func (config VoiceConfig) Params() (map[string]string, error) {
438	params, _ := config.BaseFile.Params()
439
440	if config.ReplyToMessageID != 0 {
441		params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
442	}
443	if config.Duration != 0 {
444		params["duration"] = strconv.Itoa(config.Duration)
445	}
446	if config.ReplyMarkup != nil {
447		data, err := json.Marshal(config.ReplyMarkup)
448		if err != nil {
449			return params, err
450		}
451
452		params["reply_markup"] = string(data)
453	}
454
455	return params, nil
456}
457
458// LocationConfig contains information about a SendLocation request.
459type LocationConfig struct {
460	BaseChat
461	Latitude         float64
462	Longitude        float64
463	ReplyToMessageID int
464	ReplyMarkup      interface{}
465}
466
467func (config LocationConfig) Values() (url.Values, error) {
468	v, _ := config.BaseChat.Values()
469
470	v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
471	v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
472
473	if config.ReplyToMessageID != 0 {
474		v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
475	}
476	if config.ReplyMarkup != nil {
477		data, err := json.Marshal(config.ReplyMarkup)
478		if err != nil {
479			return v, err
480		}
481
482		v.Add("reply_markup", string(data))
483	}
484
485	return v, nil
486}
487
488// ChatActionConfig contains information about a SendChatAction request.
489type ChatActionConfig struct {
490	BaseChat
491	Action string
492}
493
494func (config ChatActionConfig) Values() (url.Values, error) {
495	v, _ := config.BaseChat.Values()
496	v.Add("action", config.Action)
497	return v, nil
498}
499
500// UserProfilePhotosConfig contains information about a GetUserProfilePhotos request.
501type UserProfilePhotosConfig struct {
502	UserID int
503	Offset int
504	Limit  int
505}
506
507// FileConfig has information about a file hosted on Telegram
508type FileConfig struct {
509	FileID string
510}
511
512// UpdateConfig contains information about a GetUpdates request.
513type UpdateConfig struct {
514	Offset  int
515	Limit   int
516	Timeout int
517}
518
519// WebhookConfig contains information about a SetWebhook request.
520type WebhookConfig struct {
521	Clear       bool
522	URL         *url.URL
523	Certificate interface{}
524}
525
526// FileBytes contains information about a set of bytes to upload as a File.
527type FileBytes struct {
528	Name  string
529	Bytes []byte
530}
531
532// FileReader contains information about a reader to upload as a File.
533// If Size is -1, it will read the entire Reader into memory to calculate a Size.
534type FileReader struct {
535	Name   string
536	Reader io.Reader
537	Size   int64
538}