all repos — telegram-bot-api @ b034326d859ade2024bfcb35c0d191928915a943

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