-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendable.go
406 lines (341 loc) · 9.22 KB
/
sendable.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package crare
import (
"path/filepath"
"github.com/3JoB/unsafeConvert"
)
// Recipient is any possible endpoint you can send
// messages to: either user, group or a channel.
type Recipient interface {
Recipient() string // must return legit Telegram chat_id or username
}
// Sendable is any object that can send itself.
//
// This is pretty cool, since it lets bots implement
// custom Sendables for complex kind of media or
// chat objects spanning across multiple messages.
type Sendable interface {
Send(*Bot, Recipient, *SendOptions) (*Message, error)
}
// Send delivers media through bot b to recipient.
func (p *Photo) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"caption": p.Caption,
}
b.embedSendOptions(params, opt)
if p.HasSpoiler {
params["has_spoiler"] = true
}
msg, err := b.sendMedia(p, params, nil)
if err != nil {
return nil, err
}
msg.Photo.File.stealRef(&p.File)
*p = *msg.Photo
p.Caption = msg.Caption
p.HasSpoiler = msg.HasMediaSpoiler
return msg, nil
}
// Send delivers media through bot b to recipient.
func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"caption": a.Caption,
"performer": a.Performer,
"title": a.Title,
"file_name": a.FileName,
}
b.embedSendOptions(params, opt)
if a.Duration != 0 {
params["duration"] = a.Duration
}
msg, err := b.sendMedia(a, params, thumbnailToFilemap(a.Thumbnail))
if err != nil {
return nil, err
}
if msg.Audio != nil {
msg.Audio.File.stealRef(&a.File)
*a = *msg.Audio
a.Caption = msg.Caption
}
if msg.Document != nil {
msg.Document.File.stealRef(&a.File)
a.File = msg.Document.File
}
return msg, nil
}
// Send delivers media through bot b to recipient.
func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"caption": d.Caption,
"file_name": d.FileName,
}
b.embedSendOptions(params, opt)
if d.FileSize != 0 {
params["file_size"] = d.FileSize
}
if d.DisableTypeDetection {
params["disable_content_type_detection"] = true
}
msg, err := b.sendMedia(d, params, thumbnailToFilemap(d.Thumbnail))
if err != nil {
return nil, err
}
if doc := msg.Document; doc != nil {
doc.File.stealRef(&d.File)
*d = *doc
d.Caption = msg.Caption
} else if vid := msg.Video; vid != nil {
vid.File.stealRef(&d.File)
d.Caption = vid.Caption
d.MIME = vid.MIME
d.Thumbnail = vid.Thumbnail
}
return msg, nil
}
// Send delivers media through bot b to recipient.
func (s *Sticker) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
}
b.embedSendOptions(params, opt)
msg, err := b.sendMedia(s, params, nil)
if err != nil {
return nil, err
}
msg.Sticker.File.stealRef(&s.File)
*s = *msg.Sticker
return msg, nil
}
// Send delivers media through bot b to recipient.
func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"caption": v.Caption,
"file_name": v.FileName,
}
b.embedSendOptions(params, opt)
if v.Duration != 0 {
params["duration"] = v.Duration
}
if v.Width != 0 {
params["width"] = v.Width
}
if v.Height != 0 {
params["height"] = v.Height
}
if v.Streaming {
params["supports_streaming"] = true
}
if v.HasSpoiler {
params["has_spoiler"] = true
}
msg, err := b.sendMedia(v, params, thumbnailToFilemap(v.Thumbnail))
if err != nil {
return nil, err
}
if vid := msg.Video; vid != nil {
vid.File.stealRef(&v.File)
*v = *vid
v.Caption = msg.Caption
} else if doc := msg.Document; doc != nil {
// If video has no sound, Telegram can turn it into Document (GIF)
doc.File.stealRef(&v.File)
v.Caption = doc.Caption
v.MIME = doc.MIME
v.Thumbnail = doc.Thumbnail
}
v.HasSpoiler = msg.HasMediaSpoiler
return msg, nil
}
// Send delivers animation through bot b to recipient.
func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"caption": a.Caption,
"file_name": a.FileName,
}
b.embedSendOptions(params, opt)
if a.Duration != 0 {
params["duration"] = a.Duration
}
if a.Width != 0 {
params["width"] = a.Width
}
if a.Height != 0 {
params["height"] = a.Height
}
if a.HasSpoiler {
params["has_spoiler"] = true
}
// file_name is required, without it animation sends as a document
if params["file_name"] == "" && a.File.OnDisk() {
params["file_name"] = filepath.Base(a.File.FileLocal)
}
msg, err := b.sendMedia(a, params, thumbnailToFilemap(a.Thumbnail))
if err != nil {
return nil, err
}
if anim := msg.Animation; anim != nil {
anim.File.stealRef(&a.File)
*a = *msg.Animation
} else if doc := msg.Document; doc != nil {
*a = Animation{
File: doc.File,
Thumbnail: doc.Thumbnail,
MIME: doc.MIME,
FileName: doc.FileName,
}
}
a.HasSpoiler = msg.HasMediaSpoiler
a.Caption = msg.Caption
return msg, nil
}
// Send delivers media through bot b to recipient.
func (v *Voice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"caption": v.Caption,
}
b.embedSendOptions(params, opt)
if v.Duration != 0 {
params["duration"] = v.Duration
}
msg, err := b.sendMedia(v, params, nil)
if err != nil {
return nil, err
}
msg.Voice.File.stealRef(&v.File)
*v = *msg.Voice
return msg, nil
}
// Send delivers media through bot b to recipient.
func (v *VideoNote) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
}
b.embedSendOptions(params, opt)
if v.Duration != 0 {
params["duration"] = v.Duration
}
if v.Length != 0 {
params["length"] = v.Length
}
msg, err := b.sendMedia(v, params, thumbnailToFilemap(v.Thumbnail))
if err != nil {
return nil, err
}
msg.VideoNote.File.stealRef(&v.File)
*v = *msg.VideoNote
return msg, nil
}
// Send delivers media through bot b to recipient.
func (x *Location) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"latitude": x.Lat,
"longitude": x.Lng,
"live_period": x.LivePeriod,
}
if x.HorizontalAccuracy != nil {
params["horizontal_accuracy"] = *x.HorizontalAccuracy
}
if x.Heading != 0 {
params["heading"] = x.Heading
}
if x.AlertRadius != 0 {
params["proximity_alert_radius"] = x.Heading
}
b.embedSendOptions(params, opt)
data, err := b.Raw("sendLocation", params)
if err != nil {
return nil, err
}
return extractMessage(data)
}
// Send delivers media through bot b to recipient.
func (v *Venue) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"latitude": v.Location.Lat,
"longitude": v.Location.Lng,
"title": v.Title,
"address": v.Address,
"foursquare_id": v.FoursquareID,
"foursquare_type": v.FoursquareType,
"google_place_id": v.GooglePlaceID,
"google_place_type": v.GooglePlaceType,
}
b.embedSendOptions(params, opt)
data, err := b.Raw("sendVenue", params)
if err != nil {
return nil, err
}
return extractMessage(data)
}
// Send delivers poll through bot b to recipient.
func (p *Poll) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"question": p.Question,
"type": unsafeConvert.AnyString(p.Type),
"is_closed": p.Closed,
"is_anonymous": p.Anonymous,
"allows_multiple_answers": p.MultipleAnswers,
"correct_option_id": p.CorrectOption,
}
if p.Explanation != "" {
params["explanation"] = p.Explanation
params["explanation_parse_mode"] = p.ParseMode
}
if p.OpenPeriod != 0 {
params["open_period"] = p.OpenPeriod
} else if p.CloseUnixdate != 0 {
params["close_date"] = p.CloseUnixdate
}
b.embedSendOptions(params, opt)
var options []string
for _, o := range p.Options {
options = append(options, o.Text)
}
opts, _ := b.json.Marshal(options)
params["options"] = unsafeConvert.StringPointer(opts)
data, err := b.Raw("sendPoll", params)
if err != nil {
return nil, err
}
return extractMessage(data)
}
// Send delivers dice through bot b to recipient.
func (d *Dice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"emoji": unsafeConvert.AnyString(d.Type),
}
b.embedSendOptions(params, opt)
data, err := b.Raw("sendDice", params)
if err != nil {
return nil, err
}
return extractMessage(data)
}
// Send delivers game through bot b to recipient.
func (g *Game) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]any{
"chat_id": to.Recipient(),
"game_short_name": g.Name,
}
b.embedSendOptions(params, opt)
data, err := b.Raw("sendGame", params)
if err != nil {
return nil, err
}
return extractMessage(data)
}
func thumbnailToFilemap(thumb *Photo) map[string]File {
if thumb != nil {
return map[string]File{"thumbnail": thumb.File}
}
return nil
}