-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
170 lines (125 loc) · 3.24 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
// tgbot-go -
// https://github.com/modern-dev/tgbot-go
// Copyright (c) 2020 Bohdan Shtepan
// Licensed under the MIT license.
package tgbot
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
const (
ApiUrl = "https://api.telegram.org/bot%s/%s"
)
func (bot *Bot) makeRequest(method string, payload interface{}) ([]byte, error) {
url := fmt.Sprintf(ApiUrl, bot.Token, method)
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(payload); err != nil {
return []byte{}, err
}
resp, err := http.Post(url, "application/json", &b)
if err != nil {
return []byte{}, err
}
resp.Close = true
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
return respBytes, nil
}
func (bot *Bot) makeFileRequest(method, token, name, path string, params map[string]string) ([]byte, error) {
file, err := os.Open(path)
if err != nil {
return []byte{}, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(name, filepath.Base(path))
if err != nil {
return []byte{}, err
}
if _, err = io.Copy(part, file); err != nil {
return []byte{}, err
}
for field, value := range params {
writer.WriteField(field, value)
}
if err = writer.Close(); err != nil {
return []byte{}, err
}
url := fmt.Sprintf(ApiUrl, token, method)
req, err := http.NewRequest("POST", url, body)
if err != nil {
return []byte{}, err
}
req.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return []byte{}, err
}
if resp.StatusCode == http.StatusInternalServerError {
return []byte{}, fmt.Errorf("internal server error")
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
return respBytes, nil
}
// GetMe is a simple method for testing your bot's auth token. Requires no parameters.
// Returns basic information about the bot in form of a User object.
func (bot *Bot) GetMe() (*User, error) {
jsonResp, err := bot.makeRequest("getMe", nil)
if err != nil {
return nil, err
}
var resp struct {
Ok bool `json:"ok"`
Result *User `json:"result"`
Description string `json:"description"`
}
if err = json.Unmarshal(jsonResp, &resp); err != nil {
return nil, err
}
if resp.Ok {
return resp.Result, nil
}
return nil, fmt.Errorf("tgbot: %s", resp.Description)
}
// SendMessage is to send text messages. On success, the sent Message is returned.
func (bot *Bot) SendMessage(chatId, text string, opts *SendMessageOptions) (*Message, error) {
params := map[string]string{
"chat_id": chatId,
"text": text,
}
if opts != nil {
opts.addOptions(params)
}
jsonResp, err := bot.makeRequest("sendMessage", params)
if err != nil {
return nil, err
}
var resp struct {
Ok bool `json:"ok"`
Result *Message `json:"result"`
Description string `json:"description"`
}
if err = json.Unmarshal(jsonResp, &resp); err != nil {
return nil, err
}
if !resp.Ok {
return nil, fmt.Errorf("tgbot: %s", resp.Description)
}
return resp.Result, nil
}
//func (bot *Bot) SendPhoto(chatID string, photo InputFile)