all repos — telegram-bot-api @ d0711736ecd8b8ba32ff177eacf09d4b862ffaee

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