github.com/alexis81/domosgo@v0.0.0-20191016125037-5aee90a434af/Domos/src/gopkg.in/telegram-bot-api.v4/bot_test.go (about)

     1  package tgbotapi_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"net/http"
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/go-telegram-bot-api/telegram-bot-api"
    12  )
    13  
    14  const (
    15  	TestToken               = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
    16  	ChatID                  = 76918703
    17  	SupergroupChatID        = -1001120141283
    18  	ReplyToMessageID        = 35
    19  	ExistingPhotoFileID     = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"
    20  	ExistingDocumentFileID  = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
    21  	ExistingAudioFileID     = "BQADAgADRgADjMcoCdXg3lSIN49lAg"
    22  	ExistingVoiceFileID     = "AwADAgADWQADjMcoCeul6r_q52IyAg"
    23  	ExistingVideoFileID     = "BAADAgADZgADjMcoCav432kYe0FRAg"
    24  	ExistingVideoNoteFileID = "DQADAgADdQAD70cQSUK41dLsRMqfAg"
    25  	ExistingStickerFileID   = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
    26  )
    27  
    28  func getBot(t *testing.T) (*tgbotapi.BotAPI, error) {
    29  	bot, err := tgbotapi.NewBotAPI(TestToken)
    30  	bot.Debug = true
    31  
    32  	if err != nil {
    33  		t.Error(err)
    34  		t.Fail()
    35  	}
    36  
    37  	return bot, err
    38  }
    39  
    40  func TestNewBotAPI_notoken(t *testing.T) {
    41  	_, err := tgbotapi.NewBotAPI("")
    42  
    43  	if err == nil {
    44  		t.Error(err)
    45  		t.Fail()
    46  	}
    47  }
    48  
    49  func TestGetUpdates(t *testing.T) {
    50  	bot, _ := getBot(t)
    51  
    52  	u := tgbotapi.NewUpdate(0)
    53  
    54  	_, err := bot.GetUpdates(u)
    55  
    56  	if err != nil {
    57  		t.Error(err)
    58  		t.Fail()
    59  	}
    60  }
    61  
    62  func TestSendWithMessage(t *testing.T) {
    63  	bot, _ := getBot(t)
    64  
    65  	msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
    66  	msg.ParseMode = "markdown"
    67  	_, err := bot.Send(msg)
    68  
    69  	if err != nil {
    70  		t.Error(err)
    71  		t.Fail()
    72  	}
    73  }
    74  
    75  func TestSendWithMessageReply(t *testing.T) {
    76  	bot, _ := getBot(t)
    77  
    78  	msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
    79  	msg.ReplyToMessageID = ReplyToMessageID
    80  	_, err := bot.Send(msg)
    81  
    82  	if err != nil {
    83  		t.Error(err)
    84  		t.Fail()
    85  	}
    86  }
    87  
    88  func TestSendWithMessageForward(t *testing.T) {
    89  	bot, _ := getBot(t)
    90  
    91  	msg := tgbotapi.NewForward(ChatID, ChatID, ReplyToMessageID)
    92  	_, err := bot.Send(msg)
    93  
    94  	if err != nil {
    95  		t.Error(err)
    96  		t.Fail()
    97  	}
    98  }
    99  
   100  func TestSendWithNewPhoto(t *testing.T) {
   101  	bot, _ := getBot(t)
   102  
   103  	msg := tgbotapi.NewPhotoUpload(ChatID, "tests/image.jpg")
   104  	msg.Caption = "Test"
   105  	_, err := bot.Send(msg)
   106  
   107  	if err != nil {
   108  		t.Error(err)
   109  		t.Fail()
   110  	}
   111  }
   112  
   113  func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
   114  	bot, _ := getBot(t)
   115  
   116  	data, _ := ioutil.ReadFile("tests/image.jpg")
   117  	b := tgbotapi.FileBytes{Name: "image.jpg", Bytes: data}
   118  
   119  	msg := tgbotapi.NewPhotoUpload(ChatID, b)
   120  	msg.Caption = "Test"
   121  	_, err := bot.Send(msg)
   122  
   123  	if err != nil {
   124  		t.Error(err)
   125  		t.Fail()
   126  	}
   127  }
   128  
   129  func TestSendWithNewPhotoWithFileReader(t *testing.T) {
   130  	bot, _ := getBot(t)
   131  
   132  	f, _ := os.Open("tests/image.jpg")
   133  	reader := tgbotapi.FileReader{Name: "image.jpg", Reader: f, Size: -1}
   134  
   135  	msg := tgbotapi.NewPhotoUpload(ChatID, reader)
   136  	msg.Caption = "Test"
   137  	_, err := bot.Send(msg)
   138  
   139  	if err != nil {
   140  		t.Error(err)
   141  		t.Fail()
   142  	}
   143  }
   144  
   145  func TestSendWithNewPhotoReply(t *testing.T) {
   146  	bot, _ := getBot(t)
   147  
   148  	msg := tgbotapi.NewPhotoUpload(ChatID, "tests/image.jpg")
   149  	msg.ReplyToMessageID = ReplyToMessageID
   150  
   151  	_, err := bot.Send(msg)
   152  
   153  	if err != nil {
   154  		t.Error(err)
   155  		t.Fail()
   156  	}
   157  }
   158  
   159  func TestSendWithExistingPhoto(t *testing.T) {
   160  	bot, _ := getBot(t)
   161  
   162  	msg := tgbotapi.NewPhotoShare(ChatID, ExistingPhotoFileID)
   163  	msg.Caption = "Test"
   164  	_, err := bot.Send(msg)
   165  
   166  	if err != nil {
   167  		t.Error(err)
   168  		t.Fail()
   169  	}
   170  }
   171  
   172  func TestSendWithNewDocument(t *testing.T) {
   173  	bot, _ := getBot(t)
   174  
   175  	msg := tgbotapi.NewDocumentUpload(ChatID, "tests/image.jpg")
   176  	_, err := bot.Send(msg)
   177  
   178  	if err != nil {
   179  		t.Error(err)
   180  		t.Fail()
   181  	}
   182  }
   183  
   184  func TestSendWithExistingDocument(t *testing.T) {
   185  	bot, _ := getBot(t)
   186  
   187  	msg := tgbotapi.NewDocumentShare(ChatID, ExistingDocumentFileID)
   188  	_, err := bot.Send(msg)
   189  
   190  	if err != nil {
   191  		t.Error(err)
   192  		t.Fail()
   193  	}
   194  }
   195  
   196  func TestSendWithNewAudio(t *testing.T) {
   197  	bot, _ := getBot(t)
   198  
   199  	msg := tgbotapi.NewAudioUpload(ChatID, "tests/audio.mp3")
   200  	msg.Title = "TEST"
   201  	msg.Duration = 10
   202  	msg.Performer = "TEST"
   203  	msg.MimeType = "audio/mpeg"
   204  	msg.FileSize = 688
   205  	_, err := bot.Send(msg)
   206  
   207  	if err != nil {
   208  		t.Error(err)
   209  		t.Fail()
   210  	}
   211  }
   212  
   213  func TestSendWithExistingAudio(t *testing.T) {
   214  	bot, _ := getBot(t)
   215  
   216  	msg := tgbotapi.NewAudioShare(ChatID, ExistingAudioFileID)
   217  	msg.Title = "TEST"
   218  	msg.Duration = 10
   219  	msg.Performer = "TEST"
   220  
   221  	_, err := bot.Send(msg)
   222  
   223  	if err != nil {
   224  		t.Error(err)
   225  		t.Fail()
   226  	}
   227  }
   228  
   229  func TestSendWithNewVoice(t *testing.T) {
   230  	bot, _ := getBot(t)
   231  
   232  	msg := tgbotapi.NewVoiceUpload(ChatID, "tests/voice.ogg")
   233  	msg.Duration = 10
   234  	_, err := bot.Send(msg)
   235  
   236  	if err != nil {
   237  		t.Error(err)
   238  		t.Fail()
   239  	}
   240  }
   241  
   242  func TestSendWithExistingVoice(t *testing.T) {
   243  	bot, _ := getBot(t)
   244  
   245  	msg := tgbotapi.NewVoiceShare(ChatID, ExistingVoiceFileID)
   246  	msg.Duration = 10
   247  	_, err := bot.Send(msg)
   248  
   249  	if err != nil {
   250  		t.Error(err)
   251  		t.Fail()
   252  	}
   253  }
   254  
   255  func TestSendWithContact(t *testing.T) {
   256  	bot, _ := getBot(t)
   257  
   258  	contact := tgbotapi.NewContact(ChatID, "5551234567", "Test")
   259  
   260  	if _, err := bot.Send(contact); err != nil {
   261  		t.Error(err)
   262  		t.Fail()
   263  	}
   264  }
   265  
   266  func TestSendWithLocation(t *testing.T) {
   267  	bot, _ := getBot(t)
   268  
   269  	_, err := bot.Send(tgbotapi.NewLocation(ChatID, 40, 40))
   270  
   271  	if err != nil {
   272  		t.Error(err)
   273  		t.Fail()
   274  	}
   275  }
   276  
   277  func TestSendWithVenue(t *testing.T) {
   278  	bot, _ := getBot(t)
   279  
   280  	venue := tgbotapi.NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
   281  
   282  	if _, err := bot.Send(venue); err != nil {
   283  		t.Error(err)
   284  		t.Fail()
   285  	}
   286  }
   287  
   288  func TestSendWithNewVideo(t *testing.T) {
   289  	bot, _ := getBot(t)
   290  
   291  	msg := tgbotapi.NewVideoUpload(ChatID, "tests/video.mp4")
   292  	msg.Duration = 10
   293  	msg.Caption = "TEST"
   294  
   295  	_, err := bot.Send(msg)
   296  
   297  	if err != nil {
   298  		t.Error(err)
   299  		t.Fail()
   300  	}
   301  }
   302  
   303  func TestSendWithExistingVideo(t *testing.T) {
   304  	bot, _ := getBot(t)
   305  
   306  	msg := tgbotapi.NewVideoShare(ChatID, ExistingVideoFileID)
   307  	msg.Duration = 10
   308  	msg.Caption = "TEST"
   309  
   310  	_, err := bot.Send(msg)
   311  
   312  	if err != nil {
   313  		t.Error(err)
   314  		t.Fail()
   315  	}
   316  }
   317  
   318  func TestSendWithNewVideoNote(t *testing.T) {
   319  	bot, _ := getBot(t)
   320  
   321  	msg := tgbotapi.NewVideoNoteUpload(ChatID, 240, "tests/videonote.mp4")
   322  	msg.Duration = 10
   323  
   324  	_, err := bot.Send(msg)
   325  
   326  	if err != nil {
   327  		t.Error(err)
   328  		t.Fail()
   329  	}
   330  }
   331  
   332  func TestSendWithExistingVideoNote(t *testing.T) {
   333  	bot, _ := getBot(t)
   334  
   335  	msg := tgbotapi.NewVideoNoteShare(ChatID, 240, ExistingVideoNoteFileID)
   336  	msg.Duration = 10
   337  
   338  	_, err := bot.Send(msg)
   339  
   340  	if err != nil {
   341  		t.Error(err)
   342  		t.Fail()
   343  	}
   344  }
   345  
   346  func TestSendWithNewSticker(t *testing.T) {
   347  	bot, _ := getBot(t)
   348  
   349  	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
   350  
   351  	_, err := bot.Send(msg)
   352  
   353  	if err != nil {
   354  		t.Error(err)
   355  		t.Fail()
   356  	}
   357  }
   358  
   359  func TestSendWithExistingSticker(t *testing.T) {
   360  	bot, _ := getBot(t)
   361  
   362  	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
   363  
   364  	_, err := bot.Send(msg)
   365  
   366  	if err != nil {
   367  		t.Error(err)
   368  		t.Fail()
   369  	}
   370  }
   371  
   372  func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
   373  	bot, _ := getBot(t)
   374  
   375  	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
   376  	msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{
   377  		RemoveKeyboard: true,
   378  		Selective:      false,
   379  	}
   380  	_, err := bot.Send(msg)
   381  
   382  	if err != nil {
   383  		t.Error(err)
   384  		t.Fail()
   385  	}
   386  }
   387  
   388  func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
   389  	bot, _ := getBot(t)
   390  
   391  	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
   392  	msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{
   393  		RemoveKeyboard: true,
   394  		Selective:      false,
   395  	}
   396  
   397  	_, err := bot.Send(msg)
   398  
   399  	if err != nil {
   400  		t.Error(err)
   401  		t.Fail()
   402  	}
   403  }
   404  
   405  func TestGetFile(t *testing.T) {
   406  	bot, _ := getBot(t)
   407  
   408  	file := tgbotapi.FileConfig{FileID: ExistingPhotoFileID}
   409  
   410  	_, err := bot.GetFile(file)
   411  
   412  	if err != nil {
   413  		t.Error(err)
   414  		t.Fail()
   415  	}
   416  }
   417  
   418  func TestSendChatConfig(t *testing.T) {
   419  	bot, _ := getBot(t)
   420  
   421  	_, err := bot.Send(tgbotapi.NewChatAction(ChatID, tgbotapi.ChatTyping))
   422  
   423  	if err != nil {
   424  		t.Error(err)
   425  		t.Fail()
   426  	}
   427  }
   428  
   429  func TestSendEditMessage(t *testing.T) {
   430  	bot, _ := getBot(t)
   431  
   432  	msg, err := bot.Send(tgbotapi.NewMessage(ChatID, "Testing editing."))
   433  	if err != nil {
   434  		t.Error(err)
   435  		t.Fail()
   436  	}
   437  
   438  	edit := tgbotapi.EditMessageTextConfig{
   439  		BaseEdit: tgbotapi.BaseEdit{
   440  			ChatID:    ChatID,
   441  			MessageID: msg.MessageID,
   442  		},
   443  		Text: "Updated text.",
   444  	}
   445  
   446  	_, err = bot.Send(edit)
   447  	if err != nil {
   448  		t.Error(err)
   449  		t.Fail()
   450  	}
   451  }
   452  
   453  func TestGetUserProfilePhotos(t *testing.T) {
   454  	bot, _ := getBot(t)
   455  
   456  	_, err := bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(ChatID))
   457  	if err != nil {
   458  		t.Error(err)
   459  		t.Fail()
   460  	}
   461  }
   462  
   463  func TestSetWebhookWithCert(t *testing.T) {
   464  	bot, _ := getBot(t)
   465  
   466  	time.Sleep(time.Second * 2)
   467  
   468  	bot.RemoveWebhook()
   469  
   470  	wh := tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
   471  	_, err := bot.SetWebhook(wh)
   472  	if err != nil {
   473  		t.Error(err)
   474  		t.Fail()
   475  	}
   476  	info, err := bot.GetWebhookInfo()
   477  	if err != nil {
   478  		t.Error(err)
   479  	}
   480  	if info.LastErrorDate != 0 {
   481  		t.Errorf("[Telegram callback failed]%s", info.LastErrorMessage)
   482  	}
   483  	bot.RemoveWebhook()
   484  }
   485  
   486  func TestSetWebhookWithoutCert(t *testing.T) {
   487  	bot, _ := getBot(t)
   488  
   489  	time.Sleep(time.Second * 2)
   490  
   491  	bot.RemoveWebhook()
   492  
   493  	wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
   494  	_, err := bot.SetWebhook(wh)
   495  	if err != nil {
   496  		t.Error(err)
   497  		t.Fail()
   498  	}
   499  	info, err := bot.GetWebhookInfo()
   500  	if err != nil {
   501  		t.Error(err)
   502  	}
   503  	if info.LastErrorDate != 0 {
   504  		t.Errorf("[Telegram callback failed]%s", info.LastErrorMessage)
   505  	}
   506  	bot.RemoveWebhook()
   507  }
   508  
   509  func TestUpdatesChan(t *testing.T) {
   510  	bot, _ := getBot(t)
   511  
   512  	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
   513  	ucfg.Timeout = 60
   514  	_, err := bot.GetUpdatesChan(ucfg)
   515  
   516  	if err != nil {
   517  		t.Error(err)
   518  		t.Fail()
   519  	}
   520  }
   521  
   522  func ExampleNewBotAPI() {
   523  	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
   524  	if err != nil {
   525  		log.Panic(err)
   526  	}
   527  
   528  	bot.Debug = true
   529  
   530  	log.Printf("Authorized on account %s", bot.Self.UserName)
   531  
   532  	u := tgbotapi.NewUpdate(0)
   533  	u.Timeout = 60
   534  
   535  	updates, err := bot.GetUpdatesChan(u)
   536  
   537  	// Optional: wait for updates and clear them if you don't want to handle
   538  	// a large backlog of old messages
   539  	time.Sleep(time.Millisecond * 500)
   540  	updates.Clear()
   541  
   542  	for update := range updates {
   543  		if update.Message == nil {
   544  			continue
   545  		}
   546  
   547  		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
   548  
   549  		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
   550  		msg.ReplyToMessageID = update.Message.MessageID
   551  
   552  		bot.Send(msg)
   553  	}
   554  }
   555  
   556  func ExampleNewWebhook() {
   557  	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
   558  	if err != nil {
   559  		log.Fatal(err)
   560  	}
   561  
   562  	bot.Debug = true
   563  
   564  	log.Printf("Authorized on account %s", bot.Self.UserName)
   565  
   566  	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
   567  	if err != nil {
   568  		log.Fatal(err)
   569  	}
   570  	info, err := bot.GetWebhookInfo()
   571  	if err != nil {
   572  		log.Fatal(err)
   573  	}
   574  	if info.LastErrorDate != 0 {
   575  		log.Printf("[Telegram callback failed]%s", info.LastErrorMessage)
   576  	}
   577  	updates := bot.ListenForWebhook("/" + bot.Token)
   578  	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
   579  
   580  	for update := range updates {
   581  		log.Printf("%+v\n", update)
   582  	}
   583  }
   584  
   585  func ExampleAnswerInlineQuery() {
   586  	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") // create new bot
   587  	if err != nil {
   588  		log.Panic(err)
   589  	}
   590  
   591  	log.Printf("Authorized on account %s", bot.Self.UserName)
   592  
   593  	u := tgbotapi.NewUpdate(0)
   594  	u.Timeout = 60
   595  
   596  	updates, err := bot.GetUpdatesChan(u)
   597  
   598  	for update := range updates {
   599  		if update.InlineQuery == nil { // if no inline query, ignore it
   600  			continue
   601  		}
   602  
   603  		article := tgbotapi.NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
   604  		article.Description = update.InlineQuery.Query
   605  
   606  		inlineConf := tgbotapi.InlineConfig{
   607  			InlineQueryID: update.InlineQuery.ID,
   608  			IsPersonal:    true,
   609  			CacheTime:     0,
   610  			Results:       []interface{}{article},
   611  		}
   612  
   613  		if _, err := bot.AnswerInlineQuery(inlineConf); err != nil {
   614  			log.Println(err)
   615  		}
   616  	}
   617  }
   618  
   619  func TestDeleteMessage(t *testing.T) {
   620  	bot, _ := getBot(t)
   621  
   622  	msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
   623  	msg.ParseMode = "markdown"
   624  	message, _ := bot.Send(msg)
   625  
   626  	deleteMessageConfig := tgbotapi.DeleteMessageConfig{
   627  		ChatID:    message.Chat.ID,
   628  		MessageID: message.MessageID,
   629  	}
   630  	_, err := bot.DeleteMessage(deleteMessageConfig)
   631  
   632  	if err != nil {
   633  		t.Error(err)
   634  		t.Fail()
   635  	}
   636  }
   637  
   638  func TestPinChatMessage(t *testing.T) {
   639  	bot, _ := getBot(t)
   640  
   641  	msg := tgbotapi.NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
   642  	msg.ParseMode = "markdown"
   643  	message, _ := bot.Send(msg)
   644  
   645  	pinChatMessageConfig := tgbotapi.PinChatMessageConfig{
   646  		ChatID:              message.Chat.ID,
   647  		MessageID:           message.MessageID,
   648  		DisableNotification: false,
   649  	}
   650  	_, err := bot.PinChatMessage(pinChatMessageConfig)
   651  
   652  	if err != nil {
   653  		t.Error(err)
   654  		t.Fail()
   655  	}
   656  }
   657  
   658  func TestUnpinChatMessage(t *testing.T) {
   659  	bot, _ := getBot(t)
   660  
   661  	msg := tgbotapi.NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
   662  	msg.ParseMode = "markdown"
   663  	message, _ := bot.Send(msg)
   664  
   665  	// We need pin message to unpin something
   666  	pinChatMessageConfig := tgbotapi.PinChatMessageConfig{
   667  		ChatID:              message.Chat.ID,
   668  		MessageID:           message.MessageID,
   669  		DisableNotification: false,
   670  	}
   671  	_, err := bot.PinChatMessage(pinChatMessageConfig)
   672  
   673  	unpinChatMessageConfig := tgbotapi.UnpinChatMessageConfig{
   674  		ChatID: message.Chat.ID,
   675  	}
   676  	_, err = bot.UnpinChatMessage(unpinChatMessageConfig)
   677  
   678  	if err != nil {
   679  		t.Error(err)
   680  		t.Fail()
   681  	}
   682  }