/
Bot.go
238 lines (197 loc) · 5.92 KB
/
Bot.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package ext
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"go.uber.org/zap"
"github.com/pkg/errors"
)
type Bot struct {
Token string
Id int
FirstName string
UserName string
Logger *zap.SugaredLogger `json:"-"`
DisableWebPreview bool
AllowSendingWithoutReply bool
Requester
}
type BotCommand struct {
Command string `json:"command"`
Description string `json:"description"`
}
type Webhook struct {
Serve string // base url to where you listen
ServePath string // path you listen to
ServePort int // port you listen on
URL string // where you set the webhook to send to
// CertPath string // TODO
IPAddress string // where you set the webhook to send to
MaxConnections int // max connections; max 100, default 40
AllowedUpdates []string // which updates to allow
DropPendingUpdates bool // should start from scratch on the new updates
}
func (w Webhook) GetListenUrl() string {
if w.Serve == "" {
w.Serve = "0.0.0.0"
}
if w.ServePort == 0 {
w.ServePort = 443
}
return fmt.Sprintf("%s:%d", w.Serve, w.ServePort)
}
func NewBot(l *zap.Logger, token string) (*Bot, error) {
user, err := Bot{
Token: token,
Logger: l.Sugar(),
// getMe often times out, so add a large 5 second timeout for lots of leeway
Requester: BaseRequester{Client: http.Client{Timeout: time.Second * 5}},
}.GetMe()
if err != nil {
return nil, errors.Wrapf(err, "unable to create new bot")
}
return &Bot{
Token: token,
Id: user.Id,
FirstName: user.FirstName,
UserName: user.Username,
Logger: l.Sugar(),
Requester: DefaultTgBotRequester,
}, nil
}
// Get -> convenience function to execute commands against the TG API.
func (b Bot) Get(method string, params url.Values) (json.RawMessage, error) {
return b.Requester.Get(b.Logger, b.Token, method, params)
}
// Post -> convenience function to execute commands against the TG API.
// the "data" map is used to fill out the multipart form data to send to TG.
func (b Bot) Post(method string, params url.Values, data map[string]PostFile) (json.RawMessage, error) {
return b.Requester.Post(b.Logger, b.Token, method, params, data)
}
// GetMe gets the bot info
func (b Bot) GetMe() (*User, error) {
v := url.Values{}
r, err := b.Get("getMe", v)
if err != nil {
return nil, err
}
var u User
return &u, json.Unmarshal(r, &u)
}
func (b Bot) LogOut() (bool, error) {
return b.boolSender("logOut", nil)
}
func (b Bot) Close() (bool, error) {
return b.boolSender("close", nil)
}
// GetMyCommands gets the list of bot commands assigned to the bot.
func (b Bot) GetMyCommands() ([]BotCommand, error) {
v := url.Values{}
r, err := b.Get("getMyCommands", v)
if err != nil {
return nil, err
}
var bc []BotCommand
return bc, json.Unmarshal(r, &bc)
}
// SetMyCommands gets the list of bot commands assigned to the bot.
func (b Bot) SetMyCommands(cmds []BotCommand) (bool, error) {
if cmds == nil {
cmds = []BotCommand{}
}
v := url.Values{}
cmdBytes, err := json.Marshal(cmds)
if err != nil {
return false, errors.Wrapf(err, "failed to marshal commands")
}
v.Add("commands", string(cmdBytes))
return b.boolSender("setMyCommands", v)
}
// GetUserProfilePhotos Retrieves a user's profile pictures
func (b Bot) GetUserProfilePhotos(userId int, offset int, limit int) (*UserProfilePhotos, error) {
v := url.Values{}
v.Add("user_id", strconv.Itoa(userId))
if offset != 0 {
v.Add("offset", strconv.Itoa(offset))
}
if limit != 0 {
v.Add("limit", strconv.Itoa(limit))
}
r, err := b.Get("getUserProfilePhotos", v)
if err != nil {
return nil, err
}
var userProfilePhotos UserProfilePhotos
return &userProfilePhotos, json.Unmarshal(r, &userProfilePhotos)
}
// GetFile Retrieve a file from the bot api
func (b Bot) GetFile(fileId string) (*File, error) {
v := url.Values{}
v.Add("file_id", fileId)
r, err := b.Get("getFile", v)
if err != nil {
return nil, err
}
var f File
return &f, json.Unmarshal(r, &f)
}
type WebhookInfo struct {
URL string `json:"url"`
HasCustomCertificate bool `json:"has_custom_certificate"`
PendingUpdateCount int `json:"pending_update_count"`
IPAddress string `json:"ip_address"`
LastErrorDate int `json:"last_error_date"`
LastErrorMessage int `json:"last_error_message"`
MaxConnections int `json:"max_connections"`
AllowedUpdates []string `json:"allowed_updates"`
}
// GetWebhookInfo Get webhook info from telegram servers
func (b Bot) GetWebhookInfo() (*WebhookInfo, error) {
r, err := b.Get("getWebhookInfo", nil)
if err != nil {
return nil, err
}
var wh WebhookInfo
return &wh, json.Unmarshal(r, &wh)
}
// SetWebhook Set the webhook url for telegram to contact with updates
func (b Bot) SetWebhook(path string, webhook Webhook) (bool, error) {
allowedUpdates := webhook.AllowedUpdates
if allowedUpdates == nil {
allowedUpdates = []string{}
}
allowed, err := json.Marshal(allowedUpdates)
if err != nil {
return false, errors.Wrap(err, "cannot marshal allowedUpdates")
}
v := url.Values{}
v.Add("url", strings.TrimSuffix(webhook.URL, "/")+"/"+strings.TrimPrefix(path, "/"))
// v.Add("certificate", ) // todo: add certificate support
v.Add("ip_address", webhook.IPAddress)
v.Add("max_connections", strconv.Itoa(webhook.MaxConnections))
v.Add("allowed_updates", string(allowed))
v.Add("drop_pending_updates", strconv.FormatBool(webhook.DropPendingUpdates))
r, err := b.Get("setWebhook", v)
if err != nil {
return false, err
}
var bb bool
return bb, json.Unmarshal(r, &bb)
}
func (b Bot) DeleteWebhook() (bool, error) {
return b.DeleteWebhookDrop(false)
}
func (b Bot) DeleteWebhookDrop(dropUpdates bool) (bool, error) {
v := url.Values{}
v.Add("drop_pending_updates", strconv.FormatBool(dropUpdates))
r, err := b.Get("deleteWebhook", v)
if err != nil {
return false, err
}
var bb bool
return bb, json.Unmarshal(r, &bb)
}