-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathapi.go
More file actions
629 lines (540 loc) · 15.3 KB
/
api.go
File metadata and controls
629 lines (540 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
package telegram
// Package telegram provides implementation for Telegram Bot API
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/url"
"strings"
"golang.org/x/net/context"
)
const (
// APIEndpoint is the endpoint for all API methods,
// with formatting for Sprintf.
APIEndpoint = "https://api.telegram.org/bot%s/%s"
// FileEndpoint is the endpoint for downloading a file from Telegram.
FileEndpoint = "https://api.telegram.org/file/bot%s/%s"
)
// HTTPDoer interface helps to test api
type HTTPDoer interface {
Do(*http.Request) (*http.Response, error)
}
// DebugFunc describes function for debugging.
type DebugFunc func(msg string, fields map[string]interface{})
// DefaultDebugFunc prints debug message to default logger
var DefaultDebugFunc = func(msg string, fields map[string]interface{}) {
log.Printf("%s %v", msg, fields)
}
// API implements Telegram bot API
// described on https://core.telegram.org/bots/api
type API struct {
// token is a unique authentication string,
// obtained by each bot when it is created.
// The token looks something like
// 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
token string
client HTTPDoer
apiEndpoint string
fileEndpoint string
debug bool
debugFunc DebugFunc
}
// New returns API instance with default http client
func New(token string) *API {
return NewWithClient(token, http.DefaultClient)
}
// NewWithClient returns API instance with custom http client
func NewWithClient(token string, client HTTPDoer) *API {
return &API{
token: token,
client: client,
apiEndpoint: APIEndpoint,
fileEndpoint: FileEndpoint,
debugFunc: DefaultDebugFunc,
}
}
// Invoke is a generic method that helps to make request to Telegram Api.
// Use particular methods instead (e.x. GetMe, GetUpdates etc).
// The only case when this method seems useful is
// when Telegram Api has method
// that still doesn't exist in this implementation.
func (c *API) Invoke(ctx context.Context, m Method, dst interface{}) error {
params, err := m.Values()
if err != nil {
return err
}
var req *http.Request
if mf, casted := m.(Filer); casted && !mf.Exist() {
// upload a file, if FileID doesn't exist
req, err = c.getUploadRequest(
m.Name(),
params,
mf.Field(),
mf.File(),
)
} else {
req, err = c.getFormRequest(m.Name(), params)
}
if err != nil {
return err
}
return c.makeRequest(ctx, req, dst)
}
// Debug enables sending debug messages to default log
func (c *API) Debug(val bool) {
c.debug = val
}
// DebugFunc replaces default debug function
func (c *API) DebugFunc(f DebugFunc) {
c.debugFunc = f
}
// Telegram Bot API methods
// GetMe returns basic information about the bot in form of a User object
func (c *API) GetMe(ctx context.Context) (*User, error) {
u := &User{}
if err := c.Invoke(ctx, MeCfg{}, u); err != nil {
return nil, err
}
return u, nil
}
// GetChat returns up to date information about the chat
// (current name of the user for one-on-one conversations,
// current username of a user, group or channel, etc.).
// Returns a Chat object on success.
func (c *API) GetChat(ctx context.Context, cfg GetChatCfg) (*Chat, error) {
chat := &Chat{}
if err := c.Invoke(ctx, cfg, chat); err != nil {
return nil, err
}
return chat, nil
}
// GetChatAdministrators returns a list of administrators in a chat.
// On success, returns an Array of ChatMember objects
// that contains information about all chat administrators
// except other bots. If the chat is a group or a supergroup
// and no administrators were appointed, only the creator will be returned.
func (c *API) GetChatAdministrators(
ctx context.Context,
cfg GetChatAdministratorsCfg) ([]ChatMember, error) {
chatMembers := []ChatMember{}
if err := c.Invoke(ctx, cfg, &chatMembers); err != nil {
return nil, err
}
return chatMembers, nil
}
// GetChatMembersCount returns the number of members in a chat.
func (c *API) GetChatMembersCount(
ctx context.Context,
cfg GetChatMembersCountCfg) (int, error) {
var count int
if err := c.Invoke(ctx, cfg, &count); err != nil {
return count, err
}
return count, nil
}
// GetChatMember returns information about a member of a chat.
func (c *API) GetChatMember(
ctx context.Context,
cfg GetChatMemberCfg) (*ChatMember, error) {
member := &ChatMember{}
if err := c.Invoke(ctx, cfg, member); err != nil {
return nil, err
}
return member, nil
}
// KickChatMember kicks a user from a group or a supergroup.
// In the case of supergroups, the user will not be able to return
// to the group on their own using invite links, etc., unless unbanned first.
// The bot must be an administrator in the group for this to work.
// Returns True on success.
func (c *API) KickChatMember(
ctx context.Context,
cfg KickChatMemberCfg) (bool, error) {
var result bool
if err := c.Invoke(ctx, cfg, &result); err != nil {
return result, err
}
return result, nil
}
// UnbanChatMember unbans a previously kicked user in a supergroup.
// The user will not return to the group automatically,
// but will be able to join via link, etc.
// The bot must be an administrator in the group for this to work.
// Returns True on success.
func (c *API) UnbanChatMember(
ctx context.Context,
cfg UnbanChatMemberCfg) (bool, error) {
var result bool
if err := c.Invoke(ctx, cfg, &result); err != nil {
return result, err
}
return result, nil
}
// LeaveChat method helps your bot to leave a group, supergroup or channel.
// Returns True on success.
func (c *API) LeaveChat(
ctx context.Context,
cfg LeaveChatCfg) (bool, error) {
var result bool
if err := c.Invoke(ctx, cfg, &result); err != nil {
return result, err
}
return result, nil
}
// GetUpdates requests incoming updates using long polling.
// This method will not work if an outgoing webhook is set up.
// In order to avoid getting duplicate updates,
// recalculate offset after each server response.
func (c *API) GetUpdates(
ctx context.Context,
cfg UpdateCfg) ([]Update, error) {
updates := []Update{}
if err := c.Invoke(ctx, cfg, &updates); err != nil {
return nil, err
}
return updates, nil
}
// GetUserProfilePhotos requests a list of profile pictures for a user.
func (c *API) GetUserProfilePhotos(
ctx context.Context,
cfg UserProfilePhotosCfg) (*UserProfilePhotos, error) {
photos := &UserProfilePhotos{}
if err := c.Invoke(ctx, cfg, photos); err != nil {
return nil, err
}
return photos, nil
}
// SendChatAction tells the user that something is happening
// on the bot's side. The status is set for 5 seconds or less
// (when a message arrives from your bot,
// Telegram clients clear its typing status).
func (c *API) SendChatAction(ctx context.Context, cfg ChatActionCfg) error {
return c.Invoke(ctx, cfg, nil)
}
// GetFile returns a File which can download a file from Telegram.
//
// Requires FileID.
func (c *API) GetFile(ctx context.Context, cfg FileCfg) (*File, error) {
var file File
err := c.Invoke(ctx, cfg, &file)
if err != nil {
return nil, err
}
file.Link = fmt.Sprintf(c.fileEndpoint, c.token, file.FilePath)
return &file, nil
}
// DownloadFile downloads file from telegram servers to w
//
// Requires FileID
func (c *API) DownloadFile(ctx context.Context, cfg FileCfg, w io.Writer) error {
f, err := c.GetFile(ctx, cfg)
if err != nil {
return err
}
req, err := http.NewRequest("GET", f.Link, nil)
if err != nil {
return err
}
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil {
if c.debug {
c.print("body close error", map[string]interface{}{
"error": err.Error(),
})
}
}
}()
_, err = io.Copy(w, resp.Body)
if err != nil {
return err
}
return nil
}
// AnswerCallbackQuery sends a response to an inline query callback.
func (c *API) AnswerCallbackQuery(
ctx context.Context,
cfg AnswerCallbackCfg) (bool, error) {
var result bool
return result, c.Invoke(ctx, cfg, &result)
}
// Edit method allows you to change an existing message in the message history
// instead of sending a new one with a result of an action.
// This is most useful for messages with inline keyboards using callback queries,
// but can also help reduce clutter in conversations with regular chat bots.
// Please note, that it is currently only possible to edit messages without
// reply_markup or with inline keyboards.
//
// You can use this method directly or one of:
// EditMessageText, EditMessageCaption, EditMessageReplyMarkup,
func (c *API) Edit(ctx context.Context, cfg Method) (*EditResult, error) {
er := &EditResult{}
return er, c.Invoke(ctx, cfg, er)
}
// Send method sends message.
//
// TODO m0sth8: rewrite this doc
func (c *API) Send(ctx context.Context, cfg Messenger) (*Message, error) {
msg := cfg.Message()
return msg, c.Invoke(ctx, cfg, msg)
}
// === Methods based on Send method
// SendMessage sends text message.
func (c *API) SendMessage(
ctx context.Context,
cfg MessageCfg) (*Message, error) {
return c.Send(ctx, cfg)
}
// SendSticker sends message with sticker.
func (c *API) SendSticker(
ctx context.Context,
cfg StickerCfg) (*Message, error) {
return c.Send(ctx, cfg)
}
// SendVenue sends venue message.
func (c *API) SendVenue(
ctx context.Context,
cfg VenueCfg) (*Message, error) {
return c.Send(ctx, cfg)
}
// SendContact sends phone contact message.
func (c *API) SendContact(
ctx context.Context,
cfg ContactCfg) (*Message, error) {
return c.Send(ctx, cfg)
}
// SendPhoto sends photo.
func (c *API) SendPhoto(
ctx context.Context,
cfg PhotoCfg) (*Message, error) {
return c.Send(ctx, cfg)
}
// SendAudio sends Audio.
func (c *API) SendAudio(
ctx context.Context,
cfg AudioCfg) (*Message, error) {
return c.Send(ctx, cfg)
}
// SendVideo sends Video.
func (c *API) SendVideo(
ctx context.Context,
cfg VideoCfg) (*Message, error) {
return c.Send(ctx, cfg)
}
// SendVoice sends Voice.
func (c *API) SendVoice(
ctx context.Context,
cfg VoiceCfg) (*Message, error) {
return c.Send(ctx, cfg)
}
// SendDocument sends Document.
func (c *API) SendDocument(
ctx context.Context,
cfg DocumentCfg) (*Message, error) {
return c.Send(ctx, cfg)
}
// ForwardMessage forwards messages of any kind.
func (c *API) ForwardMessage(
ctx context.Context,
cfg ForwardMessageCfg) (*Message, error) {
return c.Send(ctx, cfg)
}
// === Methods based on Edit method
// EditMessageText modifies the text of message.
// Use this method to edit only the text of messages
// sent by the bot or via the bot (for inline bots).
// On success, if edited message is sent by the bot,
// the edited Message is returned, otherwise True is returned.
func (c *API) EditMessageText(
ctx context.Context,
cfg EditMessageTextCfg) (*EditResult, error) {
return c.Edit(ctx, cfg)
}
// EditMessageCaption modifies the caption of message.
// Use this method to edit only the caption of messages
// sent by the bot or via the bot (for inline bots).
// On success, if edited message is sent by the bot,
// the edited Message is returned, otherwise True is returned.
func (c *API) EditMessageCaption(
ctx context.Context,
cfg EditMessageCaptionCfg) (*EditResult, error) {
return c.Edit(ctx, cfg)
}
// EditMessageReplyMarkup modifies the reply markup of message.
// Use this method to edit only the reply markup of messages
// sent by the bot or via the bot (for inline bots).
// On success, if edited message is sent by the bot,
// the edited Message is returned, otherwise True is returned.
func (c *API) EditMessageReplyMarkup(
ctx context.Context,
cfg EditMessageReplyMarkupCfg) (*EditResult, error) {
return c.Edit(ctx, cfg)
}
// SetWebhook sets a webhook.
// Use this method to specify a url and receive incoming updates
// via an outgoing webhook. Whenever there is an update for the bot,
// we will send an HTTPS POST request to the specified url,
// containing a JSON‐serialized Update.
// In case of an unsuccessful request,
// we will give up after a reasonable amount of attempts.
//
// If this is set, GetUpdates will not get any data!
//
// If you do not have a legitimate TLS certificate,
// you need to include your self signed certificate with the config.
func (c *API) SetWebhook(ctx context.Context, cfg WebhookCfg) error {
return c.Invoke(ctx, cfg, nil)
}
// AnswerInlineQuery sends answers to an inline query.
// On success, True is returned. No more than 50 results per query are allowed.
func (c *API) AnswerInlineQuery(ctx context.Context, cfg AnswerInlineQueryCfg) (bool, error) {
var result bool
return result, c.Invoke(ctx, cfg, &result)
}
// Internal methods
func (c *API) print(msg string, fields map[string]interface{}) {
if c.debugFunc != nil {
c.debugFunc(msg, fields)
}
}
func (c *API) getFormRequest(
method string,
params url.Values) (*http.Request, error) {
urlStr := fmt.Sprintf(c.apiEndpoint, c.token, method)
body := params.Encode()
if c.debug {
c.print("request", map[string]interface{}{
"url": urlStr,
"data": body,
})
}
req, err := http.NewRequest(
"POST",
urlStr,
strings.NewReader(body),
)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, nil
}
func (c *API) getUploadRequest(
method string,
params url.Values,
field string,
file InputFile) (*http.Request, error) {
urlStr := fmt.Sprintf(c.apiEndpoint, c.token, method)
if c.debug {
c.print("file request", map[string]interface{}{
"url": urlStr,
"data": params.Encode(),
"file_field": field,
"file_name": file.Name(),
})
}
buf := &bytes.Buffer{}
w := multipart.NewWriter(buf)
for key, values := range params {
for _, value := range values {
err := w.WriteField(key, value)
if err != nil {
return nil, fmt.Errorf(
"can't write field %s, cause %s",
key, err.Error(),
)
}
}
}
fw, err := w.CreateFormFile(field, file.Name())
if err != nil {
return nil, err
}
if _, err = io.Copy(fw, file.Reader()); err != nil {
return nil, err
}
if err = w.Close(); err != nil {
return nil, err
}
req, err := http.NewRequest(
"POST",
urlStr,
buf,
)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", w.FormDataContentType())
return req, nil
}
func (c *API) makeRequest(
ctx context.Context,
req *http.Request,
dst interface{}) error {
var err error
var resp *http.Response
resp, err = makeRequest(ctx, c.client, req)
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil {
if c.debug {
c.print("body close error", map[string]interface{}{
"error": err.Error(),
})
}
}
}()
if c.debug {
c.print("response", map[string]interface{}{
"status_code": resp.StatusCode,
})
}
if resp.StatusCode == http.StatusForbidden {
// read all from body to save keep-alive connection.
if _, err = io.Copy(ioutil.Discard, resp.Body); err != nil {
if c.debug {
c.print("discard error", map[string]interface{}{
"error": err.Error(),
})
}
}
return errForbidden
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if c.debug {
c.print("response", map[string]interface{}{
"data": string(data),
})
}
apiResponse := APIResponse{}
err = json.Unmarshal(data, &apiResponse)
if err != nil {
return err
}
if !apiResponse.Ok {
if apiResponse.ErrorCode == 401 {
return errUnauthorized
}
return &APIError{
Description: apiResponse.Description,
ErrorCode: apiResponse.ErrorCode,
}
}
if dst != nil && apiResponse.Result != nil {
err = json.Unmarshal(*apiResponse.Result, dst)
}
return err
}