-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbot_test.go
More file actions
842 lines (767 loc) · 21.6 KB
/
Copy pathbot_test.go
File metadata and controls
842 lines (767 loc) · 21.6 KB
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
834
835
836
837
838
839
840
841
842
// bot_test.go
//
// created on: 2023.11.10.
package telegrambot
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"strings"
"testing"
"time"
)
// test timeouts and their handling
func TestTimeout(t *testing.T) {
_token := os.Getenv("TOKEN")
_verbose := os.Getenv("VERBOSE")
client := NewClient(_token)
client.Verbose = _verbose == "true"
if len(_token) <= 0 {
slog.Warn("skipping test: environment variable `TOKEN` is needed")
t.Skip("environment variable `TOKEN` is needed")
}
slog.Info("testing timeouts...")
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Nanosecond)
defer cancel()
// intentional timeout
if _, err := client.GetMe(ctx); err != nil {
if _, ok := errors.AsType[ErrContextTimeout](err); !ok {
t.Errorf("expected `ErrContextTimeout` but got: %[1]s (%[1]T)", err)
}
}
}
// test polling updates
func TestPollingUpdates(t *testing.T) {
_token := os.Getenv("TOKEN")
_verbose := os.Getenv("VERBOSE")
client := NewClient(_token)
client.Verbose = _verbose == "true"
if len(_token) <= 0 {
slog.Warn("skipping test: environment variable `TOKEN` is needed")
t.Skip("environment variable `TOKEN` is needed")
}
slog.Info("testing polling updates...")
if deleted, err := client.DeleteWebhook(context.TODO(), true); err != nil {
t.Errorf("failed to delete webhook before testing polling updates: %s", *deleted.Description)
} else {
go func() {
time.Sleep(10 * time.Second) // sleep for a while,
client.StopPollingUpdates() // stop polling
}()
// polling is synchronous
client.StartPollingUpdates(0, 1, func(b *Bot, update Update, err error) {
if err != nil {
t.Errorf("error while polling updates: %s", err)
}
}, []AllowedUpdate{AllowMessage})
slog.Info("stopped polling updates")
}
}
// test API method functions
func TestMethods(t *testing.T) {
_token := os.Getenv("TOKEN")
_chatID := os.Getenv("CHAT_ID") // NOTE: `chat_id` of a group chat with topics
_verbose := os.Getenv("VERBOSE")
client := NewClient(_token)
client.Verbose = _verbose == "true"
if len(_token) <= 0 || len(_chatID) <= 0 {
slog.Warn("skipping test: environment variables `TOKEN` and `CHAT_ID` are needed")
t.Skip("environment variables `TOKEN` and `CHAT_ID` are needed")
}
slog.Info("testing API method functions...")
////////////////////////////////
// (bot info)
//
// GetMe
if me, _ := client.GetMe(context.TODO()); !me.OK {
t.Errorf("failed to get me: %s", *me.Description)
} else {
if client.Verbose {
marshalled, _ := json.Marshal(me.Result)
slog.Info("fetched bot info", "user", marshalled)
}
// show warnings on missing permissions
if me.Result.SupportsInlineQueries != nil && !*me.Result.SupportsInlineQueries {
slog.Warn("bot does not support inline queries")
}
if me.Result.HasTopicsEnabled != nil && !*me.Result.HasTopicsEnabled {
slog.Warn("bot does not have topics enabled")
}
if me.Result.AllowsUsersToCreateTopics != nil && !*me.Result.AllowsUsersToCreateTopics {
slog.Warn("bot does not allow users to create topics")
}
////////////////////////////////
// (webhook)
//
// SetWebhook
if webhook, _ := client.SetWebhook(
context.TODO(),
"testdomain.com",
8443,
OptionsSetWebhook{},
); !webhook.OK {
t.Errorf("failed to set webhook: %s", *webhook.Description)
} else {
// GetWebhookInfo
if webhook, _ := client.GetWebhookInfo(context.TODO()); !webhook.OK {
t.Errorf("failed to get webhook info: %s", *webhook.Description)
}
// DeleteWebhook
if deleted, _ := client.DeleteWebhook(
context.TODO(),
false,
); !deleted.OK {
t.Errorf("failed to delete webhook: %s", *webhook.Description)
}
////////////////////////////////
// (general methods)
//
// GetUpdates
if updates, _ := client.GetUpdates(
context.TODO(),
OptionsGetUpdates{},
); !updates.OK {
t.Errorf("failed to get updates: %s", *updates.Description)
}
// TODO: LogOut
// TODO: Close
// SendMessage
if sent, _ := client.SendMessage(
context.TODO(),
_chatID,
"test message",
OptionsSendMessage{},
); !sent.OK {
t.Errorf("failed to send message: %s", *sent.Description)
} else {
// EditMessageText
if edited, _ := client.EditMessageText(
context.TODO(),
"edited message",
OptionsEditMessageText{}.
SetIDs(_chatID, sent.Result.MessageID),
); !edited.OK {
t.Errorf("failed to edit message text: %s", *edited.Description)
}
// CopyMessage
if copied, _ := client.CopyMessage(
context.TODO(),
_chatID,
_chatID,
sent.Result.MessageID,
OptionsCopyMessage{},
); !copied.OK {
t.Errorf("failed to copy message: %s", *copied.Description)
}
// ForwardMessage
if forwarded, _ := client.ForwardMessage(
context.TODO(),
_chatID,
_chatID,
sent.Result.MessageID,
OptionsForwardMessage{},
); !forwarded.OK {
t.Errorf("failed to forward message: %s", *forwarded.Description)
}
}
// TODO: SendMessageDraft
// TODO: ForwardMessages
// SendPhoto
if photo, _ := client.SendPhoto(
context.TODO(),
_chatID,
NewInputFileFromFilepath("./samples/_files/gopher.png"),
OptionsSendPhoto{},
); !photo.OK {
t.Errorf("failed to send photo: %s", *photo.Description)
} else {
// EditMessageCaption
if caption, _ := client.EditMessageCaption(
context.TODO(),
OptionsEditMessageCaption{}.
SetIDs(_chatID, photo.Result.MessageID).
SetCaption("edited caption"),
); !caption.OK {
t.Errorf("failed to edit message caption: %s", *caption.Description)
}
}
// SendLivePhoto
if livePhoto, _ := client.SendLivePhoto(
context.TODO(),
_chatID,
NewInputFileFromFilepath("./samples/_files/video.mp4"),
NewInputFileFromFilepath("./samples/_files/gopher.png"),
OptionsSendLivePhoto{},
); !livePhoto.OK {
t.Errorf("failed to send live photo: %s", *livePhoto.Description)
}
// TODO: SendAudio
// TODO: SendDocument
if doc, _ := client.SendDocument(
context.TODO(),
_chatID,
NewInputFileFromFilepath("./samples/_files/gopher.png"),
OptionsSendDocument{},
); !doc.OK {
t.Errorf("failed to send document: %s", *doc.Description)
} else {
// GetFile
if file, _ := client.GetFile(
context.TODO(),
doc.Result.Document.FileID,
); !file.OK {
t.Errorf("failed to get file: %s", *file.Description)
}
// DeleteMessage
if deleted, _ := client.DeleteMessage(
context.TODO(),
_chatID,
doc.Result.MessageID,
); !deleted.OK {
t.Errorf("failed to delete message: %s", *deleted.Description)
}
}
// TODO: DeleteMessages
// TODO: SendSticker
// TODO: SendVideo
// TODO: SendAnimation
// TODO: SendVoice
// TODO: SendVideoNote
// TODO: SendPaidMedia
// TODO: SendMediaGroup
// SendLocation
if location, _ := client.SendLocation(
context.TODO(),
_chatID,
37.5665,
126.9780,
OptionsSendLocation{},
); !location.OK {
t.Errorf("failed to send location: %s", *location.Description)
}
// TODO: SendVenue
// SendContact
if contact, _ := client.SendContact(
context.TODO(),
_chatID,
"911",
"Nine-One-One",
OptionsSendContact{},
); !contact.OK {
t.Errorf("failed to send contact: %s", *contact.Description)
}
// SendPoll
if poll, _ := client.SendPoll(
context.TODO(),
_chatID,
"The earth is...?",
[]InputPollOption{
{Text: "flat"},
{Text: "round"},
{Text: "nothing"},
},
OptionsSendPoll{},
); !poll.OK {
t.Errorf("failed to send poll: %s", *poll.Description)
} else {
// StopPoll
if stopped, _ := client.StopPoll(
context.TODO(),
_chatID,
poll.Result.MessageID,
OptionsStopPoll{},
); !stopped.OK {
t.Errorf("failed to stop poll: %s", *stopped.Description)
}
}
// TODO: ApproveSuggestedPost
// TODO: DeclineSuggestedPost
// TODO: SendChecklist
// SendDice
if dice, _ := client.SendDice(
context.TODO(),
_chatID,
OptionsSendDice{},
); !dice.OK {
t.Errorf("failed to send dice: %s", *dice.Description)
}
// TODO: CopyMessages
// SendChatAction
if action, _ := client.SendChatAction(
context.TODO(),
_chatID,
ChatActionTyping,
OptionsSendChatAction{},
); !action.OK {
t.Errorf("failed to send chat action: %s", *action.Description)
}
// SendRichMessage (blocks)
if sent, _ := client.SendRichMessage(
context.TODO(),
_chatID,
InputRichMessage{
Blocks: []InputRichBlock{
NewInputRichBlockSectionHeading(NewRichTextWithText("block text"), 3),
NewInputRichBlockSectionHeading(NewRichTextWithText("test"), 4),
NewInputRichBlockList([]InputRichBlockListItem{
{
Blocks: []InputRichBlock{
NewInputRichBlockParagraph(NewRichTextWithText("is it working?")),
},
},
{
Blocks: []InputRichBlock{
NewInputRichBlockParagraph(NewRichTextWithText("is it ok?")),
},
},
}),
},
},
OptionsSendRichMessage{},
); !sent.OK {
t.Errorf("failed to send rich message (blocks): %s", *sent.Description)
}
// SendRichMessage (html)
if sent, _ := client.SendRichMessage(
context.TODO(),
_chatID,
InputRichMessage{
HTML: new(`<h3>html text</h3>
<h4>test</h4>
<ul>
<li>is it working?</li>
<li>is it ok?</li>
</ul>
`),
},
OptionsSendRichMessage{},
); !sent.OK {
t.Errorf("failed to send rich message (html): %s", *sent.Description)
}
// SendRichMessage (markdown)
if sent, _ := client.SendRichMessage(
context.TODO(),
_chatID,
InputRichMessage{
Markdown: new(`# rich text
## test
- is it working?
- is it ok?
`),
},
OptionsSendRichMessage{},
); !sent.OK {
t.Errorf("failed to send rich message (markdown): %s", *sent.Description)
}
// TODO: SendRichMessageDraft
// TODO: EditEphemeralMessageText
// TODO: EditEphemeralMessageMedia
// TODO: EditEphemeralMessageCaption
// TODO: EditEphemeralMessageReplyMarkup
// TODO: DeleteEphemeralMessage
// TODO: GetUserProfilePhotos
// TODO: ApproveChatJoinRequest
// TODO: DeclineChatJoinRequest
// TODO: AnswerChatJoinRequestQuery
// TODO: SendChatJoinRequestWebApp
// TODO: GetMyCommands
// GetMyName
if name, _ := client.GetMyName(
context.TODO(),
OptionsGetMyName{},
); !name.OK {
t.Errorf("failed to get my name: %s", *name.Description)
} else {
newName := "telegram api test bot"
if name.Result.Name != newName {
// SetMyName
if name, _ := client.SetMyName(
context.TODO(),
newName,
OptionsSetMyName{},
); !name.OK {
t.Errorf("failed to set my name: %s", *name.Description)
}
}
}
// SetMyDescription
if desc, _ := client.SetMyDescription(
context.TODO(),
OptionsSetMyDescription{}.
SetDescription("A bot for testing library: telegram-bot-go"),
); !desc.OK {
t.Errorf("failed to set my description: %s", *desc.Description)
}
// GetMyDescription
if desc, _ := client.GetMyDescription(
context.TODO(),
OptionsGetMyDescription{},
); !desc.OK {
t.Errorf("failed to get my description: %s", *desc.Description)
}
// SetMyShortDescription
if desc, _ := client.SetMyShortDescription(
context.TODO(),
OptionsSetMyShortDescription{}.
SetShortDescription("telegram-bot-go"),
); !desc.OK {
t.Errorf("failed to set my short description: %s", *desc.Description)
}
// GetMyShortDescription
if desc, _ := client.GetMyShortDescription(
context.TODO(),
OptionsGetMyShortDescription{},
); !desc.OK {
t.Errorf("failed to get my short description: %s", *desc.Description)
}
// TODO: GetUserProfileAudios
// TODO: GetUserChatBoosts
// RemoveMyProfilePhoto - FIXME: Bad Request: BOT_FALLBACK_UNSUPPORTED
if removed, _ := client.RemoveMyProfilePhoto(
context.TODO(),
); !removed.OK {
t.Errorf("failed to remove my profile photo: %s", *removed.Description)
}
// SetMyProfilePhoto
if photo, _ := client.SetMyProfilePhoto(
context.TODO(),
NewInputProfilePhotoFromFilepath(
InputProfilePhotoStatic,
"./samples/_files/gopher.jpg",
),
); !photo.OK {
t.Errorf("failed to set my profile photo: %s", *photo.Description)
}
// TODO: SetMyCommands
// TODO: DeleteMyCommands
// TODO: SetChatMenuButton
// TODO: GetChatMenuButton
// TODO: SetMyDefaultAdministratorRights
// TODO: GetMyDefaultAdministratorRights
// TODO: EditMessageMedia
// TODO: EditMessageReplyMarkup
// TODO: EditMessageLiveLocation
// TODO: StopMessageLiveLocation
// TODO: EditMessageChecklist
////////////////////////////////
// (business connection)
//
// TODO: GetBusinessConnection
// TODO: ReadBusinessMessage
// TODO: DeleteBusinessMessages
// TODO: SetBusinessAccountName
// TODO: SetBusinessAccountUsername
// TODO: SetBusinessAccountBio
// TODO: SetBusinessAccountProfilePhoto
// TODO: RemoveBusinessAccountProfilePhoto
// TODO: SetBusinessAccountGiftSettings
// TODO: GetBusinessAccountStarBalance
// TODO: TransferBusinessAccountStars
// TODO: GetBusinessAccountGifts
// TODO: GetUserGifts
// TODO: GetChatGifts
// TODO: ConvertGiftToStars
// TODO: UpgradeGift
// TODO: TransferGift
// TODO: PostStory
// TODO: RepostStory
// TODO: EditStory
// TODO: DeleteStory
////////////////////////////////
// (callback query)
//
// TODO: AnswerCallbackQuery
////////////////////////////////
// (guest query)
//
// TODO: AnswerGuestQuery
////////////////////////////////
// (inline query)
//
// TODO: AnswerInlineQuery
////////////////////////////////
// (sticker)
//
// TODO: SendSticker
// TODO: GetStickerSet
// TODO: GetCustomEmojiStickers
// TODO: UploadStickerFile
// TODO: CreateNewsStickerSet
// TODO: AddStickerToSet
// TODO: SetStickerPositionInSet
// TODO: DeleteStickerFromSet
// TODO: ReplaceStickerInSet
// TODO: SetStickerSetThumbnail
// TODO: SetCustomEmojiStickerSetThumbnail
// TODO: SetStickerSetTitle
// TODO: DeleteStickerSet
// TODO: SetStickerEmojiList
// TODO: SetStickerKeywords
// TODO: SetStickerMaskPosition
// TODO: SetChatStickerSet
// TODO: DeleteChatStickerSet
////////////////////////////////
// (chat administration)
//
// GetChat
if chat, _ := client.GetChat(
context.TODO(),
_chatID,
); !chat.OK {
t.Errorf("failed to get chat: %s", *chat.Description)
}
// GetChatAdministrators
if admins, _ := client.GetChatAdministrators(
context.TODO(),
_chatID,
OptionsGetChatAdministrators{}.
SetReturnBots(true),
); !admins.OK {
t.Errorf("failed to get chat administrators: %s", *admins.Description)
}
// GetChatMemberCount
if count, _ := client.GetChatMemberCount(
context.TODO(),
_chatID,
); !count.OK {
t.Errorf("failed to get chat member count: %s", *count.Description)
}
// TODO: GetChatMember
// TODO: GetUserPersonalChatMessages
// TODO: CreateChat
// TODO: SetChatTitle
// SetChatDescription
if desc, _ := client.SetChatDescription(
context.TODO(),
_chatID,
fmt.Sprintf(
"[telegram-bot-go] chat_id: %s (last update: %d)",
_chatID,
time.Now().Unix(),
),
); !desc.OK {
t.Errorf("failed to set chat description: %s", *desc.Description)
}
// TODO: BanChatMember
// TODO: LeaveChat
// TODO: UnbanChatMember
// TODO: RestrictChatMember
// TODO: PromoteChatMember
// TODO: SetChatAdministratorCustomTitle
// TODO: SetChatMemberTag
// TODO: BanChatSenderChat
// TODO: UnbanChatSenderChat
// TODO: SetChatPermissions
// TODO: SetChatPhoto
// TODO: DeleteChatPhoto
// TODO: PinChatMessage
// TODO: UnpinChatMessage
// TODO: UnpinAllChatMessages
// TODO: ExportChatInviteLink
// TODO: CreateChatInviteLink
// TODO: EditChatInviteLink
// TODO: CreateChatSubscriptionInviteLink
// TODO: EditChatSubscriptionInviteLink
// TODO: RevokeChatInviteLink
////////////////////////////////
// (shopping)
//
// TODO: SendInvoice
// TODO: CreateInvoiceLink
// TODO: AnswerShippingQuery
// TODO: AnswerPreCheckoutQuery
// GetMyStarBalance
if balance, _ := client.GetMyStarBalance(context.TODO()); !balance.OK {
t.Errorf("failed to get my star balance: %s", *balance.Description)
}
// TODO: GetStarTransactions
// TODO: RefundStarPayment
// TODO: EditUserStarSubscription
////////////////////////////////
// (forum)
//
// CreateForumTopic
if created, _ := client.CreateForumTopic(
context.TODO(),
_chatID,
fmt.Sprintf("forum topic with chat_id: %s", _chatID),
OptionsCreateForumTopic{},
); created.OK {
_messageThreadID := created.Result.MessageThreadID
// EditForumTopic
if edited, _ := client.EditForumTopic(
context.TODO(),
_chatID,
_messageThreadID,
OptionsEditForumTopic{}.
SetName(
fmt.Sprintf(
"updated forum topic with chat_id: %s, message_thread_id: %d",
_chatID,
_messageThreadID,
),
),
); !edited.OK {
t.Errorf("failed to edit forum topic: %s", *edited.Description)
}
// UnpinAllForumTopicMessages
if unpinned, _ := client.UnpinAllForumTopicMessages(
context.TODO(),
_chatID,
_messageThreadID,
); !unpinned.OK {
t.Errorf("failed to unpin all forum topic messages: %s", *unpinned.Description)
}
// DeleteForumTopic
if deleted, _ := client.DeleteForumTopic(
context.TODO(),
_chatID,
_messageThreadID,
); !deleted.OK {
t.Errorf("failed to delete forum topic: %s", *deleted.Description)
}
} else {
t.Errorf("failed to create forum topic: %s", *created.Description)
}
// TODO: CloseForumTopic
// TODO: ReopenForumTopic
// TODO: EditGeneralForumTopic
// TODO: CloseGeneralForumTopic
// TODO: ReopenGeneralForumTopic
// TODO: HideGeneralForumTopic
// TODO: UnhideGeneralForumTopic
// TODO: UnpinAllGeneralForumTopicMessages
// TODO: GetForumTopicIconStickers
////////////////////////////////
// (game)
//
// TODO: SendGame
// TODO: SetGameScore
// TODO: GetGameHighScores
////////////////////////////////
// (reaction)
//
// TODO: SetMessageReaction
// TODO: DeleteMessageReaction
// TODO: DeleteAllMessageReactions
////////////////////////////////
// (gift)
//
// TODO: GetAvailableGifts
// TODO: SendGift
// TODO: GiftPremiumSubscription
////////////////////////////////
// (verification)
//
// TODO: VerifyUser
// TODO: RemoveUserVerification
// TODO: VerifyChat
// TODO: RemoveChatVerification
////////////////////////////////
// (managed bot)
//
// TODO: GetManagedBotToken
// TODO: ReplaceManagedBotToken
// TODO: GetManagedBotAccessSettings
// TODO: SetManagedBotAccessSettings
////////////////////////////////
// (webapp)
//
// TODO: AnswerWebAppQuery
// TODO: SetUserEmojiStatus
// TODO: SavePreparedInlineMessage
// TODO: SavePreparedKeyboardButton
}
}
}
// test (un)classified errors
func TestErrors(t *testing.T) {
_token := os.Getenv("TOKEN")
_chatID := os.Getenv("CHAT_ID") // NOTE: `chat_id` of a group chat
_verbose := os.Getenv("VERBOSE")
client := NewClient(_token)
client.Verbose = _verbose == "true"
if len(_token) <= 0 || len(_chatID) <= 0 {
slog.Warn("skipping test: environment variables `TOKEN` and `CHAT_ID` are needed")
t.Skip("environment variables `TOKEN` and `CHAT_ID` are needed")
}
slog.Info("testing classification of errors...")
// ErrUnauthorized
unauthClient := NewClient("000000000:UNAUTHORIZEDabcdefghijklmnopqrs-0_Z")
unauthClient.Verbose = _verbose == "true"
if sent, err := unauthClient.SendMessage(
context.TODO(),
_chatID,
"unauthorized",
OptionsSendMessage{},
); !sent.OK {
if _, ok := errors.AsType[ErrUnauthorized](err); !ok {
t.Errorf("should have failed with ErrUnauthorized, but got: %s", err)
}
} else {
t.Errorf("should have failed to send unauthorized request")
}
// ErrChatNotFound
if sent, err := client.SendMessage(
context.TODO(),
0,
"no-such-chat",
OptionsSendMessage{},
); !sent.OK {
if _, ok := errors.AsType[ErrChatNotFound](err); !ok {
t.Errorf("should have failed with ErrChatNotFound, but got: %s", err)
}
} else {
t.Errorf("should have failed to send message to a non-existent chat")
}
// TODO: ErrUserNotFound
// TODO: ErrUserDeactivated
// TODO: ErrBotKicked
// TODO: ErrBotBlockedByUser
// TODO: ErrBotCantSendToBots
// TODO: ErrMessageNotModified
// TODO: ErrGroupMigratedToSupergroup
// TODO: ErrInvalidFileID
// TODO: ErrConflictedLongPoll
// TODO: ErrConflictedWebHook
// TODO: ErrWrongParameterAction
// ErrMessageEmpty
if sent, err := client.SendMessage(
context.TODO(),
_chatID,
"",
OptionsSendMessage{},
); !sent.OK {
if _, ok := errors.AsType[ErrMessageEmpty](err); !ok {
t.Errorf("should have failed with ErrMessageEmpty but got: %s", err)
}
} else {
t.Errorf("should have failed to send an empty message")
}
// ErrMessageTooLong
longLongMessage := strings.Repeat("a", 4097)
if sent, err := client.SendMessage(
context.TODO(),
_chatID,
longLongMessage,
OptionsSendMessage{},
); !sent.OK {
if _, ok := errors.AsType[ErrMessageTooLong](err); !ok {
t.Errorf("should have failed with ErrMessageTooLong but got: %s", err)
}
} else {
t.Errorf("should have failed to send a long message")
}
// ErrMessageCantBeEdited
// TODO: ErrMessageCantBeEdited
// ErrTooManyRequests
// TODO: ErrTooManyRequests
// ErrJSONParseFailed
// TODO: ErrJSONParseFailed
// TODO: add more errors here
// ErrUnclassified
// TODO: ErrUnclassified
}