bot.go (view raw)
1// Package tgbotapi has functions and types used for interacting with
2// the Telegram Bot API.
3package tgbotapi
4
5import (
6 "encoding/json"
7 "errors"
8 "fmt"
9 "io"
10 "mime/multipart"
11 "net/http"
12 "net/url"
13 "strings"
14 "time"
15)
16
17// HTTPClient is the type needed for the bot to perform HTTP requests.
18type HTTPClient interface {
19 Do(req *http.Request) (*http.Response, error)
20}
21
22// BotAPI allows you to interact with the Telegram Bot API.
23type BotAPI struct {
24 Token string `json:"token"`
25 Debug bool `json:"debug"`
26 Buffer int `json:"buffer"`
27
28 Self User `json:"-"`
29 Client HTTPClient `json:"-"`
30 shutdownChannel chan interface{}
31
32 apiEndpoint string
33}
34
35// NewBotAPI creates a new BotAPI instance.
36//
37// It requires a token, provided by @BotFather on Telegram.
38func NewBotAPI(token string) (*BotAPI, error) {
39 return NewBotAPIWithClient(token, APIEndpoint, &http.Client{})
40}
41
42// NewBotAPIWithAPIEndpoint creates a new BotAPI instance
43// and allows you to pass API endpoint.
44//
45// It requires a token, provided by @BotFather on Telegram and API endpoint.
46func NewBotAPIWithAPIEndpoint(token, apiEndpoint string) (*BotAPI, error) {
47 return NewBotAPIWithClient(token, apiEndpoint, &http.Client{})
48}
49
50// NewBotAPIWithClient creates a new BotAPI instance
51// and allows you to pass a http.Client.
52//
53// It requires a token, provided by @BotFather on Telegram and API endpoint.
54func NewBotAPIWithClient(token, apiEndpoint string, client HTTPClient) (*BotAPI, error) {
55 bot := &BotAPI{
56 Token: token,
57 Client: client,
58 Buffer: 100,
59 shutdownChannel: make(chan interface{}),
60
61 apiEndpoint: apiEndpoint,
62 }
63
64 self, err := bot.GetMe()
65 if err != nil {
66 return nil, err
67 }
68
69 bot.Self = self
70
71 return bot, nil
72}
73
74// SetAPIEndpoint changes the Telegram Bot API endpoint used by the instance.
75func (bot *BotAPI) SetAPIEndpoint(apiEndpoint string) {
76 bot.apiEndpoint = apiEndpoint
77}
78
79func buildParams(in Params) url.Values {
80 if in == nil {
81 return url.Values{}
82 }
83
84 out := url.Values{}
85
86 for key, value := range in {
87 out.Set(key, value)
88 }
89
90 return out
91}
92
93// MakeRequest makes a request to a specific endpoint with our token.
94func (bot *BotAPI) MakeRequest(endpoint string, params Params) (*APIResponse, error) {
95 if bot.Debug {
96 log.Printf("Endpoint: %s, params: %v\n", endpoint, params)
97 }
98
99 method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)
100
101 values := buildParams(params)
102
103 req, err := http.NewRequest("POST", method, strings.NewReader(values.Encode()))
104 if err != nil {
105 return &APIResponse{}, err
106 }
107 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
108
109 resp, err := bot.Client.Do(req)
110 if err != nil {
111 return nil, err
112 }
113 defer resp.Body.Close()
114
115 var apiResp APIResponse
116 bytes, err := bot.decodeAPIResponse(resp.Body, &apiResp)
117 if err != nil {
118 return &apiResp, err
119 }
120
121 if bot.Debug {
122 log.Printf("Endpoint: %s, response: %s\n", endpoint, string(bytes))
123 }
124
125 if !apiResp.Ok {
126 var parameters ResponseParameters
127
128 if apiResp.Parameters != nil {
129 parameters = *apiResp.Parameters
130 }
131
132 return &apiResp, &Error{
133 Code: apiResp.ErrorCode,
134 Message: apiResp.Description,
135 ResponseParameters: parameters,
136 }
137 }
138
139 return &apiResp, nil
140}
141
142// decodeAPIResponse decode response and return slice of bytes if debug enabled.
143// If debug disabled, just decode http.Response.Body stream to APIResponse struct
144// for efficient memory usage
145func (bot *BotAPI) decodeAPIResponse(responseBody io.Reader, resp *APIResponse) ([]byte, error) {
146 if !bot.Debug {
147 dec := json.NewDecoder(responseBody)
148 err := dec.Decode(resp)
149 return nil, err
150 }
151
152 // if debug, read response body
153 data, err := io.ReadAll(responseBody)
154 if err != nil {
155 return nil, err
156 }
157
158 err = json.Unmarshal(data, resp)
159 if err != nil {
160 return nil, err
161 }
162
163 return data, nil
164}
165
166// UploadFiles makes a request to the API with files.
167func (bot *BotAPI) UploadFiles(endpoint string, params Params, files []RequestFile) (*APIResponse, error) {
168 r, w := io.Pipe()
169 m := multipart.NewWriter(w)
170
171 // This code modified from the very helpful @HirbodBehnam
172 // https://github.com/go-telegram-bot-api/telegram-bot-api/issues/354#issuecomment-663856473
173 go func() {
174 defer w.Close()
175 defer m.Close()
176
177 for field, value := range params {
178 if err := m.WriteField(field, value); err != nil {
179 w.CloseWithError(err)
180 return
181 }
182 }
183
184 for _, file := range files {
185 if file.Data.NeedsUpload() {
186 name, reader, err := file.Data.UploadData()
187 if err != nil {
188 w.CloseWithError(err)
189 return
190 }
191
192 part, err := m.CreateFormFile(file.Name, name)
193 if err != nil {
194 w.CloseWithError(err)
195 return
196 }
197
198 if _, err := io.Copy(part, reader); err != nil {
199 w.CloseWithError(err)
200 return
201 }
202
203 if closer, ok := reader.(io.ReadCloser); ok {
204 if err = closer.Close(); err != nil {
205 w.CloseWithError(err)
206 return
207 }
208 }
209 } else {
210 value := file.Data.SendData()
211
212 if err := m.WriteField(file.Name, value); err != nil {
213 w.CloseWithError(err)
214 return
215 }
216 }
217 }
218 }()
219
220 if bot.Debug {
221 log.Printf("Endpoint: %s, params: %v, with %d files\n", endpoint, params, len(files))
222 }
223
224 method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)
225
226 req, err := http.NewRequest("POST", method, r)
227 if err != nil {
228 return nil, err
229 }
230
231 req.Header.Set("Content-Type", m.FormDataContentType())
232
233 resp, err := bot.Client.Do(req)
234 if err != nil {
235 return nil, err
236 }
237 defer resp.Body.Close()
238
239 var apiResp APIResponse
240 bytes, err := bot.decodeAPIResponse(resp.Body, &apiResp)
241 if err != nil {
242 return &apiResp, err
243 }
244
245 if bot.Debug {
246 log.Printf("Endpoint: %s, response: %s\n", endpoint, string(bytes))
247 }
248
249 if !apiResp.Ok {
250 var parameters ResponseParameters
251
252 if apiResp.Parameters != nil {
253 parameters = *apiResp.Parameters
254 }
255
256 return &apiResp, &Error{
257 Message: apiResp.Description,
258 ResponseParameters: parameters,
259 }
260 }
261
262 return &apiResp, nil
263}
264
265// GetFileDirectURL returns direct URL to file
266//
267// It requires the FileID.
268func (bot *BotAPI) GetFileDirectURL(fileID string) (string, error) {
269 file, err := bot.GetFile(FileConfig{fileID})
270
271 if err != nil {
272 return "", err
273 }
274
275 return file.Link(bot.Token), nil
276}
277
278// GetMe fetches the currently authenticated bot.
279//
280// This method is called upon creation to validate the token,
281// and so you may get this data from BotAPI.Self without the need for
282// another request.
283func (bot *BotAPI) GetMe() (User, error) {
284 resp, err := bot.MakeRequest("getMe", nil)
285 if err != nil {
286 return User{}, err
287 }
288
289 var user User
290 err = json.Unmarshal(resp.Result, &user)
291
292 return user, err
293}
294
295// IsMessageToMe returns true if message directed to this bot.
296//
297// It requires the Message.
298func (bot *BotAPI) IsMessageToMe(message Message) bool {
299 return strings.Contains(message.Text, "@"+bot.Self.UserName)
300}
301
302func hasFilesNeedingUpload(files []RequestFile) bool {
303 for _, file := range files {
304 if file.Data.NeedsUpload() {
305 return true
306 }
307 }
308
309 return false
310}
311
312// Request sends a Chattable to Telegram, and returns the APIResponse.
313func (bot *BotAPI) Request(c Chattable) (*APIResponse, error) {
314 params, err := c.params()
315 if err != nil {
316 return nil, err
317 }
318
319 if t, ok := c.(Fileable); ok {
320 files := t.files()
321
322 // If we have files that need to be uploaded, we should delegate the
323 // request to UploadFile.
324 if hasFilesNeedingUpload(files) {
325 return bot.UploadFiles(t.method(), params, files)
326 }
327
328 // However, if there are no files to be uploaded, there's likely things
329 // that need to be turned into params instead.
330 for _, file := range files {
331 params[file.Name] = file.Data.SendData()
332 }
333 }
334
335 return bot.MakeRequest(c.method(), params)
336}
337
338// Send will send a Chattable item to Telegram and provides the
339// returned Message.
340func (bot *BotAPI) Send(c Chattable) (Message, error) {
341 resp, err := bot.Request(c)
342 if err != nil {
343 return Message{}, err
344 }
345
346 var message Message
347 err = json.Unmarshal(resp.Result, &message)
348
349 return message, err
350}
351
352// SendMediaGroup sends a media group and returns the resulting messages.
353func (bot *BotAPI) SendMediaGroup(config MediaGroupConfig) ([]Message, error) {
354 resp, err := bot.Request(config)
355 if err != nil {
356 return nil, err
357 }
358
359 var messages []Message
360 err = json.Unmarshal(resp.Result, &messages)
361
362 return messages, err
363}
364
365// GetUserProfilePhotos gets a user's profile photos.
366//
367// It requires UserID.
368// Offset and Limit are optional.
369func (bot *BotAPI) GetUserProfilePhotos(config UserProfilePhotosConfig) (UserProfilePhotos, error) {
370 resp, err := bot.Request(config)
371 if err != nil {
372 return UserProfilePhotos{}, err
373 }
374
375 var profilePhotos UserProfilePhotos
376 err = json.Unmarshal(resp.Result, &profilePhotos)
377
378 return profilePhotos, err
379}
380
381// GetFile returns a File which can download a file from Telegram.
382//
383// Requires FileID.
384func (bot *BotAPI) GetFile(config FileConfig) (File, error) {
385 resp, err := bot.Request(config)
386 if err != nil {
387 return File{}, err
388 }
389
390 var file File
391 err = json.Unmarshal(resp.Result, &file)
392
393 return file, err
394}
395
396// GetUpdates fetches updates.
397// If a WebHook is set, this will not return any data!
398//
399// Offset, Limit, Timeout, and AllowedUpdates are optional.
400// To avoid stale items, set Offset to one higher than the previous item.
401// Set Timeout to a large number to reduce requests, so you can get updates
402// instantly instead of having to wait between requests.
403func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) {
404 resp, err := bot.Request(config)
405 if err != nil {
406 return []Update{}, err
407 }
408
409 var updates []Update
410 err = json.Unmarshal(resp.Result, &updates)
411
412 return updates, err
413}
414
415// GetWebhookInfo allows you to fetch information about a webhook and if
416// one currently is set, along with pending update count and error messages.
417func (bot *BotAPI) GetWebhookInfo() (WebhookInfo, error) {
418 resp, err := bot.MakeRequest("getWebhookInfo", nil)
419 if err != nil {
420 return WebhookInfo{}, err
421 }
422
423 var info WebhookInfo
424 err = json.Unmarshal(resp.Result, &info)
425
426 return info, err
427}
428
429// GetUpdatesChan starts and returns a channel for getting updates.
430func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) UpdatesChannel {
431 ch := make(chan Update, bot.Buffer)
432
433 go func() {
434 for {
435 select {
436 case <-bot.shutdownChannel:
437 close(ch)
438 return
439 default:
440 }
441
442 updates, err := bot.GetUpdates(config)
443 if err != nil {
444 log.Println(err)
445 log.Println("Failed to get updates, retrying in 3 seconds...")
446 time.Sleep(time.Second * 3)
447
448 continue
449 }
450
451 for _, update := range updates {
452 if update.UpdateID >= config.Offset {
453 config.Offset = update.UpdateID + 1
454 ch <- update
455 }
456 }
457 }
458 }()
459
460 return ch
461}
462
463// StopReceivingUpdates stops the go routine which receives updates
464func (bot *BotAPI) StopReceivingUpdates() {
465 if bot.Debug {
466 log.Println("Stopping the update receiver routine...")
467 }
468 close(bot.shutdownChannel)
469}
470
471// ListenForWebhook registers a http handler for a webhook.
472func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel {
473 ch := make(chan Update, bot.Buffer)
474
475 http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
476 update, err := bot.HandleUpdate(r)
477 if err != nil {
478 errMsg, _ := json.Marshal(map[string]string{"error": err.Error()})
479 w.WriteHeader(http.StatusBadRequest)
480 w.Header().Set("Content-Type", "application/json")
481 _, _ = w.Write(errMsg)
482 return
483 }
484
485 ch <- *update
486 })
487
488 return ch
489}
490
491// ListenForWebhookRespReqFormat registers a http handler for a single incoming webhook.
492func (bot *BotAPI) ListenForWebhookRespReqFormat(w http.ResponseWriter, r *http.Request) UpdatesChannel {
493 ch := make(chan Update, bot.Buffer)
494
495 func(w http.ResponseWriter, r *http.Request) {
496 update, err := bot.HandleUpdate(r)
497 if err != nil {
498 errMsg, _ := json.Marshal(map[string]string{"error": err.Error()})
499 w.WriteHeader(http.StatusBadRequest)
500 w.Header().Set("Content-Type", "application/json")
501 _, _ = w.Write(errMsg)
502 return
503 }
504
505 ch <- *update
506 close(ch)
507 }(w, r)
508
509 return ch
510}
511
512// HandleUpdate parses and returns update received via webhook
513func (bot *BotAPI) HandleUpdate(r *http.Request) (*Update, error) {
514 if r.Method != http.MethodPost {
515 err := errors.New("wrong HTTP method required POST")
516 return nil, err
517 }
518
519 var update Update
520 err := json.NewDecoder(r.Body).Decode(&update)
521 if err != nil {
522 return nil, err
523 }
524
525 return &update, nil
526}
527
528// WriteToHTTPResponse writes the request to the HTTP ResponseWriter.
529//
530// It doesn't support uploading files.
531//
532// See https://core.telegram.org/bots/api#making-requests-when-getting-updates
533// for details.
534func WriteToHTTPResponse(w http.ResponseWriter, c Chattable) error {
535 params, err := c.params()
536 if err != nil {
537 return err
538 }
539
540 if t, ok := c.(Fileable); ok {
541 if hasFilesNeedingUpload(t.files()) {
542 return errors.New("unable to use http response to upload files")
543 }
544 }
545
546 values := buildParams(params)
547 values.Set("method", c.method())
548
549 w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
550 _, err = w.Write([]byte(values.Encode()))
551 return err
552}
553
554// GetChat gets information about a chat.
555func (bot *BotAPI) GetChat(config ChatInfoConfig) (Chat, error) {
556 resp, err := bot.Request(config)
557 if err != nil {
558 return Chat{}, err
559 }
560
561 var chat Chat
562 err = json.Unmarshal(resp.Result, &chat)
563
564 return chat, err
565}
566
567// GetChatAdministrators gets a list of administrators in the chat.
568//
569// If none have been appointed, only the creator will be returned.
570// Bots are not shown, even if they are an administrator.
571func (bot *BotAPI) GetChatAdministrators(config ChatAdministratorsConfig) ([]ChatMember, error) {
572 resp, err := bot.Request(config)
573 if err != nil {
574 return []ChatMember{}, err
575 }
576
577 var members []ChatMember
578 err = json.Unmarshal(resp.Result, &members)
579
580 return members, err
581}
582
583// GetChatMembersCount gets the number of users in a chat.
584func (bot *BotAPI) GetChatMembersCount(config ChatMemberCountConfig) (int, error) {
585 resp, err := bot.Request(config)
586 if err != nil {
587 return -1, err
588 }
589
590 var count int
591 err = json.Unmarshal(resp.Result, &count)
592
593 return count, err
594}
595
596// GetChatMember gets a specific chat member.
597func (bot *BotAPI) GetChatMember(config GetChatMemberConfig) (ChatMember, error) {
598 resp, err := bot.Request(config)
599 if err != nil {
600 return ChatMember{}, err
601 }
602
603 var member ChatMember
604 err = json.Unmarshal(resp.Result, &member)
605
606 return member, err
607}
608
609// GetGameHighScores allows you to get the high scores for a game.
610func (bot *BotAPI) GetGameHighScores(config GetGameHighScoresConfig) ([]GameHighScore, error) {
611 resp, err := bot.Request(config)
612 if err != nil {
613 return []GameHighScore{}, err
614 }
615
616 var highScores []GameHighScore
617 err = json.Unmarshal(resp.Result, &highScores)
618
619 return highScores, err
620}
621
622// GetInviteLink get InviteLink for a chat
623func (bot *BotAPI) GetInviteLink(config ChatInviteLinkConfig) (string, error) {
624 resp, err := bot.Request(config)
625 if err != nil {
626 return "", err
627 }
628
629 var inviteLink string
630 err = json.Unmarshal(resp.Result, &inviteLink)
631
632 return inviteLink, err
633}
634
635// GetStickerSet returns a StickerSet.
636func (bot *BotAPI) GetStickerSet(config GetStickerSetConfig) (StickerSet, error) {
637 resp, err := bot.Request(config)
638 if err != nil {
639 return StickerSet{}, err
640 }
641
642 var stickers StickerSet
643 err = json.Unmarshal(resp.Result, &stickers)
644
645 return stickers, err
646}
647
648// StopPoll stops a poll and returns the result.
649func (bot *BotAPI) StopPoll(config StopPollConfig) (Poll, error) {
650 resp, err := bot.Request(config)
651 if err != nil {
652 return Poll{}, err
653 }
654
655 var poll Poll
656 err = json.Unmarshal(resp.Result, &poll)
657
658 return poll, err
659}
660
661// GetMyCommands gets the currently registered commands.
662func (bot *BotAPI) GetMyCommands() ([]BotCommand, error) {
663 return bot.GetMyCommandsWithConfig(GetMyCommandsConfig{})
664}
665
666// GetMyCommandsWithConfig gets the currently registered commands with a config.
667func (bot *BotAPI) GetMyCommandsWithConfig(config GetMyCommandsConfig) ([]BotCommand, error) {
668 resp, err := bot.Request(config)
669 if err != nil {
670 return nil, err
671 }
672
673 var commands []BotCommand
674 err = json.Unmarshal(resp.Result, &commands)
675
676 return commands, err
677}
678
679// CopyMessage copy messages of any kind. The method is analogous to the method
680// forwardMessage, but the copied message doesn't have a link to the original
681// message. Returns the MessageID of the sent message on success.
682func (bot *BotAPI) CopyMessage(config CopyMessageConfig) (MessageID, error) {
683 resp, err := bot.Request(config)
684 if err != nil {
685 return MessageID{}, err
686 }
687
688 var messageID MessageID
689 err = json.Unmarshal(resp.Result, &messageID)
690
691 return messageID, err
692}
693
694// AnswerWebAppQuery sets the result of an interaction with a Web App and send a
695// corresponding message on behalf of the user to the chat from which the query originated.
696func (bot *BotAPI) AnswerWebAppQuery(config AnswerWebAppQueryConfig) (SentWebAppMessage, error) {
697 var sentWebAppMessage SentWebAppMessage
698
699 resp, err := bot.Request(config)
700 if err != nil {
701 return sentWebAppMessage, err
702 }
703
704 err = json.Unmarshal(resp.Result, &sentWebAppMessage)
705 return sentWebAppMessage, err
706}
707
708// GetMyDefaultAdministratorRights gets the current default administrator rights of the bot.
709func (bot *BotAPI) GetMyDefaultAdministratorRights(config GetMyDefaultAdministratorRightsConfig) (ChatAdministratorRights, error) {
710 var rights ChatAdministratorRights
711
712 resp, err := bot.Request(config)
713 if err != nil {
714 return rights, err
715 }
716
717 err = json.Unmarshal(resp.Result, &rights)
718 return rights, err
719}
720
721// EscapeText takes an input text and escape Telegram markup symbols.
722// In this way we can send a text without being afraid of having to escape the characters manually.
723// Note that you don't have to include the formatting style in the input text, or it will be escaped too.
724// If there is an error, an empty string will be returned.
725//
726// parseMode is the text formatting mode (ModeMarkdown, ModeMarkdownV2 or ModeHTML)
727// text is the input string that will be escaped
728func EscapeText(parseMode string, text string) string {
729 var replacer *strings.Replacer
730
731 if parseMode == ModeHTML {
732 replacer = strings.NewReplacer("<", "<", ">", ">", "&", "&")
733 } else if parseMode == ModeMarkdown {
734 replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[")
735 } else if parseMode == ModeMarkdownV2 {
736 replacer = strings.NewReplacer(
737 "_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(",
738 "\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>",
739 "#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|",
740 "\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!",
741 )
742 } else {
743 return ""
744 }
745
746 return replacer.Replace(text)
747}