-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi.go
117 lines (95 loc) · 2.67 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
package tgo
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/http"
)
const TelegramHost = "https://api.telegram.org"
type httpResponse[T any] struct {
OK bool `json:"ok"`
Result T `json:"result,omitempty"`
Error
}
// API is a telegram bot API client instance.
type API struct {
host string
token string
client *http.Client
}
// NewAPI creates a new instance of the Telegram API client.
// It takes a token, an optional host (default is "https://api.telegram.org"),
// and an optional http.Client (default is a new http.Client instance).
// It returns a pointer to the API struct.
func NewAPI(token, host string, client *http.Client) *API {
if host == "" {
host = TelegramHost
}
if client == nil {
client = &http.Client{}
}
return &API{
host: host,
token: token,
client: client,
}
}
// Download downloads a file from the Telegram server.
// It takes the filePath obtained from GetFile and returns an http.Response
// and an error. Please note that the filePath is not the same as the fileID.
func (api *API) Download(filePath string) (*http.Response, error) {
return api.client.Get(api.host + "/file/bot" + api.token + "/" + filePath)
}
func callJson[T any](a *API, method string, rawData any) (T, error) {
var response httpResponse[T]
body := bytes.NewBuffer(nil)
if err := json.NewEncoder(body).Encode(rawData); err != nil {
return response.Result, err
}
resp, err := a.client.Post(a.host+"/bot"+a.token+"/"+method, "application/json", body)
if err != nil {
return response.Result, err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return response.Result, err
} else if !response.OK {
return response.Result, response.Error
}
return response.Result, nil
}
func callMultipart[T any](a *API, method string, params map[string]string, files map[string]*InputFile) (T, error) {
r, w := io.Pipe()
defer r.Close()
m := multipart.NewWriter(w)
go func() {
defer w.Close()
defer m.Close()
for key, val := range params {
m.WriteField(key, val)
}
for key, file := range files {
ww, err := m.CreateFormFile(key, file.Value)
if err != nil {
w.CloseWithError(err)
return
} else if _, err = io.Copy(ww, file.Reader); err != nil {
w.CloseWithError(err)
return
}
}
}()
var response httpResponse[T]
resp, err := a.client.Post(a.host+"/bot"+a.token+"/"+method, m.FormDataContentType(), r)
if err != nil {
return response.Result, nil
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return response.Result, err
} else if !response.OK {
return response.Result, response.Error
}
return response.Result, nil
}