-
Notifications
You must be signed in to change notification settings - Fork 1
/
ted.go
161 lines (144 loc) · 3.77 KB
/
ted.go
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
package ted
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
type Request interface {
doWith(bot Bot) (Response, error)
}
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type Bot struct {
Token string
HTTPClient HTTPClient
}
func (b Bot) Do(request Request) (Response, error) {
return request.doWith(b)
}
type MultiError []error
func (m MultiError) Error() string {
total := len(m)
errored := 0
for _, err := range m {
if err != nil {
errored++
}
}
return fmt.Sprintf("%d out of %d requests were unsuccessful", errored, total)
}
func (b Bot) DoMulti(requests ...Request) ([]Response, error) {
responses := make([]Response, len(requests))
errs := make([]error, len(requests))
var wg sync.WaitGroup
wg.Add(len(requests))
for i, request := range requests {
go func(i int, req Request) {
res, err := b.Do(req)
if err != nil {
errs[i] = err
} else {
responses[i] = res
}
wg.Done()
}(i, request)
}
for _, err := range errs {
if err != nil {
return nil, MultiError(errs)
}
}
return responses, nil
}
type Response struct {
OK bool `json:"ok"`
Result json.RawMessage `json:"result"`
ErrorCode int `json:"error_code"`
Description string `json:"description"`
Parameters *ResponseParameters `json:"parameters"`
}
// ResponseParameters contains information about why a request was unsuccessful.
type ResponseParameters struct {
// Optional. The group has been migrated to a supergroup with the
// specified identifier.
MigrateToChatID int64 `json:"migrate_to_chat_id"`
// Optional. In case of exceeding flood control, the number of seconds left to wait before the request can be repeated
RetryAfter int `json:"retry_after"`
}
func (r Response) Error() string {
return r.Description
}
type temporaryError interface {
Temporary() bool
}
// doReq makes the provided http.Request to the Telegram Bot API.
// Currently hardcoded to retry up to 3 times if the error is retryable (implements Temporary() bool).
func (b Bot) doReq(req *http.Request) (Response, error) {
retries := 3
var res *http.Response
var err error
for {
res, err = b.HTTPClient.Do(req)
if err != nil {
if terr, ok := err.(temporaryError); ok && terr.Temporary() && retries > 0 {
retries--
time.Sleep(1 * time.Second)
continue
}
return Response{}, err
}
break
}
defer res.Body.Close()
var response Response
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return Response{}, err
}
if !response.OK {
return Response{}, response
}
return response, nil
}
// doQuery makes a GET request to the Telegram Bot API with URL query parameters.
func (b Bot) doQuery(method string, params map[string]interface{}) (Response, error) {
form := url.Values{}
for k, v := range params {
form.Set(k, fmt.Sprintf("%v", v))
}
u := fmt.Sprintf("https://api.telegram.org/bot%s/%s", b.Token, method)
req, err := http.NewRequest(http.MethodPost, u, nil)
if err != nil {
return Response{}, err
}
req.URL.RawQuery = form.Encode()
return b.doReq(req)
}
// doJSON makes a POST request to the Telegram Bot API with a JSON body.
func (b Bot) doJSON(method string, request interface{}) (Response, error) {
u := fmt.Sprintf("https://api.telegram.org/bot%s/%s", b.Token, method)
var body bytes.Buffer
err := json.NewEncoder(&body).Encode(request)
if err != nil {
return Response{}, err
}
req, err := http.NewRequest(http.MethodPost, u, &body)
if err != nil {
return Response{}, err
}
req.Header.Set("Content-Type", "application/json")
return b.doReq(req)
}
func IsMessageNotModified(err error) bool {
res, ok := err.(Response)
if !ok {
return false
}
return strings.Contains(res.Description, "message is not modified")
}