-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtggateway.go
53 lines (43 loc) · 961 Bytes
/
tggateway.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
package tggateway
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
)
type Client struct {
token string
httpClient *http.Client
}
func NewClient(token string) Client {
return Client{
token: token,
httpClient: http.DefaultClient,
}
}
func (c Client) makeAPIRequest(ctx context.Context, endpoint string, body any, result any) error {
jsonData, err := json.Marshal(body)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, "POST", "https://gatewayapi.telegram.org/"+endpoint, bytes.NewBuffer(jsonData))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.token)
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if err := json.Unmarshal(respBody, &result); err != nil {
return err
}
return nil
}