-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtelegrambot.go
201 lines (160 loc) · 4.88 KB
/
telegrambot.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
package telegrambot
// https://core.telegram.org/bots/api
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
)
// URL of official Telegram Bot API endpoint
const DefaultAPIEndpointURL = "https://api.telegram.org/bot"
var defaultFasthttpClient = &fasthttp.Client{
NoDefaultUserAgentHeader: true,
DisableHeaderNamesNormalizing: true,
DisablePathNormalizing: true,
}
// Default function for performing http requests by API
func DefaultHttpDoRequest(method string, url string, headers map[string]string, body []byte) (respBody []byte, err error) {
req := &fasthttp.Request{}
req.Header.SetMethod(method)
req.SetRequestURI(url)
for k, v := range headers {
req.Header.Set(k, v)
}
req.SetBody(body)
resp := &fasthttp.Response{}
err = defaultFasthttpClient.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("DefaultHttpDoRequest: %w", err)
}
respBody = resp.Body()
return respBody, nil
}
// Main object in this library, for performing Telegram Bot API requests
type API struct {
Token string
EndpointURL string
HttpDoRequest func(method string, url string, headers map[string]string, body []byte) (respBody []byte, err error)
}
// Creates Telegram Bot API interface instance. If you want to customize http
// requests behavior or api endpoint url (e.x. use local instance), then
// instance API struct directly
//
// Check code of this function, if you want to create API with custom parameters
func NewAPI(token string) (*API, *User, error) {
api := &API{
Token: token,
EndpointURL: DefaultAPIEndpointURL,
HttpDoRequest: DefaultHttpDoRequest,
}
user, err := api.GetMe()
if err != nil {
return nil, nil, fmt.Errorf("NewAPI: %w", err)
}
return api, user, nil
}
// Response on API request. Used internally by this library.
type Response struct {
OK bool `json:"ok"`
ErrorCode int `json:"error_code,omitempty"`
Description string `json:"description,omitempty"`
Parameters *ResponseParameters `json:"parameters,omitempty"`
Result any `json:"result,omitempty"`
}
func (api *API) makeAPICall(method string, requestData any, inputFiles []InputFile, resultDest any) (migrateToChatID ChatID, err error) {
var (
reqURL = api.EndpointURL + api.Token + "/" + method
reqContentType string
reqBody []byte
)
jsoniterCfg := jsoniter.Config{
OnlyTaggedField: true,
ObjectFieldMustBeSimpleString: true,
CaseSensitive: true,
}.Froze()
requestDataJSON, err := jsoniterCfg.Marshal(requestData)
if err != nil {
return 0, fmt.Errorf("makeAPICall: %w", err)
}
if inputFilesToUpload := filterInputFilesNeedingUpload(inputFiles); len(inputFilesToUpload) == 0 {
reqContentType = "application/json"
reqBody = requestDataJSON
} else {
reqBodyBuf := bytes.NewBuffer(nil)
mw := multipart.NewWriter(reqBodyBuf)
var err error
iter := jsoniterCfg.BorrowIterator(requestDataJSON)
iter.ReadMapCB(func(i *jsoniter.Iterator, s string) bool {
err = mw.WriteField(s, i.ReadAny().ToString())
return err == nil
})
if err != nil {
return 0, fmt.Errorf("makeAPICall: %w", err)
}
for _, inputFile := range inputFilesToUpload {
fieldname, filename, reader := inputFile.multipartFormFile()
filew, err := mw.CreateFormFile(fieldname, filename)
if err != nil {
return 0, fmt.Errorf("makeAPICall: %w", err)
}
_, err = io.Copy(filew, reader)
if err != nil {
return 0, fmt.Errorf("makeAPICall: %w", err)
}
}
err = mw.Close()
if err != nil {
return 0, fmt.Errorf("makeAPICall: %w", err)
}
reqContentType = mw.FormDataContentType()
reqBody = reqBodyBuf.Bytes()
}
loop:
for {
respBody, err := api.HttpDoRequest("POST", reqURL, map[string]string{
"Content-Type": reqContentType,
}, reqBody)
if err != nil {
return 0, fmt.Errorf("makeAPICall: %w", err)
}
apiResp := &Response{
Result: resultDest,
}
err = jsoniterCfg.Unmarshal(respBody, apiResp)
if err != nil {
return 0, fmt.Errorf("makeAPICall: %w", err)
}
if !apiResp.OK {
if apiRespParams := apiResp.Parameters; apiRespParams != nil {
switch {
case apiRespParams.MigrateToChatID != 0:
return apiRespParams.MigrateToChatID, nil
case apiRespParams.RetryAfter != 0:
time.Sleep(time.Second * time.Duration(apiRespParams.RetryAfter))
continue loop
}
}
return 0, fmt.Errorf("makeAPICall - telegram bot api error: %w", errors.New(apiResp.Description))
}
return 0, nil
}
}
func filterInputFilesNeedingUpload(inputFiles []InputFile) []InputFile {
if inputFiles == nil {
return nil
}
filteredInputFiles := []InputFile{}
for _, inputFile := range inputFiles {
if inputFile == nil {
continue
}
if _, _, reader := inputFile.multipartFormFile(); reader != nil {
filteredInputFiles = append(filteredInputFiles, inputFile)
}
}
return filteredInputFiles
}