-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
332 lines (285 loc) · 8.06 KB
/
api.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
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
package crare
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"os"
"time"
"github.com/3JoB/ulib/litefmt"
"github.com/3JoB/ulib/pool"
"github.com/3JoB/unsafeConvert"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"gopkg.in/crare.v1/pkg/updates"
)
// Raw lets you call any method of Bot API manually.
// It also handles API errors, so you only need to unwrap
// result field from json data.
//
// It now returns a *bytes.Buffer, which will be automatically returned to the
// pool most of the time, but if you call a method such as Raw alone that returns
// a Buffer pointer, please use the ReleaseBuffer() method to save it back to the pool.
//
// It will be automatically released when err != nil, so there is no need to release it
// again. At the same time, additional judgment is made on the null pointer in the pool.
func (b *Bot) Raw(method string, payload ...any) (*bytes.Buffer, error) {
url := b.buildUrl(method)
req, resp := b.client.Acquire()
defer b.client.Release(req, resp)
buf := pool.NewBuffer()
req.SetRequestURI(url)
req.MethodPOST()
req.SetWriter(buf)
if len(payload) > 0 && payload[0] != nil {
if err := req.WriteJson(payload[0]); err != nil {
ReleaseBuffer(buf)
return nil, wrapError(err)
}
}
if err := req.Do(); err != nil {
ReleaseBuffer(buf)
return nil, wrapError(err)
}
if b.verbose {
b.verboses(method, payload, buf)
}
// returning data as well
return buf, extractOk(buf)
}
func (b *Bot) buildUrl(method string) string {
if b.URLCache != "" {
return litefmt.Sprint(b.URLCache, method)
}
b.URLCache = litefmt.Sprint(b.URL, "/bot", b.Token, "/")
return litefmt.Sprint(b.URLCache, method)
}
func (b *Bot) sendFiles(method string, files map[string]File, params map[string]any) (*bytes.Buffer, error) {
rawFiles := make(map[string]any)
for name, f := range files {
switch {
case f.InCloud():
params[name] = f.FileID
case f.FileURL != "":
params[name] = f.FileURL
case f.OnDisk():
rawFiles[name] = f.FileLocal
case f.FileReader != nil:
rawFiles[name] = f.FileReader
default:
return nil, fmt.Errorf("crare: file for field %s doesn't exist", name)
}
}
if len(rawFiles) == 0 {
return b.Raw(method, params)
}
pipeReader, pipeWriter := io.Pipe()
writer := multipart.NewWriter(pipeWriter)
go func() {
defer pipeWriter.Close()
for field, file := range rawFiles {
if err := addFileToWriter(writer, files[field].fileName, field, file); err != nil {
pipeWriter.CloseWithError(err) //nolint:errcheck
return
}
}
for field, value := range params {
if err := writer.WriteField(field, value.(string)); err != nil {
pipeWriter.CloseWithError(err) //nolint:errcheck
return
}
}
if err := writer.Close(); err != nil {
pipeWriter.CloseWithError(err) //nolint:errcheck
return
}
}()
url := b.buildUrl(method)
req, resp := b.client.Acquire()
defer b.client.Release(req, resp)
req.SetRequestURI(url)
buf := pool.NewBuffer()
if err := req.WriteFile(writer.FormDataContentType(), pipeReader); err != nil {
err = wrapError(err)
pipeReader.CloseWithError(err) //nolint:errcheck
ReleaseBuffer(buf)
return nil, err
}
if err := req.Do(); err != nil {
err = wrapError(err)
pipeReader.CloseWithError(err) //nolint:errcheck
ReleaseBuffer(buf)
return nil, err
}
defer b.client.ReleaseResponse(resp)
if resp.IsStatusCode(500) {
return nil, ErrInternal
}
return buf, extractOk(buf)
}
func addFileToWriter(writer *multipart.Writer, filename, field string, file any) error {
var reader io.Reader
switch r := file.(type) {
case io.Reader:
reader = r
case string:
f, err := os.Open(r)
if err != nil {
return err
}
defer f.Close()
reader = f
default:
return fmt.Errorf("crare: file for field %v should be io.ReadCloser or string", field)
}
part, err := writer.CreateFormFile(field, filename)
if err != nil {
return err
}
_, err = io.Copy(part, reader)
return err
}
func (b *Bot) sendText(to Recipient, text string, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"text": text,
}
b.embedSendOptions(params, opt)
data, err := b.Raw("sendMessage", params)
if err != nil {
return nil, err
}
return extractMessage(data)
}
func (b *Bot) sendMedia(media Media, params map[string]any, files map[string]File) (*Message, error) {
kind := media.MediaType()
what := litefmt.Sprint("send", cases.Title(language.English).String(kind))
if kind == "videoNote" {
kind = "video_note"
}
sendFiles := map[string]File{kind: *media.MediaFile()}
for k, v := range files {
sendFiles[k] = v
}
data, err := b.sendFiles(what, sendFiles, params)
if err != nil {
return nil, err
}
return extractMessage(data)
}
func (b *Bot) getMe() (*User, error) {
data, err := b.Raw("getMe")
if err != nil {
return nil, err
}
defer ReleaseBuffer(data)
var resp Response[*User]
if err := b.json.NewEncoder(data).Encode(&resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}
func (b *Bot) getUpdates(offset, limit int, timeout time.Duration, allowed []string) ([]Update, error) {
params := updates.AcquireParams()
params.Offset = offset
params.Timeout = int(timeout / time.Second)
params.AllowedUpdates = allowed
if limit != 0 {
params.Limit = limit
}
data, err := b.Raw("getUpdates", params)
if err != nil {
updates.ReleaseParams(params)
return nil, err
}
updates.ReleaseParams(params)
defer ReleaseBuffer(data)
var resp Response[[]Update]
if err := b.json.NewDecoder(data).Decode(&resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}
type extracts struct {
Ok bool `json:"ok"`
Code int `json:"error_code"`
Description string `json:"description"`
Parameters map[string]float64 `json:"parameters"`
}
// extractOk checks given result for error. If result is ok returns nil.
// In other cases it extracts API error. If error is not presented
// in errors.go, it will be prefixed with `unknown` keyword.
func extractOk(data *bytes.Buffer) error {
var e extracts
if err := defaultJson.Unmarshal(data.Bytes(), &e); err != nil {
ReleaseBuffer(data)
return err
}
if e.Ok {
return nil
}
ReleaseBuffer(data)
err := Err(e.Description)
switch err {
case nil:
case ErrGroupMigrated:
migratedTo, ok := e.Parameters["migrate_to_chat_id"]
if !ok {
return NewError(e.Code, e.Description)
}
return GroupError{
err: err.(*Error),
MigratedTo: int64(migratedTo),
}
default:
return err
}
if e.Code == 429 {
retryAfter, ok := e.Parameters["retry_after"]
if !ok {
return NewError(e.Code, e.Description)
}
err = FloodError{
err: NewError(e.Code, e.Description),
RetryAfter: int(retryAfter),
}
} else {
err = fmt.Errorf("telegram: %s (%d)", e.Description, e.Code)
}
return err
}
// extractMessage extracts common Message result from given data.
// Should be called after extractOk or b.Raw() to handle possible errors.
//
// This method will automatically release the incoming Buffer
func extractMessage(data *bytes.Buffer) (*Message, error) {
defer ReleaseBuffer(data)
var resp Response[*Message]
if err := defaultJson.Unmarshal(data.Bytes(), &resp); err != nil {
var resp Response[bool]
if err := defaultJson.NewDecoder(data).Decode(&resp); err != nil {
return nil, wrapError(err)
}
if resp.Result {
return nil, ErrTrueResult
}
return nil, wrapError(err)
}
return resp.Result, nil
}
func indent(b []byte) string {
buf := pool.NewBuffer()
defer ReleaseBuffer(buf)
_ = defaultJson.Indent(buf, b, "", " ")
return buf.String()
}
func (b *Bot) verboses(method string, payload any, data *bytes.Buffer) {
body, _ := defaultJson.Marshal(payload)
body = bytes.ReplaceAll(body, unsafeConvert.BytePointer(`\"`), unsafeConvert.BytePointer(`"`))
body = bytes.ReplaceAll(body, unsafeConvert.BytePointer(`"{`), unsafeConvert.BytePointer(`{`))
body = bytes.ReplaceAll(body, unsafeConvert.BytePointer(`}"`), unsafeConvert.BytePointer(`}`))
b.logger.Printf(
"[verbose] crare: sent request\nMethod: %v\nParams: %v\nResponse: %v",
method, indent(body), indent(data.Bytes()),
)
}