all repos — telegram-bot-api @ 53d566ba56fb4b9925de3aeb74726e870f8d2e1d

Golang bindings for the Telegram Bot API

bot_test.go (view raw)

   1package tgbotapi
   2
   3import (
   4	"io/ioutil"
   5	"net/http"
   6	"os"
   7	"testing"
   8	"time"
   9)
  10
  11const (
  12	TestToken               = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
  13	ChatID                  = 76918703
  14	Channel                 = "@tgbotapitest"
  15	SupergroupChatID        = -1001120141283
  16	ReplyToMessageID        = 35
  17	ExistingPhotoFileID     = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"
  18	ExistingDocumentFileID  = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
  19	ExistingAudioFileID     = "BQADAgADRgADjMcoCdXg3lSIN49lAg"
  20	ExistingVoiceFileID     = "AwADAgADWQADjMcoCeul6r_q52IyAg"
  21	ExistingVideoFileID     = "BAADAgADZgADjMcoCav432kYe0FRAg"
  22	ExistingVideoNoteFileID = "DQADAgADdQAD70cQSUK41dLsRMqfAg"
  23	ExistingStickerFileID   = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
  24)
  25
  26type testLogger struct {
  27	t *testing.T
  28}
  29
  30func (t testLogger) Println(v ...interface{}) {
  31	t.t.Log(v...)
  32}
  33
  34func (t testLogger) Printf(format string, v ...interface{}) {
  35	t.t.Logf(format, v...)
  36}
  37
  38func getBot(t *testing.T) (*BotAPI, error) {
  39	bot, err := NewBotAPI(TestToken)
  40	bot.Debug = true
  41
  42	logger := testLogger{t}
  43	SetLogger(logger)
  44
  45	if err != nil {
  46		t.Error(err)
  47	}
  48
  49	return bot, err
  50}
  51
  52func TestNewBotAPI_notoken(t *testing.T) {
  53	_, err := NewBotAPI("")
  54
  55	if err == nil {
  56		t.Error(err)
  57	}
  58}
  59
  60func TestGetUpdates(t *testing.T) {
  61	bot, _ := getBot(t)
  62
  63	u := NewUpdate(0)
  64
  65	_, err := bot.GetUpdates(u)
  66
  67	if err != nil {
  68		t.Error(err)
  69	}
  70}
  71
  72func TestSendWithMessage(t *testing.T) {
  73	bot, _ := getBot(t)
  74
  75	msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
  76	msg.ParseMode = ModeMarkdown
  77	_, err := bot.Send(msg)
  78
  79	if err != nil {
  80		t.Error(err)
  81	}
  82}
  83
  84func TestSendWithMessageReply(t *testing.T) {
  85	bot, _ := getBot(t)
  86
  87	msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
  88	msg.ReplyToMessageID = ReplyToMessageID
  89	_, err := bot.Send(msg)
  90
  91	if err != nil {
  92		t.Error(err)
  93	}
  94}
  95
  96func TestSendWithMessageForward(t *testing.T) {
  97	bot, _ := getBot(t)
  98
  99	msg := NewForward(ChatID, ChatID, ReplyToMessageID)
 100	_, err := bot.Send(msg)
 101
 102	if err != nil {
 103		t.Error(err)
 104	}
 105}
 106
 107func TestCopyMessage(t *testing.T) {
 108	bot, _ := getBot(t)
 109
 110	msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
 111	message, err := bot.Send(msg)
 112	if err != nil {
 113		t.Error(err)
 114	}
 115
 116	copyMessageConfig := NewCopyMessage(SupergroupChatID, message.Chat.ID, message.MessageID)
 117	messageID, err := bot.CopyMessage(copyMessageConfig)
 118	if err != nil {
 119		t.Error(err)
 120	}
 121
 122	if messageID.MessageID == message.MessageID {
 123		t.Error("copied message ID was the same as original message")
 124	}
 125}
 126
 127func TestSendWithNewPhoto(t *testing.T) {
 128	bot, _ := getBot(t)
 129
 130	msg := NewPhoto(ChatID, "tests/image.jpg")
 131	msg.Caption = "Test"
 132	_, err := bot.Send(msg)
 133
 134	if err != nil {
 135		t.Error(err)
 136	}
 137}
 138
 139func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
 140	bot, _ := getBot(t)
 141
 142	data, _ := ioutil.ReadFile("tests/image.jpg")
 143	b := FileBytes{Name: "image.jpg", Bytes: data}
 144
 145	msg := NewPhoto(ChatID, b)
 146	msg.Caption = "Test"
 147	_, err := bot.Send(msg)
 148
 149	if err != nil {
 150		t.Error(err)
 151	}
 152}
 153
 154func TestSendWithNewPhotoWithFileReader(t *testing.T) {
 155	bot, _ := getBot(t)
 156
 157	f, _ := os.Open("tests/image.jpg")
 158	reader := FileReader{Name: "image.jpg", Reader: f}
 159
 160	msg := NewPhoto(ChatID, reader)
 161	msg.Caption = "Test"
 162	_, err := bot.Send(msg)
 163
 164	if err != nil {
 165		t.Error(err)
 166	}
 167}
 168
 169func TestSendWithNewPhotoReply(t *testing.T) {
 170	bot, _ := getBot(t)
 171
 172	msg := NewPhoto(ChatID, "tests/image.jpg")
 173	msg.ReplyToMessageID = ReplyToMessageID
 174
 175	_, err := bot.Send(msg)
 176
 177	if err != nil {
 178		t.Error(err)
 179	}
 180}
 181
 182func TestSendNewPhotoToChannel(t *testing.T) {
 183	bot, _ := getBot(t)
 184
 185	msg := NewPhotoToChannel(Channel, "tests/image.jpg")
 186	msg.Caption = "Test"
 187	_, err := bot.Send(msg)
 188
 189	if err != nil {
 190		t.Error(err)
 191		t.Fail()
 192	}
 193}
 194
 195func TestSendNewPhotoToChannelFileBytes(t *testing.T) {
 196	bot, _ := getBot(t)
 197
 198	data, _ := ioutil.ReadFile("tests/image.jpg")
 199	b := FileBytes{Name: "image.jpg", Bytes: data}
 200
 201	msg := NewPhotoToChannel(Channel, b)
 202	msg.Caption = "Test"
 203	_, err := bot.Send(msg)
 204
 205	if err != nil {
 206		t.Error(err)
 207		t.Fail()
 208	}
 209}
 210
 211func TestSendNewPhotoToChannelFileReader(t *testing.T) {
 212	bot, _ := getBot(t)
 213
 214	f, _ := os.Open("tests/image.jpg")
 215	reader := FileReader{Name: "image.jpg", Reader: f}
 216
 217	msg := NewPhotoToChannel(Channel, reader)
 218	msg.Caption = "Test"
 219	_, err := bot.Send(msg)
 220
 221	if err != nil {
 222		t.Error(err)
 223		t.Fail()
 224	}
 225}
 226
 227func TestSendWithExistingPhoto(t *testing.T) {
 228	bot, _ := getBot(t)
 229
 230	msg := NewPhoto(ChatID, FileID(ExistingPhotoFileID))
 231	msg.Caption = "Test"
 232	_, err := bot.Send(msg)
 233
 234	if err != nil {
 235		t.Error(err)
 236	}
 237}
 238
 239func TestSendWithNewDocument(t *testing.T) {
 240	bot, _ := getBot(t)
 241
 242	msg := NewDocument(ChatID, "tests/image.jpg")
 243	_, err := bot.Send(msg)
 244
 245	if err != nil {
 246		t.Error(err)
 247	}
 248}
 249
 250func TestSendWithNewDocumentAndThumb(t *testing.T) {
 251	bot, _ := getBot(t)
 252
 253	msg := NewDocument(ChatID, "tests/voice.ogg")
 254	msg.Thumb = "tests/image.jpg"
 255	_, err := bot.Send(msg)
 256
 257	if err != nil {
 258		t.Error(err)
 259	}
 260}
 261
 262func TestSendWithExistingDocument(t *testing.T) {
 263	bot, _ := getBot(t)
 264
 265	msg := NewDocument(ChatID, FileID(ExistingDocumentFileID))
 266	_, err := bot.Send(msg)
 267
 268	if err != nil {
 269		t.Error(err)
 270	}
 271}
 272
 273func TestSendWithNewAudio(t *testing.T) {
 274	bot, _ := getBot(t)
 275
 276	msg := NewAudio(ChatID, "tests/audio.mp3")
 277	msg.Title = "TEST"
 278	msg.Duration = 10
 279	msg.Performer = "TEST"
 280	_, err := bot.Send(msg)
 281
 282	if err != nil {
 283		t.Error(err)
 284	}
 285}
 286
 287func TestSendWithExistingAudio(t *testing.T) {
 288	bot, _ := getBot(t)
 289
 290	msg := NewAudio(ChatID, FileID(ExistingAudioFileID))
 291	msg.Title = "TEST"
 292	msg.Duration = 10
 293	msg.Performer = "TEST"
 294
 295	_, err := bot.Send(msg)
 296
 297	if err != nil {
 298		t.Error(err)
 299	}
 300}
 301
 302func TestSendWithNewVoice(t *testing.T) {
 303	bot, _ := getBot(t)
 304
 305	msg := NewVoice(ChatID, "tests/voice.ogg")
 306	msg.Duration = 10
 307	_, err := bot.Send(msg)
 308
 309	if err != nil {
 310		t.Error(err)
 311	}
 312}
 313
 314func TestSendWithExistingVoice(t *testing.T) {
 315	bot, _ := getBot(t)
 316
 317	msg := NewVoice(ChatID, FileID(ExistingVoiceFileID))
 318	msg.Duration = 10
 319	_, err := bot.Send(msg)
 320
 321	if err != nil {
 322		t.Error(err)
 323	}
 324}
 325
 326func TestSendWithContact(t *testing.T) {
 327	bot, _ := getBot(t)
 328
 329	contact := NewContact(ChatID, "5551234567", "Test")
 330
 331	if _, err := bot.Send(contact); err != nil {
 332		t.Error(err)
 333	}
 334}
 335
 336func TestSendWithLocation(t *testing.T) {
 337	bot, _ := getBot(t)
 338
 339	_, err := bot.Send(NewLocation(ChatID, 40, 40))
 340
 341	if err != nil {
 342		t.Error(err)
 343	}
 344}
 345
 346func TestSendWithVenue(t *testing.T) {
 347	bot, _ := getBot(t)
 348
 349	venue := NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
 350
 351	if _, err := bot.Send(venue); err != nil {
 352		t.Error(err)
 353	}
 354}
 355
 356func TestSendWithNewVideo(t *testing.T) {
 357	bot, _ := getBot(t)
 358
 359	msg := NewVideo(ChatID, "tests/video.mp4")
 360	msg.Duration = 10
 361	msg.Caption = "TEST"
 362
 363	_, err := bot.Send(msg)
 364
 365	if err != nil {
 366		t.Error(err)
 367	}
 368}
 369
 370func TestSendWithExistingVideo(t *testing.T) {
 371	bot, _ := getBot(t)
 372
 373	msg := NewVideo(ChatID, FileID(ExistingVideoFileID))
 374	msg.Duration = 10
 375	msg.Caption = "TEST"
 376
 377	_, err := bot.Send(msg)
 378
 379	if err != nil {
 380		t.Error(err)
 381	}
 382}
 383
 384func TestSendWithNewVideoNote(t *testing.T) {
 385	bot, _ := getBot(t)
 386
 387	msg := NewVideoNote(ChatID, 240, "tests/videonote.mp4")
 388	msg.Duration = 10
 389
 390	_, err := bot.Send(msg)
 391
 392	if err != nil {
 393		t.Error(err)
 394	}
 395}
 396
 397func TestSendWithExistingVideoNote(t *testing.T) {
 398	bot, _ := getBot(t)
 399
 400	msg := NewVideoNote(ChatID, 240, FileID(ExistingVideoNoteFileID))
 401	msg.Duration = 10
 402
 403	_, err := bot.Send(msg)
 404
 405	if err != nil {
 406		t.Error(err)
 407	}
 408}
 409
 410func TestSendWithNewSticker(t *testing.T) {
 411	bot, _ := getBot(t)
 412
 413	msg := NewSticker(ChatID, "tests/image.jpg")
 414
 415	_, err := bot.Send(msg)
 416
 417	if err != nil {
 418		t.Error(err)
 419	}
 420}
 421
 422func TestSendWithExistingSticker(t *testing.T) {
 423	bot, _ := getBot(t)
 424
 425	msg := NewSticker(ChatID, FileID(ExistingStickerFileID))
 426
 427	_, err := bot.Send(msg)
 428
 429	if err != nil {
 430		t.Error(err)
 431	}
 432}
 433
 434func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
 435	bot, _ := getBot(t)
 436
 437	msg := NewSticker(ChatID, "tests/image.jpg")
 438	msg.ReplyMarkup = ReplyKeyboardRemove{
 439		RemoveKeyboard: true,
 440		Selective:      false,
 441	}
 442	_, err := bot.Send(msg)
 443
 444	if err != nil {
 445		t.Error(err)
 446	}
 447}
 448
 449func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
 450	bot, _ := getBot(t)
 451
 452	msg := NewSticker(ChatID, FileID(ExistingStickerFileID))
 453	msg.ReplyMarkup = ReplyKeyboardRemove{
 454		RemoveKeyboard: true,
 455		Selective:      false,
 456	}
 457
 458	_, err := bot.Send(msg)
 459
 460	if err != nil {
 461		t.Error(err)
 462	}
 463}
 464
 465func TestSendWithDice(t *testing.T) {
 466	bot, _ := getBot(t)
 467
 468	msg := NewDice(ChatID)
 469	_, err := bot.Send(msg)
 470
 471	if err != nil {
 472		t.Error(err)
 473		t.Fail()
 474	}
 475
 476}
 477
 478func TestSendWithDiceWithEmoji(t *testing.T) {
 479	bot, _ := getBot(t)
 480
 481	msg := NewDiceWithEmoji(ChatID, "🏀")
 482	_, err := bot.Send(msg)
 483
 484	if err != nil {
 485		t.Error(err)
 486		t.Fail()
 487	}
 488
 489}
 490
 491func TestGetFile(t *testing.T) {
 492	bot, _ := getBot(t)
 493
 494	file := FileConfig{
 495		FileID: ExistingPhotoFileID,
 496	}
 497
 498	_, err := bot.GetFile(file)
 499
 500	if err != nil {
 501		t.Error(err)
 502	}
 503}
 504
 505func TestSendChatConfig(t *testing.T) {
 506	bot, _ := getBot(t)
 507
 508	_, err := bot.Request(NewChatAction(ChatID, ChatTyping))
 509
 510	if err != nil {
 511		t.Error(err)
 512	}
 513}
 514
 515func TestSendEditMessage(t *testing.T) {
 516	bot, _ := getBot(t)
 517
 518	msg, err := bot.Send(NewMessage(ChatID, "Testing editing."))
 519	if err != nil {
 520		t.Error(err)
 521	}
 522
 523	edit := EditMessageTextConfig{
 524		BaseEdit: BaseEdit{
 525			ChatID:    ChatID,
 526			MessageID: msg.MessageID,
 527		},
 528		Text: "Updated text.",
 529	}
 530
 531	_, err = bot.Send(edit)
 532	if err != nil {
 533		t.Error(err)
 534	}
 535}
 536
 537func TestGetUserProfilePhotos(t *testing.T) {
 538	bot, _ := getBot(t)
 539
 540	_, err := bot.GetUserProfilePhotos(NewUserProfilePhotos(ChatID))
 541	if err != nil {
 542		t.Error(err)
 543	}
 544}
 545
 546func TestSetWebhookWithCert(t *testing.T) {
 547	bot, _ := getBot(t)
 548
 549	time.Sleep(time.Second * 2)
 550
 551	bot.Request(DeleteWebhookConfig{})
 552
 553	wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
 554
 555	if err != nil {
 556		t.Error(err)
 557	}
 558	_, err = bot.Request(wh)
 559
 560	if err != nil {
 561		t.Error(err)
 562	}
 563
 564	_, err = bot.GetWebhookInfo()
 565
 566	if err != nil {
 567		t.Error(err)
 568	}
 569
 570	bot.Request(DeleteWebhookConfig{})
 571}
 572
 573func TestSetWebhookWithoutCert(t *testing.T) {
 574	bot, _ := getBot(t)
 575
 576	time.Sleep(time.Second * 2)
 577
 578	bot.Request(DeleteWebhookConfig{})
 579
 580	wh, err := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
 581
 582	if err != nil {
 583		t.Error(err)
 584	}
 585
 586	_, err = bot.Request(wh)
 587
 588	if err != nil {
 589		t.Error(err)
 590	}
 591
 592	info, err := bot.GetWebhookInfo()
 593
 594	if err != nil {
 595		t.Error(err)
 596	}
 597	if info.MaxConnections == 0 {
 598		t.Errorf("Expected maximum connections to be greater than 0")
 599	}
 600	if info.LastErrorDate != 0 {
 601		t.Errorf("failed to set webhook: %s", info.LastErrorMessage)
 602	}
 603
 604	bot.Request(DeleteWebhookConfig{})
 605}
 606
 607func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
 608	bot, _ := getBot(t)
 609
 610	cfg := NewMediaGroup(ChatID, []interface{}{
 611		NewInputMediaPhoto(FileURL("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/image.jpg")),
 612		NewInputMediaPhoto("tests/image.jpg"),
 613		NewInputMediaVideo("tests/video.mp4"),
 614	})
 615
 616	messages, err := bot.SendMediaGroup(cfg)
 617	if err != nil {
 618		t.Error(err)
 619	}
 620
 621	if messages == nil {
 622		t.Error("No received messages")
 623	}
 624
 625	if len(messages) != len(cfg.Media) {
 626		t.Errorf("Different number of messages: %d", len(messages))
 627	}
 628}
 629
 630func TestSendWithMediaGroupDocument(t *testing.T) {
 631	bot, _ := getBot(t)
 632
 633	cfg := NewMediaGroup(ChatID, []interface{}{
 634		NewInputMediaDocument(FileURL("https://i.imgur.com/unQLJIb.jpg")),
 635		NewInputMediaDocument("tests/image.jpg"),
 636	})
 637
 638	messages, err := bot.SendMediaGroup(cfg)
 639	if err != nil {
 640		t.Error(err)
 641	}
 642
 643	if messages == nil {
 644		t.Error("No received messages")
 645	}
 646
 647	if len(messages) != len(cfg.Media) {
 648		t.Errorf("Different number of messages: %d", len(messages))
 649	}
 650}
 651
 652func TestSendWithMediaGroupAudio(t *testing.T) {
 653	bot, _ := getBot(t)
 654
 655	cfg := NewMediaGroup(ChatID, []interface{}{
 656		NewInputMediaAudio("tests/audio.mp3"),
 657		NewInputMediaAudio("tests/audio.mp3"),
 658	})
 659
 660	messages, err := bot.SendMediaGroup(cfg)
 661	if err != nil {
 662		t.Error(err)
 663	}
 664
 665	if messages == nil {
 666		t.Error("No received messages")
 667	}
 668
 669	if len(messages) != len(cfg.Media) {
 670		t.Errorf("Different number of messages: %d", len(messages))
 671	}
 672}
 673
 674func ExampleNewBotAPI() {
 675	bot, err := NewBotAPI("MyAwesomeBotToken")
 676	if err != nil {
 677		panic(err)
 678	}
 679
 680	bot.Debug = true
 681
 682	log.Printf("Authorized on account %s", bot.Self.UserName)
 683
 684	u := NewUpdate(0)
 685	u.Timeout = 60
 686
 687	updates := bot.GetUpdatesChan(u)
 688
 689	// Optional: wait for updates and clear them if you don't want to handle
 690	// a large backlog of old messages
 691	time.Sleep(time.Millisecond * 500)
 692	updates.Clear()
 693
 694	for update := range updates {
 695		if update.Message == nil {
 696			continue
 697		}
 698
 699		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
 700
 701		msg := NewMessage(update.Message.Chat.ID, update.Message.Text)
 702		msg.ReplyToMessageID = update.Message.MessageID
 703
 704		bot.Send(msg)
 705	}
 706}
 707
 708func ExampleNewWebhook() {
 709	bot, err := NewBotAPI("MyAwesomeBotToken")
 710	if err != nil {
 711		panic(err)
 712	}
 713
 714	bot.Debug = true
 715
 716	log.Printf("Authorized on account %s", bot.Self.UserName)
 717
 718	wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
 719
 720	if err != nil {
 721		panic(err)
 722	}
 723
 724	_, err = bot.Request(wh)
 725
 726	if err != nil {
 727		panic(err)
 728	}
 729
 730	info, err := bot.GetWebhookInfo()
 731
 732	if err != nil {
 733		panic(err)
 734	}
 735
 736	if info.LastErrorDate != 0 {
 737		log.Printf("failed to set webhook: %s", info.LastErrorMessage)
 738	}
 739
 740	updates := bot.ListenForWebhook("/" + bot.Token)
 741	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
 742
 743	for update := range updates {
 744		log.Printf("%+v\n", update)
 745	}
 746}
 747
 748func ExampleWebhookHandler() {
 749	bot, err := NewBotAPI("MyAwesomeBotToken")
 750	if err != nil {
 751		panic(err)
 752	}
 753
 754	bot.Debug = true
 755
 756	log.Printf("Authorized on account %s", bot.Self.UserName)
 757
 758	wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
 759
 760	if err != nil {
 761		panic(err)
 762	}
 763
 764	_, err = bot.Request(wh)
 765	if err != nil {
 766		panic(err)
 767	}
 768	info, err := bot.GetWebhookInfo()
 769	if err != nil {
 770		panic(err)
 771	}
 772	if info.LastErrorDate != 0 {
 773		log.Printf("[Telegram callback failed]%s", info.LastErrorMessage)
 774	}
 775
 776	http.HandleFunc("/"+bot.Token, func(w http.ResponseWriter, r *http.Request) {
 777		update, err := bot.HandleUpdate(r)
 778		if err != nil {
 779			log.Printf("%+v\n", err.Error())
 780		} else {
 781			log.Printf("%+v\n", *update)
 782		}
 783	})
 784
 785	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
 786}
 787
 788func ExampleInlineConfig() {
 789	bot, err := NewBotAPI("MyAwesomeBotToken") // create new bot
 790	if err != nil {
 791		panic(err)
 792	}
 793
 794	log.Printf("Authorized on account %s", bot.Self.UserName)
 795
 796	u := NewUpdate(0)
 797	u.Timeout = 60
 798
 799	updates := bot.GetUpdatesChan(u)
 800
 801	for update := range updates {
 802		if update.InlineQuery == nil { // if no inline query, ignore it
 803			continue
 804		}
 805
 806		article := NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
 807		article.Description = update.InlineQuery.Query
 808
 809		inlineConf := InlineConfig{
 810			InlineQueryID: update.InlineQuery.ID,
 811			IsPersonal:    true,
 812			CacheTime:     0,
 813			Results:       []interface{}{article},
 814		}
 815
 816		if _, err := bot.Request(inlineConf); err != nil {
 817			log.Println(err)
 818		}
 819	}
 820}
 821
 822func TestDeleteMessage(t *testing.T) {
 823	bot, _ := getBot(t)
 824
 825	msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
 826	msg.ParseMode = ModeMarkdown
 827	message, _ := bot.Send(msg)
 828
 829	deleteMessageConfig := DeleteMessageConfig{
 830		ChatID:    message.Chat.ID,
 831		MessageID: message.MessageID,
 832	}
 833	_, err := bot.Request(deleteMessageConfig)
 834
 835	if err != nil {
 836		t.Error(err)
 837	}
 838}
 839
 840func TestPinChatMessage(t *testing.T) {
 841	bot, _ := getBot(t)
 842
 843	msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
 844	msg.ParseMode = ModeMarkdown
 845	message, _ := bot.Send(msg)
 846
 847	pinChatMessageConfig := PinChatMessageConfig{
 848		ChatID:              message.Chat.ID,
 849		MessageID:           message.MessageID,
 850		DisableNotification: false,
 851	}
 852	_, err := bot.Request(pinChatMessageConfig)
 853
 854	if err != nil {
 855		t.Error(err)
 856	}
 857}
 858
 859func TestUnpinChatMessage(t *testing.T) {
 860	bot, _ := getBot(t)
 861
 862	msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
 863	msg.ParseMode = ModeMarkdown
 864	message, _ := bot.Send(msg)
 865
 866	// We need pin message to unpin something
 867	pinChatMessageConfig := PinChatMessageConfig{
 868		ChatID:              message.Chat.ID,
 869		MessageID:           message.MessageID,
 870		DisableNotification: false,
 871	}
 872
 873	if _, err := bot.Request(pinChatMessageConfig); err != nil {
 874		t.Error(err)
 875	}
 876
 877	unpinChatMessageConfig := UnpinChatMessageConfig{
 878		ChatID:    message.Chat.ID,
 879		MessageID: message.MessageID,
 880	}
 881
 882	if _, err := bot.Request(unpinChatMessageConfig); err != nil {
 883		t.Error(err)
 884	}
 885}
 886
 887func TestUnpinAllChatMessages(t *testing.T) {
 888	bot, _ := getBot(t)
 889
 890	msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
 891	msg.ParseMode = ModeMarkdown
 892	message, _ := bot.Send(msg)
 893
 894	pinChatMessageConfig := PinChatMessageConfig{
 895		ChatID:              message.Chat.ID,
 896		MessageID:           message.MessageID,
 897		DisableNotification: true,
 898	}
 899
 900	if _, err := bot.Request(pinChatMessageConfig); err != nil {
 901		t.Error(err)
 902	}
 903
 904	unpinAllChatMessagesConfig := UnpinAllChatMessagesConfig{
 905		ChatID: message.Chat.ID,
 906	}
 907
 908	if _, err := bot.Request(unpinAllChatMessagesConfig); err != nil {
 909		t.Error(err)
 910	}
 911}
 912
 913func TestPolls(t *testing.T) {
 914	bot, _ := getBot(t)
 915
 916	poll := NewPoll(SupergroupChatID, "Are polls working?", "Yes", "No")
 917
 918	msg, err := bot.Send(poll)
 919	if err != nil {
 920		t.Error(err)
 921	}
 922
 923	result, err := bot.StopPoll(NewStopPoll(SupergroupChatID, msg.MessageID))
 924	if err != nil {
 925		t.Error(err)
 926	}
 927
 928	if result.Question != "Are polls working?" {
 929		t.Error("Poll question did not match")
 930	}
 931
 932	if !result.IsClosed {
 933		t.Error("Poll did not end")
 934	}
 935
 936	if result.Options[0].Text != "Yes" || result.Options[0].VoterCount != 0 || result.Options[1].Text != "No" || result.Options[1].VoterCount != 0 {
 937		t.Error("Poll options were incorrect")
 938	}
 939}
 940
 941func TestSendDice(t *testing.T) {
 942	bot, _ := getBot(t)
 943
 944	dice := NewSendDice(ChatID)
 945
 946	msg, err := bot.Send(dice)
 947	if err != nil {
 948		t.Error("Unable to send dice roll")
 949	}
 950
 951	if msg.Dice == nil {
 952		t.Error("Dice roll was not received")
 953	}
 954}
 955
 956func TestSetCommands(t *testing.T) {
 957	bot, _ := getBot(t)
 958
 959	setCommands := NewSetMyCommands(BotCommand{
 960		Command:     "test",
 961		Description: "a test command",
 962	})
 963
 964	if _, err := bot.Request(setCommands); err != nil {
 965		t.Error("Unable to set commands")
 966	}
 967
 968	commands, err := bot.GetMyCommands()
 969	if err != nil {
 970		t.Error("Unable to get commands")
 971	}
 972
 973	if len(commands) != 1 {
 974		t.Error("Incorrect number of commands returned")
 975	}
 976
 977	if commands[0].Command != "test" || commands[0].Description != "a test command" {
 978		t.Error("Commands were incorrectly set")
 979	}
 980}
 981
 982func TestEditMessageMedia(t *testing.T) {
 983	bot, _ := getBot(t)
 984
 985	msg := NewPhoto(ChatID, "tests/image.jpg")
 986	msg.Caption = "Test"
 987	m, err := bot.Send(msg)
 988
 989	if err != nil {
 990		t.Error(err)
 991	}
 992
 993	edit := EditMessageMediaConfig{
 994		BaseEdit: BaseEdit{
 995			ChatID:    ChatID,
 996			MessageID: m.MessageID,
 997		},
 998		Media: NewInputMediaVideo("tests/video.mp4"),
 999	}
1000
1001	_, err = bot.Request(edit)
1002	if err != nil {
1003		t.Error(err)
1004	}
1005}
1006
1007func TestPrepareInputMediaForParams(t *testing.T) {
1008	media := []interface{}{
1009		NewInputMediaPhoto("tests/image.jpg"),
1010		NewInputMediaVideo(FileID("test")),
1011	}
1012
1013	prepared := prepareInputMediaForParams(media)
1014
1015	if media[0].(InputMediaPhoto).Media != "tests/image.jpg" {
1016		t.Error("Original media was changed")
1017	}
1018
1019	if prepared[0].(InputMediaPhoto).Media != "attach://file-0" {
1020		t.Error("New media was not replaced")
1021	}
1022
1023	if prepared[1].(InputMediaVideo).Media != FileID("test") {
1024		t.Error("Passthrough value was not the same")
1025	}
1026}