This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
updates.go
284 lines (218 loc) · 11.7 KB
/
updates.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package telegram
import (
"net"
"strconv"
"strings"
"time"
http "github.com/valyala/fasthttp"
)
type (
// Update represents an incoming update.
//
// At most one of the optional parameters can be present in any given update.
Update struct {
// The update‘s unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order.
ID int64 `json:"update_id"`
// New incoming message of any kind — text, photo, sticker, etc.
Message *Message `json:"message,omitempty"`
// New version of a message that is known to the bot and was edited
EditedMessage *Message `json:"edited_message,omitempty"`
// New incoming channel post of any kind — text, photo, sticker, etc.
ChannelPost *Message `json:"channel_post,omitempty"`
// New version of a channel post that is known to the bot and was edited
EditedChannelPost *Message `json:"adited_channel_post,omitempty"`
// New incoming inline query
InlineQuery *InlineQuery `json:"inline_query,omitempty"`
// The result of an inline query that was chosen by a user and sent to their chat partner.
ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result,omitempty"`
// New incoming callback query
CallbackQuery *CallbackQuery `json:"callback_query,omitempty"`
// New incoming shipping query. Only for invoices with flexible price
ShippingQuery *ShippingQuery `json:"shipping_query,omitempty"`
// New incoming pre-checkout query. Contains full information about checkout
PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query,omitempty"`
// New poll state. Bots receive only updates about polls, which are sent or stopped by the bot
Poll *Poll `json:"poll,omitempty"`
// A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were
// sent by the bot itself.
PollAnswer *PollAnswer `json:"poll_answer,omitempty"`
// The bot's chat member status was updated in a chat. For private chats, this update is received only
// when the bot is blocked or unblocked by the user.
MyChatMember *ChatMemberUpdated `json:"my_chat_member,omitempty"`
// A chat member's status was updated in a chat. The bot must be an administrator in the chat and must
// explicitly specify “chat_member” in the list of allowed_updates to receive these updates.
ChatMember *ChatMemberUpdated `json:"chat_member,omitempty"`
}
// WebhookInfo contains information about the current status of a webhook.
WebhookInfo struct {
// Webhook URL, may be empty if webhook is not set up
URL string `json:"url"`
// True, if a custom certificate was provided for webhook certificate checks
HasCustomCertificate bool `json:"has_custom_certificate"`
// Number of updates awaiting delivery
PendingUpdateCount int `json:"pending_update_count"`
// Currently used webhook IP address
IpAddress net.IP `json:"ip_address,omitempty"`
// Error message in human-readable format for the most recent error that happened when trying to
// deliver an update via webhook
LastErrorMessage string `json:"last_error_message,omitempty"`
// Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery
MaxConnections int `json:"max_connections,omitempty"`
// Unix time for the most recent error that happened when trying to deliver an update via webhook
LastErrorDate int64 `json:"last_error_date,omitempty"`
// A list of update types the bot is subscribed to. Defaults to all update types
AllowedUpdates []string `json:"allowed_updates,omitempty"`
}
// GetUpdatesParameters represents data for GetUpdates method.
GetUpdates struct {
// Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id. The negative offset can be specified to retrieve updates starting from -offset update from the end of the updates queue. All previous updates will forgotten.
Offset int64 `json:"offset,omitempty"`
// Limits the number of updates to be retrieved. Values between 1—100 are accepted. Defaults to 100.
Limit int `json:"limit,omitempty"`
// Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling. Should be positive, short polling should be used for testing purposes only.
Timeout int `json:"timeout,omitempty"`
// List the types of updates you want your bot to receive. For example, specify ["message", "edited_channel_post", "callback_query"] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all updates regardless of type (default). If not specified, the previous setting will be used.
//
// Please note that this parameter doesn't affect updates created before the call to the getUpdates, so unwanted updates may be received for a short period of time.
AllowedUpdates []string `json:"allowed_updates,omitempty"`
}
// SetWebhookParameters represents data for SetWebhook method.
SetWebhook struct {
// HTTPS url to send updates to. Use an empty string to remove webhook integration
URL string `json:"url"`
// Upload your public key certificate so that the root certificate in use can be checked. See our self-signed guide for details.
Certificate InputFile `json:"certificate,omitempty"`
// The fixed IP address which will be used to send webhook requests instead of the IP address resolved
// through DNS
IpAddress net.IP `json:"ip_address,omitempty"`
// Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40. Use lower values to limit the load on your bot‘s server, and higher values to increase your bot’s throughput.
MaxConnections int `json:"max_connections,omitempty"`
// List the types of updates you want your bot to receive. For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all updates regardless of type (default). If not specified, the previous setting will be used.
//
// Please note that this parameter doesn't affect updates created before the call to the setWebhook, so unwanted updates may be received for a short period of time.
AllowedUpdates []string `json:"allowed_updates,omitempty"`
// Pass True to drop all pending updates
DropPendingUpdates bool `json:"drop_pending_updates,omitempty"`
}
DeleteWebhook struct {
// Pass True to drop all pending updates
DropPendingUpdates bool `json:"drop_pending_updates,omitempty"`
}
// UpdatesChannel represents channel for incoming updates.
UpdatesChannel chan *Update
)
// GetUpdates receive incoming updates using long polling. An Array of Update objects is returned.
func (b Bot) GetUpdates(p *GetUpdates) ([]*Update, error) {
src, err := b.Do(MethodGetUpdates, p)
if err != nil {
return nil, err
}
result := make([]*Update, 0)
if err = parseResponseError(b.marshler, src, &result); err != nil {
return nil, err
}
return result, nil
}
// SetWebhook specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. Returns true.
//
// If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the URL, e.g. https://www.example.com/<token>. Since nobody else knows your bot‘s token, you can be pretty sure it’s us.
func (b Bot) SetWebhook(p SetWebhook) (ok bool, err error) {
if p.Certificate.IsAttachment() {
_, err := b.Upload(MethodSetWebhook, map[string]string{
"url": p.URL,
"ip_address": p.IpAddress.String(),
"max_connections": strconv.Itoa(p.MaxConnections),
"allowed_updates": strings.Join(p.AllowedUpdates, ","),
"drop_pending_updates": strconv.FormatBool(p.DropPendingUpdates),
}, &p.Certificate)
return err == nil, err
}
src, err := b.Do(MethodSetWebhook, p)
if err != nil {
return ok, err
}
if err = parseResponseError(b.marshler, src, &ok); err != nil {
return
}
return
}
// DeleteWebhook remove webhook integration if you decide to switch back to getUpdates. Returns True on success. Requires no parameters.
func (b Bot) DeleteWebhook(p DeleteWebhook) (ok bool, err error) {
src, err := b.Do(MethodDeleteWebhook, p)
if err != nil {
return ok, err
}
if err = parseResponseError(b.marshler, src, &ok); err != nil {
return
}
return
}
// GetWebhookInfo get current webhook status. Requires no parameters. On success, returns a WebhookInfo object. If the bot is using getUpdates, will return an object with the url field empty.
func (b Bot) GetWebhookInfo() (*WebhookInfo, error) {
src, err := b.Do(MethodGetWebhookInfo, nil)
if err != nil {
return nil, err
}
result := new(WebhookInfo)
if err = parseResponseError(b.marshler, src, result); err != nil {
return nil, err
}
return result, nil
}
// IsMessage checks that the current update is a message creation event.
func (u Update) IsMessage() bool { return u.Message != nil }
// IsEditedMessage checks that the current update is a editing message event.
func (u Update) IsEditedMessage() bool { return u.EditedMessage != nil }
// IsChannelPost checks that the current update is a post channel creation event.
func (u Update) IsChannelPost() bool { return u.ChannelPost != nil }
// IsEditedChannelPost checks that the current update is a editing post channel event.
func (u Update) IsEditedChannelPost() bool { return u.EditedChannelPost != nil }
// IsInlineQuery checks that the current update is a inline query update.
func (u Update) IsInlineQuery() bool { return u.InlineQuery != nil }
// IsChosenInlineResult checks that the current update is a chosen inline result update.
func (u Update) IsChosenInlineResult() bool { return u.ChosenInlineResult != nil }
// IsCallbackQuery checks that the current update is a callback query update.
func (u Update) IsCallbackQuery() bool { return u.CallbackQuery != nil }
// IsShippingQuery checks that the current update is a shipping query update.
func (u Update) IsShippingQuery() bool { return u.ShippingQuery != nil }
// IsPreCheckoutQuery checks that the current update is a pre checkout query update.
func (u Update) IsPreCheckoutQuery() bool { return u.PreCheckoutQuery != nil }
// IsPoll checks that the current update is a poll update.
func (u Update) IsPoll() bool { return u.Poll != nil }
// Type return update type for current update.
func (u Update) Type() string {
switch {
case u.IsCallbackQuery():
return UpdateCallbackQuery
case u.IsChannelPost():
return UpdateChannelPost
case u.IsChosenInlineResult():
return UpdateChosenInlineResult
case u.IsEditedChannelPost():
return UpdateEditedChannelPost
case u.IsEditedMessage():
return UpdateEditedMessage
case u.IsInlineQuery():
return UpdateInlineQuery
case u.IsMessage():
return UpdateMessage
case u.IsPreCheckoutQuery():
return UpdatePreCheckoutQuery
case u.IsShippingQuery():
return UpdateShippingQuery
case u.IsPoll():
return UpdatePoll
default:
return ""
}
}
func (w WebhookInfo) LastErrorTime() time.Time { return time.Unix(w.LastErrorDate, 0) }
func (w WebhookInfo) HasURL() bool { return w.URL != "" }
func (w WebhookInfo) URI() *http.URI {
if !w.HasURL() {
return nil
}
u := http.AcquireURI()
u.Update(w.URL)
return u
}