all repos — telegram-bot-api @ c67df954072c3347a29182a1fbcf8b69b096a5f2

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("from_chat_id", strconv.Itoa(config.FromChatID))
169	v.Add("message_id", strconv.Itoa(config.MessageID))
170	return v, nil
171}
172
173func (config ForwardConfig) Method() string {
174	return "forwardMessage"
175}
176
177// PhotoConfig contains information about a SendPhoto request.
178type PhotoConfig struct {
179	BaseFile
180	Caption string
181}
182
183func (config PhotoConfig) Params() (map[string]string, error) {
184	params, _ := config.BaseFile.Params()
185
186	if config.Caption != "" {
187		params["caption"] = config.Caption
188	}
189
190	return params, nil
191}
192
193func (config PhotoConfig) Values() (url.Values, error) {
194	v, _ := config.BaseChat.Values()
195
196	v.Add("photo", config.FileID)
197	if config.Caption != "" {
198		v.Add("caption", config.Caption)
199	}
200	return v, nil
201}
202
203func (config PhotoConfig) Name() string {
204	return "photo"
205}
206
207func (config PhotoConfig) Method() string {
208	return "SendPhoto"
209}
210
211// AudioConfig contains information about a SendAudio request.
212type AudioConfig struct {
213	BaseFile
214	Duration  int
215	Performer string
216	Title     string
217}
218
219func (config AudioConfig) Values() (url.Values, error) {
220	v, _ := config.BaseChat.Values()
221
222	v.Add("audio", config.FileID)
223	if config.Duration != 0 {
224		v.Add("duration", strconv.Itoa(config.Duration))
225	}
226
227	if config.Performer != "" {
228		v.Add("performer", config.Performer)
229	}
230	if config.Title != "" {
231		v.Add("title", config.Title)
232	}
233
234	return v, nil
235}
236
237func (config AudioConfig) Params() (map[string]string, error) {
238	params, _ := config.BaseFile.Params()
239
240	if config.Duration != 0 {
241		params["duration"] = strconv.Itoa(config.Duration)
242	}
243
244	if config.Performer != "" {
245		params["performer"] = config.Performer
246	}
247	if config.Title != "" {
248		params["title"] = config.Title
249	}
250
251	return params, nil
252}
253
254func (config AudioConfig) Name() string {
255	return "audio"
256}
257
258func (config AudioConfig) Method() string {
259	return "SendAudio"
260}
261
262// DocumentConfig contains information about a SendDocument request.
263type DocumentConfig struct {
264	BaseFile
265}
266
267func (config DocumentConfig) Values() (url.Values, error) {
268	v, _ := config.BaseChat.Values()
269
270	v.Add("document", config.FileID)
271
272	return v, nil
273}
274
275func (config DocumentConfig) Params() (map[string]string, error) {
276	params, _ := config.BaseFile.Params()
277
278	return params, nil
279}
280
281func (config DocumentConfig) Name() string {
282	return "document"
283}
284
285func (config DocumentConfig) Method() string {
286	return "sendDocument"
287}
288
289// StickerConfig contains information about a SendSticker request.
290type StickerConfig struct {
291	BaseFile
292}
293
294func (config StickerConfig) Values() (url.Values, error) {
295	v, _ := config.BaseChat.Values()
296
297	v.Add("sticker", config.FileID)
298
299	return v, nil
300}
301
302func (config StickerConfig) Params() (map[string]string, error) {
303	params, _ := config.BaseFile.Params()
304
305	return params, nil
306}
307
308func (config StickerConfig) Name() string {
309	return "sticker"
310}
311
312func (config StickerConfig) Method() string {
313	return "sendSticker"
314}
315
316// VideoConfig contains information about a SendVideo request.
317type VideoConfig struct {
318	BaseFile
319	Duration int
320	Caption  string
321}
322
323func (config VideoConfig) Values() (url.Values, error) {
324	v, _ := config.BaseChat.Values()
325
326	v.Add("video", config.FileID)
327	if config.Duration != 0 {
328		v.Add("duration", strconv.Itoa(config.Duration))
329	}
330	if config.Caption != "" {
331		v.Add("caption", config.Caption)
332	}
333
334	return v, nil
335}
336
337func (config VideoConfig) Params() (map[string]string, error) {
338	params, _ := config.BaseFile.Params()
339
340	return params, nil
341}
342
343func (config VideoConfig) Name() string {
344	return "video"
345}
346
347func (config VideoConfig) Method() string {
348	return "sendVideo"
349}
350
351// VoiceConfig contains information about a SendVoice request.
352type VoiceConfig struct {
353	BaseFile
354	Duration int
355}
356
357func (config VoiceConfig) Values() (url.Values, error) {
358	v, _ := config.BaseChat.Values()
359
360	v.Add("voice", config.FileID)
361	if config.Duration != 0 {
362		v.Add("duration", strconv.Itoa(config.Duration))
363	}
364
365	return v, nil
366}
367
368func (config VoiceConfig) Params() (map[string]string, error) {
369	params, _ := config.BaseFile.Params()
370
371	if config.Duration != 0 {
372		params["duration"] = strconv.Itoa(config.Duration)
373	}
374
375	return params, nil
376}
377
378func (config VoiceConfig) Name() string {
379	return "voice"
380}
381
382func (config VoiceConfig) Method() string {
383	return "sendVoice"
384}
385
386// LocationConfig contains information about a SendLocation request.
387type LocationConfig struct {
388	BaseChat
389	Latitude  float64
390	Longitude float64
391}
392
393func (config LocationConfig) Values() (url.Values, error) {
394	v, _ := config.BaseChat.Values()
395
396	v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
397	v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
398
399	return v, nil
400}
401
402func (config LocationConfig) Method() string {
403	return "sendLocation"
404}
405
406// ChatActionConfig contains information about a SendChatAction request.
407type ChatActionConfig struct {
408	BaseChat
409	Action string
410}
411
412func (config ChatActionConfig) Values() (url.Values, error) {
413	v, _ := config.BaseChat.Values()
414	v.Add("action", config.Action)
415	return v, nil
416}
417
418// UserProfilePhotosConfig contains information about a GetUserProfilePhotos request.
419type UserProfilePhotosConfig struct {
420	UserID int
421	Offset int
422	Limit  int
423}
424
425// FileConfig has information about a file hosted on Telegram
426type FileConfig struct {
427	FileID string
428}
429
430// UpdateConfig contains information about a GetUpdates request.
431type UpdateConfig struct {
432	Offset  int
433	Limit   int
434	Timeout int
435}
436
437// WebhookConfig contains information about a SetWebhook request.
438type WebhookConfig struct {
439	Clear       bool
440	URL         *url.URL
441	Certificate interface{}
442}
443
444// FileBytes contains information about a set of bytes to upload as a File.
445type FileBytes struct {
446	Name  string
447	Bytes []byte
448}
449
450// FileReader contains information about a reader to upload as a File.
451// If Size is -1, it will read the entire Reader into memory to calculate a Size.
452type FileReader struct {
453	Name   string
454	Reader io.Reader
455	Size   int64
456}