-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.go
833 lines (664 loc) · 22.5 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
package micha
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
)
const (
defaultAPIServer = "https://api.telegram.org"
)
type Response struct {
Ok bool `json:"ok"`
ErrorCode int `json:"error_code"`
Description string `json:"description"`
Result json.RawMessage `json:"result"`
}
// Bot telegram bot
type Bot struct {
Options
Me User
token string
updates chan Update
offset uint64
cancelFunc context.CancelFunc
}
// NewBot - create new bot instance
func NewBot(token string, opts ...Option) (*Bot, error) {
options := Options{
limit: 100,
timeout: 25,
logger: slog.Default(),
apiServer: defaultAPIServer,
httpClient: http.DefaultClient,
ctx: context.Background(),
}
for _, opt := range opts {
opt(&options)
}
bot := Bot{
Options: options,
token: token,
updates: make(chan Update),
}
bot.ctx, bot.cancelFunc = context.WithCancel(options.ctx)
me, err := bot.GetMe()
if err != nil {
return nil, err
}
bot.Me = *me
return &bot, nil
}
// Build url for API method
func (bot *Bot) buildURL(method string) string {
return bot.Options.apiServer + fmt.Sprintf("/bot%s/%s", bot.token, method)
}
// Decode response result to target object
func (bot *Bot) decodeResponse(data []byte, target interface{}) error {
response := new(Response)
if err := json.Unmarshal(data, response); err != nil {
return fmt.Errorf("decode response error: %w", err)
}
if !response.Ok {
return fmt.Errorf("Error %d (%s)", response.ErrorCode, response.Description)
}
if target == nil {
// Don't need to decode result
return nil
}
if err := json.Unmarshal(response.Result, target); err != nil {
return fmt.Errorf("decode result error: %w", err)
}
return nil
}
// Send GET request to Telegram API
func (bot *Bot) get(method string, params url.Values, target interface{}) error {
request, err := newGetRequest(bot.ctx, bot.buildURL(method), params)
if err != nil {
return err
}
response, err := bot.httpClient.Do(request)
if err != nil {
return err
}
body, err := handleResponse(response)
if err != nil {
return err
}
return bot.decodeResponse(body, target)
}
// Send POST request to Telegram API
func (bot *Bot) post(method string, data, target interface{}) error {
request, err := newPostRequest(bot.ctx, bot.buildURL(method), data)
if err != nil {
return err
}
response, err := bot.httpClient.Do(request)
if err != nil {
return err
}
body, err := handleResponse(response)
if err != nil {
return err
}
return bot.decodeResponse(body, target)
}
// Send POST multipart request to Telegram API
func (bot *Bot) postMultipart(method string, file *fileField, params url.Values, target interface{}) error {
request, err := newMultipartRequest(bot.ctx, bot.buildURL(method), file, params)
if err != nil {
return err
}
response, err := bot.httpClient.Do(request)
if err != nil {
return err
}
body, err := handleResponse(response)
if err != nil {
return err
}
return bot.decodeResponse(body, target)
}
// Use this method to receive incoming updates using long polling.
// An Array of Update objects is returned.
func (bot *Bot) getUpdates(offset uint64, allowedUpdates ...string) ([]Update, error) {
params := url.Values{
"limit": {fmt.Sprintf("%d", bot.limit)},
"offset": {fmt.Sprintf("%d", offset)},
"timeout": {fmt.Sprintf("%d", bot.timeout)},
}
if len(allowedUpdates) > 0 {
params["allowed_updates"] = allowedUpdates
}
updates := []Update{}
err := bot.get("getUpdates", params, &updates)
return updates, err
}
// Start getting updates
func (bot *Bot) Start(allowedUpdates ...string) {
for {
updates, err := bot.getUpdates(bot.offset+1, allowedUpdates...)
if err != nil {
bot.logger.ErrorContext(bot.ctx, "Get updates error", "error", err)
httpErr := HTTPError{}
if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusConflict {
bot.cancelFunc()
}
}
for _, update := range updates {
bot.updates <- update
bot.offset = update.UpdateID
}
select {
case <-bot.ctx.Done():
close(bot.updates)
return
default:
}
}
}
// Stop getting updates
func (bot *Bot) Stop() {
bot.cancelFunc()
}
// Updates channel
func (bot *Bot) Updates() <-chan Update {
return bot.updates
}
func (bot *Bot) GetWebhookInfo() (*WebhookInfo, error) {
webhookInfo := new(WebhookInfo)
err := bot.get("getWebhookInfo", url.Values{}, webhookInfo)
return webhookInfo, err
}
func (bot *Bot) SetWebhook(webhookURL string, options *SetWebhookOptions) error {
var file *fileField
params := url.Values{
"url": {webhookURL},
}
if options != nil {
if options.MaxConnections > 0 {
params.Set("max_connections", fmt.Sprintf("%d", options.MaxConnections))
}
if len(options.AllowedUpdates) > 0 {
params["allowed_updates"] = options.AllowedUpdates
}
if len(options.Certificate) > 0 {
file = &fileField{
Source: bytes.NewBuffer(options.Certificate),
Fieldname: "certificate",
Filename: "certificate",
}
}
}
return bot.postMultipart("setWebhook", file, params, nil)
}
func (bot *Bot) DeleteWebhook() error {
return bot.post("deleteWebhook", nil, nil)
}
// Logout
// Use this method to log out from the cloud Bot API server before launching the bot locally.
// You must log out the bot before running it locally,
// otherwise there is no guarantee that the bot will receive updates.
// After a successful call, you can immediately log in on a local server,
// but will not be able to log in back to the cloud Bot API server for 10 minutes.
func (bot *Bot) Logout() error {
url := defaultAPIServer + fmt.Sprintf("/bot%s/logOut", bot.token)
request, err := newGetRequest(bot.ctx, url, nil)
if err != nil {
return err
}
response, err := bot.httpClient.Do(request)
if err != nil {
return err
}
_, err = handleResponse(response)
return err
}
// A simple method for testing your bot's auth token.
// Returns basic information about the bot in form of a User object.
func (bot *Bot) GetMe() (*User, error) {
me := new(User)
err := bot.get("getMe", nil, me)
return me, err
}
// Raw - send any method and return raw response
func (bot *Bot) Raw(method string, data any) ([]byte, error) {
request, err := newPostRequest(bot.ctx, bot.buildURL(method), data)
if err != nil {
return nil, err
}
response, err := bot.httpClient.Do(request)
if err != nil {
return nil, err
}
return handleResponse(response)
}
// Use this method to send text messages.
func (bot *Bot) SendMessage(chatID ChatID, text string, options *SendMessageOptions) (*Message, error) {
params := sendMessageParams{
ChatID: chatID,
Text: text,
}
if options != nil {
params.SendMessageOptions = *options
}
message := new(Message)
err := bot.post("sendMessage", params, message)
return message, err
}
// Send exists photo by file_id
func (bot *Bot) SendPhoto(chatID ChatID, photoID string, options *SendPhotoOptions) (*Message, error) {
params := newSendPhotoParams(chatID, photoID, options)
message := new(Message)
err := bot.post("sendPhoto", params, message)
return message, err
}
// Send photo file
func (bot *Bot) SendPhotoFile(chatID ChatID, file io.Reader, fileName string, options *SendPhotoOptions) (*Message, error) {
params := newSendPhotoParams(chatID, "", options)
values, err := structToValues(params)
if err != nil {
return nil, err
}
f := &fileField{
Source: file,
Fieldname: "photo",
Filename: fileName,
}
message := new(Message)
err = bot.postMultipart("sendPhoto", f, values, message)
return message, err
}
// Send exists audio by file_id
func (bot *Bot) SendAudio(chatID ChatID, audioID string, options *SendAudioOptions) (*Message, error) {
params := newSendAudioParams(chatID, audioID, options)
message := new(Message)
err := bot.post("sendAudio", params, message)
return message, err
}
// Send audio file
func (bot *Bot) SendAudioFile(chatID ChatID, file io.Reader, fileName string, options *SendAudioOptions) (*Message, error) {
params := newSendAudioParams(chatID, "", options)
values, err := structToValues(params)
if err != nil {
return nil, err
}
f := &fileField{
Source: file,
Fieldname: "audio",
Filename: fileName,
}
message := new(Message)
err = bot.postMultipart("sendAudio", f, values, message)
return message, err
}
// Send exists document by file_id
func (bot *Bot) SendDocument(chatID ChatID, documentID string, options *SendDocumentOptions) (*Message, error) {
params := newSendDocumentParams(chatID, documentID, options)
message := new(Message)
err := bot.post("sendDocument", params, message)
return message, err
}
// Send file
func (bot *Bot) SendDocumentFile(chatID ChatID, file io.Reader, fileName string, options *SendDocumentOptions) (*Message, error) {
params := newSendDocumentParams(chatID, "", options)
values, err := structToValues(params)
if err != nil {
return nil, err
}
f := &fileField{
Source: file,
Fieldname: "document",
Filename: fileName,
}
message := new(Message)
err = bot.postMultipart("sendDocument", f, values, message)
return message, err
}
// Send exists sticker by file_id
func (bot *Bot) SendSticker(chatID ChatID, stickerID string, options *SendStickerOptions) (*Message, error) {
params := newSendStickerParams(chatID, stickerID, options)
message := new(Message)
err := bot.post("sendSticker", params, message)
return message, err
}
// Send .webp sticker file
func (bot *Bot) SendStickerFile(chatID ChatID, file io.Reader, fileName string, options *SendStickerOptions) (*Message, error) {
params := newSendStickerParams(chatID, "", options)
values, err := structToValues(params)
if err != nil {
return nil, err
}
f := &fileField{
Source: file,
Fieldname: "sticker",
Filename: fileName,
}
message := new(Message)
err = bot.postMultipart("sendSticker", f, values, message)
return message, err
}
// Send exists video by file_id
func (bot *Bot) SendVideo(chatID ChatID, videoID string, options *SendVideoOptions) (*Message, error) {
params := newSendVideoParams(chatID, videoID, options)
message := new(Message)
err := bot.post("sendVideo", params, message)
return message, err
}
// Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document).
func (bot *Bot) SendVideoFile(chatID ChatID, file io.Reader, fileName string, options *SendVideoOptions) (*Message, error) {
params := newSendVideoParams(chatID, "", options)
values, err := structToValues(params)
if err != nil {
return nil, err
}
f := &fileField{
Source: file,
Fieldname: "video",
Filename: fileName,
}
message := new(Message)
err = bot.postMultipart("sendVideo", f, values, message)
return message, err
}
// Send exists voice by file_id
func (bot *Bot) SendVoice(chatID ChatID, voiceID string, options *SendVoiceOptions) (*Message, error) {
params := newSendVoiceParams(chatID, voiceID, options)
message := new(Message)
err := bot.post("sendVoice", params, message)
return message, err
}
// Use this method to send audio files,
// if you want Telegram clients to display the file as a playable voice message.
// For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document).
func (bot *Bot) SendVoiceFile(chatID ChatID, file io.Reader, fileName string, options *SendVoiceOptions) (*Message, error) {
params := newSendVoiceParams(chatID, "", options)
values, err := structToValues(params)
if err != nil {
return nil, err
}
f := &fileField{
Source: file,
Fieldname: "voice",
Filename: fileName,
}
message := new(Message)
err = bot.postMultipart("sendVoice", f, values, message)
return message, err
}
// Send exists video note by file_id
func (bot *Bot) SendVideoNote(chatID ChatID, videoNoteID string, options *SendVideoNoteOptions) (*Message, error) {
params := newSendVideoNoteParams(chatID, videoNoteID, options)
message := new(Message)
err := bot.post("sendVideoNote", params, message)
return message, err
}
// Use this method to send video messages
func (bot *Bot) SendVideoNoteFile(chatID ChatID, file io.Reader, fileName string, options *SendVideoNoteOptions) (*Message, error) {
params := newSendVideoNoteParams(chatID, "", options)
values, err := structToValues(params)
if err != nil {
return nil, err
}
f := &fileField{
Source: file,
Fieldname: "video_note",
Filename: fileName,
}
message := new(Message)
err = bot.postMultipart("sendVideoNote", f, values, message)
return message, err
}
// Use this method to send point on the map
func (bot *Bot) SendLocation(chatID ChatID, latitude, longitude float64, options *SendLocationOptions) (*Message, error) {
params := newSendLocationParams(chatID, latitude, longitude, options)
message := new(Message)
err := bot.post("sendLocation", params, message)
return message, err
}
// Use this method to send information about a venue
func (bot *Bot) SendVenue(chatID ChatID, latitude, longitude float64, title, address string, options *SendVenueOptions) (*Message, error) {
params := newSendVenueParams(chatID, latitude, longitude, title, address, options)
message := new(Message)
err := bot.post("sendVenue", params, message)
return message, err
}
// Use this method to send phone contacts
func (bot *Bot) SendContact(chatID ChatID, phoneNumber, firstName, lastName string, options *SendContactOptions) (*Message, error) {
params := newSendContactParams(chatID, phoneNumber, firstName, lastName, options)
message := new(Message)
err := bot.post("sendContact", params, message)
return message, err
}
// Use this method to forward messages of any kind.
func (bot *Bot) ForwardMessage(chatID, fromChatID ChatID, messageID int64, disableNotification bool) (*Message, error) {
params := map[string]interface{}{
"chat_id": chatID,
"from_chat_id": fromChatID,
"message_id": messageID,
"disable_notification": disableNotification,
}
message := new(Message)
err := bot.post("forwardMessage", params, message)
return message, err
}
// Use this method when you need to tell the user that something is happening on the bot's side.
// The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status).
func (bot *Bot) SendChatAction(chatID ChatID, action ChatAction) error {
params := map[string]interface{}{
"chat_id": chatID,
"action": action,
}
return bot.post("sendChatAction", params, nil)
}
// Use this method to get a list of profile pictures for a user.
func (bot *Bot) GetUserProfilePhotos(userID int64, offset, limit *int) (*UserProfilePhotos, error) {
params := url.Values{
"user_id": {fmt.Sprintf("%d", userID)},
}
if offset != nil {
params["offset"] = []string{fmt.Sprintf("%d", *offset)}
}
if limit != nil {
params["limit"] = []string{fmt.Sprintf("%d", *limit)}
}
profilePhotos := new(UserProfilePhotos)
err := bot.get("getUserProfilePhotos", params, profilePhotos)
return profilePhotos, err
}
// Use this method to get basic info about a file and prepare it for downloading.
// It is guaranteed that the link will be valid for at least 1 hour.
// When the link expires, a new one can be requested by calling getFile again.
func (bot *Bot) GetFile(fileID string) (*File, error) {
params := url.Values{
"file_id": {fileID},
}
file := new(File)
err := bot.get("getFile", params, file)
return file, err
}
// Return absolute url for file downloading by file path
func (bot *Bot) DownloadFileURL(filePath string) string {
return bot.Options.apiServer + fmt.Sprintf("/file/bot%s/%s", bot.token, filePath)
}
// Use this method to edit text messages sent by the bot or via the bot (for inline bots).
func (bot *Bot) EditMessageText(chatID ChatID, messageID int64, inlineMessageID, text string, options *EditMessageTextOptions) (*Message, error) {
params := editMessageTextParams{
ChatID: chatID,
MessageID: messageID,
InlineMessageID: inlineMessageID,
Text: text,
}
if options != nil {
params.EditMessageTextOptions = *options
}
message := new(Message)
err := bot.post("editMessageText", params, message)
return message, err
}
// Use this method to edit captions of messages sent by the bot or via the bot (for inline bots).
func (bot *Bot) EditMessageCaption(chatID ChatID, messageID int64, inlineMessageID string, options *EditMessageCationOptions) (*Message, error) {
params := editMessageCationParams{
ChatID: chatID,
MessageID: messageID,
InlineMessageID: inlineMessageID,
}
if options != nil {
params.EditMessageCationOptions = *options
}
message := new(Message)
err := bot.post("editMessageCaption", params, message)
return message, err
}
// Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots).
func (bot *Bot) EditMessageReplyMarkup(chatID ChatID, messageID int64, inlineMessageID string, replyMarkup ReplyMarkup) (*Message, error) {
params := editMessageReplyMarkupParams{
ChatID: chatID,
MessageID: messageID,
InlineMessageID: inlineMessageID,
ReplyMarkup: replyMarkup,
}
message := new(Message)
err := bot.post("editMessageReplyMarkup", params, message)
return message, err
}
// Use this method to delete a message.
// A message can only be deleted if it was sent less than 48 hours ago.
// Any such recently sent outgoing message may be deleted.
// Additionally, if the bot is an administrator in a group chat, it can delete any message.
// If the bot is an administrator in a supergroup, it can delete messages from any other user and service messages about people joining or leaving the group (other types of service messages may only be removed by the group creator). In channels, bots can only remove their own messages.
func (bot *Bot) DeleteMessage(chatID ChatID, messageID int64) (bool, error) {
params := map[string]interface{}{
"chat_id": chatID,
"message_id": messageID,
}
var success bool
err := bot.post("deleteMessage", params, &success)
return success, err
}
// Use this method to send answers to an inline query.
// No more than 50 results per query are allowed.
func (bot *Bot) AnswerInlineQuery(inlineQueryID string, results InlineQueryResults, options *AnswerInlineQueryOptions) error {
params := answerInlineQueryParams{
InlineQueryID: inlineQueryID,
Results: results,
}
if options != nil {
params.AnswerInlineQueryOptions = *options
}
return bot.post("answerInlineQuery", params, nil)
}
// Use this method to kick a user from a group or a supergroup.
// In the case of supergroups, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first.
// The bot must be an administrator in the group for this to work.
func (bot *Bot) KickChatMember(chatID ChatID, userID int64) error {
params := map[string]interface{}{
"chat_id": chatID,
"user_id": userID,
}
return bot.post("kickChatMember", params, nil)
}
// Use this method for your bot to leave a group, supergroup or channel
func (bot *Bot) LeaveChat(chatID ChatID) error {
params := map[string]interface{}{
"chat_id": chatID,
}
return bot.post("leaveChat", params, nil)
}
// Use this method to unban a previously kicked user in a supergroup.
// The user will not return to the group automatically, but will be able to join via link, etc.
// The bot must be an administrator in the group for this to work.
func (bot *Bot) UnbanChatMember(chatID ChatID, userID int64) error {
params := map[string]interface{}{
"chat_id": chatID,
"user_id": userID,
}
return bot.post("unbanChatMember", params, nil)
}
// Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.).
func (bot *Bot) GetChat(chatID ChatID) (*Chat, error) {
params := url.Values{
"chat_id": {string(chatID)},
}
chat := new(Chat)
err := bot.get("getChat", params, chat)
return chat, err
}
// Use this method to get a list of administrators in a chat.
// If the chat is a group or a supergroup and no administrators were appointed, only the creator will be returned.
func (bot *Bot) GetChatAdministrators(chatID ChatID) ([]ChatMember, error) {
params := url.Values{
"chat_id": {string(chatID)},
}
administrators := []ChatMember{}
err := bot.get("getChatAdministrators", params, &administrators)
return administrators, err
}
// Use this method to get the number of members in a chat.
func (bot *Bot) GetChatMembersCount(chatID ChatID) (int, error) {
params := url.Values{
"chat_id": {string(chatID)},
}
count := 0
err := bot.get("getChatMembersCount", params, &count)
return count, err
}
// Use this method to get information about a member of a chat.
func (bot *Bot) GetChatMember(chatID ChatID, userID int64) (*ChatMember, error) {
params := url.Values{
"chat_id": {string(chatID)},
"user_id": {fmt.Sprintf("%d", userID)},
}
chatMember := new(ChatMember)
err := bot.get("getChatMember", params, chatMember)
return chatMember, err
}
// Use this method to send answers to callback queries sent from inline keyboards.
// The answer will be displayed to the user as a notification at the top of the chat screen or as an alert.
func (bot *Bot) AnswerCallbackQuery(callbackQueryID string, options *AnswerCallbackQueryOptions) error {
params := newAnswerCallbackQueryParams(callbackQueryID, options)
return bot.post("answerCallbackQuery", params, nil)
}
// Use this method to send a game.
func (bot *Bot) SendGame(chatID ChatID, gameShortName string, options *SendGameOptions) (*Message, error) {
params := sendGameParams{
ChatID: chatID,
GameShortName: gameShortName,
}
if options != nil {
params.SendGameOptions = *options
}
message := new(Message)
err := bot.post("sendGame", params, message)
return message, err
}
// Use this method to set the score of the specified user in a game.
func (bot *Bot) SetGameScore(userID int64, score int, options *SetGameScoreOptions) (*Message, error) {
params := setGameScoreParams{
UserID: userID,
Score: score,
}
if options != nil {
params.SetGameScoreOptions = *options
}
message := new(Message)
err := bot.post("setGameScore", params, message)
return message, err
}
// Use this method to get data for high score tables.
// Will return the score of the specified user and several of his neighbors in a game.
func (bot *Bot) GetGameHighScores(userID int64, options *GetGameHighScoresOptions) ([]GameHighScore, error) {
params, err := structToValues(options)
if err != nil {
return nil, err
}
params.Set("user_id", fmt.Sprintf("%d", userID))
scores := []GameHighScore{}
err = bot.get("getGameHighScores", params, &scores)
return scores, err
}