forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
164 lines (133 loc) · 3.36 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
package telebot
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
)
func sendCommand(method string, token string, params url.Values) ([]byte, error) {
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s?%s",
token, method, params.Encode())
resp, err := http.Get(url)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
json, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
return json, nil
}
func sendFile(method, token, name, path string, params url.Values) ([]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, values := range params {
if len(values) > 0 {
writer.WriteField(field, values[0])
}
}
if err = writer.Close(); err != nil {
return []byte{}, err
}
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", 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("Telegram API returned 500 internal server error")
}
json, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
return json, nil
}
func embedSendOptions(params *url.Values, options *SendOptions) {
if params == nil || options == nil {
return
}
if options.ReplyTo.ID != 0 {
params.Set("reply_to_message_id", strconv.Itoa(options.ReplyTo.ID))
}
if options.DisableWebPagePreview {
params.Set("disable_web_page_preview", "true")
}
if options.ParseMode != ModeDefault {
params.Set("parse_mode", string(options.ParseMode))
}
// process reply_markup
if options.ReplyMarkup.ForceReply || options.ReplyMarkup.CustomKeyboard != nil || options.ReplyMarkup.HideCustomKeyboard {
replyMarkup, _ := json.Marshal(options.ReplyMarkup)
params.Set("reply_markup", string(replyMarkup))
}
}
func getMe(token string) (User, error) {
meJSON, err := sendCommand("getMe", token, url.Values{})
if err != nil {
return User{}, err
}
var botInfo struct {
Ok bool
Result User
Description string
}
err = json.Unmarshal(meJSON, &botInfo)
if err != nil {
return User{}, fmt.Errorf("telebot: invalid token")
}
if botInfo.Ok {
return botInfo.Result, nil
}
return User{}, fmt.Errorf("telebot: %s", botInfo.Description)
}
func getUpdates(token string, offset int, timeout int) (updates []Update, err error) {
params := url.Values{}
params.Set("offset", strconv.Itoa(offset))
params.Set("timeout", strconv.Itoa(timeout))
updatesJSON, err := sendCommand("getUpdates", token, params)
if err != nil {
return
}
var updatesRecieved struct {
Ok bool
Result []Update
Description string
}
err = json.Unmarshal(updatesJSON, &updatesRecieved)
if err != nil {
return
}
if !updatesRecieved.Ok {
err = fmt.Errorf("telebot: %s", updatesRecieved.Description)
return
}
updates = updatesRecieved.Result
return
}