telegram-bot-api/bot_test.go

1049 lines
20 KiB
Go
Raw Normal View History

2018-10-09 06:34:34 +02:00
package tgbotapi
import (
2015-11-21 12:44:26 +01:00
"io/ioutil"
2015-11-21 12:22:08 +01:00
"net/http"
"os"
"testing"
"time"
)
2015-11-21 13:34:30 +01:00
const (
2017-05-22 00:04:12 +02:00
TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
ChatID = 76918703
2020-07-21 09:28:39 +02:00
Channel = "@tgbotapitest"
SupergroupChatID = -1001120141283
2017-05-22 00:04:12 +02:00
ReplyToMessageID = 35
ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"
ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
ExistingAudioFileID = "BQADAgADRgADjMcoCdXg3lSIN49lAg"
ExistingVoiceFileID = "AwADAgADWQADjMcoCeul6r_q52IyAg"
ExistingVideoFileID = "BAADAgADZgADjMcoCav432kYe0FRAg"
ExistingVideoNoteFileID = "DQADAgADdQAD70cQSUK41dLsRMqfAg"
ExistingStickerFileID = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
2015-11-21 13:34:30 +01:00
)
2020-11-06 05:29:48 +01:00
type testLogger struct {
t *testing.T
}
func (t testLogger) Println(v ...interface{}) {
t.t.Log(v...)
}
func (t testLogger) Printf(format string, v ...interface{}) {
t.t.Logf(format, v...)
}
2018-10-09 06:34:34 +02:00
func getBot(t *testing.T) (*BotAPI, error) {
bot, err := NewBotAPI(TestToken)
2017-05-22 00:04:12 +02:00
bot.Debug = true
2020-11-06 05:29:48 +01:00
logger := testLogger{t}
SetLogger(logger)
2015-11-21 13:34:30 +01:00
if err != nil {
t.Error(err)
}
2015-11-21 13:34:30 +01:00
return bot, err
}
func TestNewBotAPI_notoken(t *testing.T) {
2018-10-09 06:34:34 +02:00
_, err := NewBotAPI("")
2015-11-20 11:42:26 +01:00
if err == nil {
t.Error(err)
}
}
func TestGetUpdates(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
u := NewUpdate(0)
2015-11-21 13:34:30 +01:00
_, err := bot.GetUpdates(u)
if err != nil {
t.Error(err)
}
}
2015-11-20 16:30:50 +01:00
func TestSendWithMessage(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
2020-11-06 18:36:00 +01:00
msg.ParseMode = ModeMarkdown
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
func TestSendWithMessageReply(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
2015-11-21 13:34:30 +01:00
msg.ReplyToMessageID = ReplyToMessageID
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
func TestSendWithMessageForward(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
msg := NewForward(ChatID, ChatID, ReplyToMessageID)
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 16:30:50 +01:00
if err != nil {
t.Error(err)
2015-11-20 16:30:50 +01:00
}
}
2020-11-06 18:36:00 +01:00
func TestCopyMessage(t *testing.T) {
bot, _ := getBot(t)
msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
message, err := bot.Send(msg)
if err != nil {
t.Error(err)
}
copyMessageConfig := NewCopyMessage(SupergroupChatID, message.Chat.ID, message.MessageID)
messageID, err := bot.CopyMessage(copyMessageConfig)
if err != nil {
t.Error(err)
}
if messageID.MessageID == message.MessageID {
t.Error("copied message ID was the same as original message")
}
}
2015-11-20 17:43:56 +01:00
func TestSendWithNewPhoto(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 16:30:50 +01:00
2021-08-20 21:31:52 +02:00
msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
2015-11-20 18:26:12 +01:00
msg.Caption = "Test"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 16:30:50 +01:00
if err != nil {
t.Error(err)
2015-11-20 16:30:50 +01:00
}
}
2015-11-21 12:36:43 +01:00
func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:36:43 +01:00
data, _ := ioutil.ReadFile("tests/image.jpg")
2018-10-09 06:34:34 +02:00
b := FileBytes{Name: "image.jpg", Bytes: data}
2015-11-21 12:36:43 +01:00
msg := NewPhoto(ChatID, b)
2015-11-21 12:36:43 +01:00
msg.Caption = "Test"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 12:36:43 +01:00
if err != nil {
t.Error(err)
2015-11-21 12:36:43 +01:00
}
}
func TestSendWithNewPhotoWithFileReader(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:36:43 +01:00
f, _ := os.Open("tests/image.jpg")
2020-07-26 23:06:22 +02:00
reader := FileReader{Name: "image.jpg", Reader: f}
2015-11-21 12:36:43 +01:00
msg := NewPhoto(ChatID, reader)
2015-11-21 12:36:43 +01:00
msg.Caption = "Test"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 12:36:43 +01:00
if err != nil {
t.Error(err)
2015-11-21 12:36:43 +01:00
}
}
2015-11-20 18:26:12 +01:00
func TestSendWithNewPhotoReply(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2021-08-20 21:31:52 +02:00
msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
2015-11-21 13:34:30 +01:00
msg.ReplyToMessageID = ReplyToMessageID
2015-11-20 17:43:56 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
2015-11-20 17:43:56 +01:00
func TestSendNewPhotoToChannel(t *testing.T) {
2020-07-21 09:28:39 +02:00
bot, _ := getBot(t)
2021-08-20 21:31:52 +02:00
msg := NewPhotoToChannel(Channel, FilePath("tests/image.jpg"))
msg.Caption = "Test"
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
t.Fail()
}
}
func TestSendNewPhotoToChannelFileBytes(t *testing.T) {
2020-07-21 09:28:39 +02:00
bot, _ := getBot(t)
data, _ := ioutil.ReadFile("tests/image.jpg")
b := FileBytes{Name: "image.jpg", Bytes: data}
msg := NewPhotoToChannel(Channel, b)
msg.Caption = "Test"
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
t.Fail()
}
}
func TestSendNewPhotoToChannelFileReader(t *testing.T) {
2020-07-21 09:28:39 +02:00
bot, _ := getBot(t)
f, _ := os.Open("tests/image.jpg")
2020-07-26 23:06:22 +02:00
reader := FileReader{Name: "image.jpg", Reader: f}
msg := NewPhotoToChannel(Channel, reader)
msg.Caption = "Test"
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
t.Fail()
}
}
2015-11-20 17:43:56 +01:00
func TestSendWithExistingPhoto(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
msg := NewPhoto(ChatID, FileID(ExistingPhotoFileID))
2015-11-20 18:26:12 +01:00
msg.Caption = "Test"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
func TestSendWithNewDocument(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2021-08-20 21:31:52 +02:00
msg := NewDocument(ChatID, FilePath("tests/image.jpg"))
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
}
}
func TestSendWithNewDocumentAndThumb(t *testing.T) {
bot, _ := getBot(t)
2021-08-20 21:31:52 +02:00
msg := NewDocument(ChatID, FilePath("tests/voice.ogg"))
msg.Thumb = FilePath("tests/image.jpg")
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
func TestSendWithExistingDocument(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
msg := NewDocument(ChatID, FileID(ExistingDocumentFileID))
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
2015-11-20 18:26:12 +01:00
func TestSendWithNewAudio(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2021-08-20 21:31:52 +02:00
msg := NewAudio(ChatID, FilePath("tests/audio.mp3"))
2015-11-20 18:26:12 +01:00
msg.Title = "TEST"
msg.Duration = 10
msg.Performer = "TEST"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
func TestSendWithExistingAudio(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
msg := NewAudio(ChatID, FileID(ExistingAudioFileID))
2015-11-20 18:26:12 +01:00
msg.Title = "TEST"
msg.Duration = 10
msg.Performer = "TEST"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
func TestSendWithNewVoice(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2021-08-20 21:31:52 +02:00
msg := NewVoice(ChatID, FilePath("tests/voice.ogg"))
2015-11-20 18:26:12 +01:00
msg.Duration = 10
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
func TestSendWithExistingVoice(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
msg := NewVoice(ChatID, FileID(ExistingVoiceFileID))
2015-11-20 18:26:12 +01:00
msg.Duration = 10
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
2016-04-14 00:06:18 +02:00
func TestSendWithContact(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
contact := NewContact(ChatID, "5551234567", "Test")
2016-04-14 00:06:18 +02:00
if _, err := bot.Send(contact); err != nil {
t.Error(err)
2016-04-14 00:06:18 +02:00
}
}
2015-11-20 18:26:12 +01:00
func TestSendWithLocation(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2018-10-09 06:34:34 +02:00
_, err := bot.Send(NewLocation(ChatID, 40, 40))
2015-11-20 18:26:12 +01:00
if err != nil {
t.Error(err)
2015-11-20 18:26:12 +01:00
}
}
2016-04-14 00:06:18 +02:00
func TestSendWithVenue(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
venue := NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
2016-04-14 00:06:18 +02:00
if _, err := bot.Send(venue); err != nil {
t.Error(err)
2016-04-14 00:06:18 +02:00
}
}
2015-11-21 11:17:34 +01:00
func TestSendWithNewVideo(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2021-08-20 21:31:52 +02:00
msg := NewVideo(ChatID, FilePath("tests/video.mp4"))
2015-11-21 11:17:34 +01:00
msg.Duration = 10
msg.Caption = "TEST"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
func TestSendWithExistingVideo(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
msg := NewVideo(ChatID, FileID(ExistingVideoFileID))
2015-11-21 11:17:34 +01:00
msg.Duration = 10
msg.Caption = "TEST"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
2017-05-22 00:04:12 +02:00
func TestSendWithNewVideoNote(t *testing.T) {
bot, _ := getBot(t)
2021-08-20 21:31:52 +02:00
msg := NewVideoNote(ChatID, 240, FilePath("tests/videonote.mp4"))
2017-05-22 00:04:12 +02:00
msg.Duration = 10
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
}
}
func TestSendWithExistingVideoNote(t *testing.T) {
bot, _ := getBot(t)
msg := NewVideoNote(ChatID, 240, FileID(ExistingVideoNoteFileID))
2017-05-22 00:04:12 +02:00
msg.Duration = 10
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
}
}
2015-11-21 11:17:34 +01:00
func TestSendWithNewSticker(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2021-08-20 21:31:52 +02:00
msg := NewSticker(ChatID, FilePath("tests/image.jpg"))
2015-11-21 11:17:34 +01:00
2015-11-21 13:36:55 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
func TestSendWithExistingSticker(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
msg := NewSticker(ChatID, FileID(ExistingStickerFileID))
2015-11-21 11:17:34 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2021-08-20 21:31:52 +02:00
msg := NewSticker(ChatID, FilePath("tests/image.jpg"))
2018-10-09 06:34:34 +02:00
msg.ReplyMarkup = ReplyKeyboardRemove{
2017-12-29 06:17:32 +01:00
RemoveKeyboard: true,
Selective: false,
}
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
msg := NewSticker(ChatID, FileID(ExistingStickerFileID))
2018-10-09 06:34:34 +02:00
msg.ReplyMarkup = ReplyKeyboardRemove{
2017-12-29 06:17:32 +01:00
RemoveKeyboard: true,
Selective: false,
}
2015-11-21 11:17:34 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Error(err)
2015-11-21 11:17:34 +01:00
}
}
2020-11-06 05:29:48 +01:00
func TestSendWithDice(t *testing.T) {
bot, _ := getBot(t)
msg := NewDice(ChatID)
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
t.Fail()
}
}
func TestSendWithDiceWithEmoji(t *testing.T) {
bot, _ := getBot(t)
msg := NewDiceWithEmoji(ChatID, "🏀")
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
t.Fail()
}
}
2015-11-20 17:43:56 +01:00
func TestGetFile(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2018-10-09 06:34:34 +02:00
file := FileConfig{
2017-12-29 06:17:32 +01:00
FileID: ExistingPhotoFileID,
}
2015-11-20 17:43:56 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.GetFile(file)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
func TestSendChatConfig(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2018-10-09 06:34:34 +02:00
_, err := bot.Request(NewChatAction(ChatID, ChatTyping))
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
2016-04-12 19:53:19 +02:00
func TestSendEditMessage(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg, err := bot.Send(NewMessage(ChatID, "Testing editing."))
2016-04-12 19:53:19 +02:00
if err != nil {
t.Error(err)
2016-04-12 19:53:19 +02:00
}
2018-10-09 06:34:34 +02:00
edit := EditMessageTextConfig{
BaseEdit: BaseEdit{
2016-04-12 19:53:19 +02:00
ChatID: ChatID,
MessageID: msg.MessageID,
},
Text: "Updated text.",
}
_, err = bot.Send(edit)
if err != nil {
t.Error(err)
2016-04-12 19:53:19 +02:00
}
}
2015-11-20 17:43:56 +01:00
func TestGetUserProfilePhotos(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2018-10-09 06:34:34 +02:00
_, err := bot.GetUserProfilePhotos(NewUserProfilePhotos(ChatID))
2015-11-20 17:43:56 +01:00
if err != nil {
t.Error(err)
2015-11-20 17:43:56 +01:00
}
}
2015-11-21 12:36:43 +01:00
func TestSetWebhookWithCert(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:22:08 +01:00
time.Sleep(time.Second * 2)
2020-11-05 22:53:37 +01:00
bot.Request(DeleteWebhookConfig{})
2015-11-21 12:22:08 +01:00
2021-08-20 21:31:52 +02:00
wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, FilePath("tests/cert.pem"))
if err != nil {
t.Error(err)
}
_, err = bot.Request(wh)
2015-11-21 12:22:08 +01:00
if err != nil {
t.Error(err)
2015-11-21 12:22:08 +01:00
}
2018-10-08 09:25:09 +02:00
_, err = bot.GetWebhookInfo()
2018-03-26 19:22:16 +02:00
2018-01-07 19:10:50 +01:00
if err != nil {
t.Error(err)
}
2018-03-26 19:22:16 +02:00
2020-11-05 22:53:37 +01:00
bot.Request(DeleteWebhookConfig{})
2015-11-21 12:22:08 +01:00
}
2015-11-21 12:36:43 +01:00
func TestSetWebhookWithoutCert(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:36:43 +01:00
time.Sleep(time.Second * 2)
2020-11-05 22:53:37 +01:00
bot.Request(DeleteWebhookConfig{})
2015-11-21 12:36:43 +01:00
wh, err := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
if err != nil {
t.Error(err)
}
_, err = bot.Request(wh)
2015-11-21 12:36:43 +01:00
if err != nil {
t.Error(err)
2015-11-21 12:36:43 +01:00
}
2018-01-07 19:10:50 +01:00
info, err := bot.GetWebhookInfo()
2018-03-26 19:22:16 +02:00
2018-01-07 19:10:50 +01:00
if err != nil {
t.Error(err)
}
if info.MaxConnections == 0 {
t.Errorf("Expected maximum connections to be greater than 0")
}
2018-01-07 19:10:50 +01:00
if info.LastErrorDate != 0 {
2018-03-26 19:22:16 +02:00
t.Errorf("failed to set webhook: %s", info.LastErrorMessage)
2018-01-07 19:10:50 +01:00
}
2018-03-26 19:22:16 +02:00
2020-11-05 22:53:37 +01:00
bot.Request(DeleteWebhookConfig{})
2015-11-21 12:36:43 +01:00
}
func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
cfg := NewMediaGroup(ChatID, []interface{}{
NewInputMediaPhoto(FileURL("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/image.jpg")),
2021-08-20 21:31:52 +02:00
NewInputMediaPhoto(FilePath("tests/image.jpg")),
NewInputMediaVideo(FilePath("tests/video.mp4")),
})
2018-12-25 22:44:01 +01:00
messages, err := bot.SendMediaGroup(cfg)
if err != nil {
t.Error(err)
}
2018-12-25 22:44:01 +01:00
if messages == nil {
t.Error("No received messages")
2018-12-25 22:44:01 +01:00
}
if len(messages) != len(cfg.Media) {
t.Errorf("Different number of messages: %d", len(messages))
2018-12-25 22:44:01 +01:00
}
}
func TestSendWithMediaGroupDocument(t *testing.T) {
bot, _ := getBot(t)
cfg := NewMediaGroup(ChatID, []interface{}{
NewInputMediaDocument(FileURL("https://i.imgur.com/unQLJIb.jpg")),
2021-08-20 21:31:52 +02:00
NewInputMediaDocument(FilePath("tests/image.jpg")),
})
messages, err := bot.SendMediaGroup(cfg)
if err != nil {
t.Error(err)
}
if messages == nil {
t.Error("No received messages")
}
if len(messages) != len(cfg.Media) {
t.Errorf("Different number of messages: %d", len(messages))
}
}
func TestSendWithMediaGroupAudio(t *testing.T) {
bot, _ := getBot(t)
cfg := NewMediaGroup(ChatID, []interface{}{
2021-08-20 21:31:52 +02:00
NewInputMediaAudio(FilePath("tests/audio.mp3")),
NewInputMediaAudio(FilePath("tests/audio.mp3")),
})
messages, err := bot.SendMediaGroup(cfg)
if err != nil {
t.Error(err)
}
if messages == nil {
t.Error("No received messages")
}
if len(messages) != len(cfg.Media) {
t.Errorf("Different number of messages: %d", len(messages))
}
}
func ExampleNewBotAPI() {
2018-10-09 06:34:34 +02:00
bot, err := NewBotAPI("MyAwesomeBotToken")
if err != nil {
2018-10-09 06:34:34 +02:00
panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
2018-10-09 06:34:34 +02:00
u := NewUpdate(0)
u.Timeout = 60
2018-10-09 08:45:18 +02:00
updates := bot.GetUpdatesChan(u)
// Optional: wait for updates and clear them if you don't want to handle
// a large backlog of old messages
time.Sleep(time.Millisecond * 500)
updates.Clear()
2015-11-21 15:26:28 +01:00
for update := range updates {
2016-05-22 17:16:28 +02:00
if update.Message == nil {
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
2018-10-09 06:34:34 +02:00
msg := NewMessage(update.Message.Chat.ID, update.Message.Text)
msg.ReplyToMessageID = update.Message.MessageID
2015-11-20 16:30:50 +01:00
bot.Send(msg)
}
}
2015-11-21 12:53:00 +01:00
func ExampleNewWebhook() {
2018-10-09 06:34:34 +02:00
bot, err := NewBotAPI("MyAwesomeBotToken")
2015-11-21 12:53:00 +01:00
if err != nil {
2018-10-09 06:34:34 +02:00
panic(err)
2015-11-21 12:53:00 +01:00
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
2021-08-20 21:31:52 +02:00
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))
if err != nil {
panic(err)
}
_, err = bot.Request(wh)
2015-11-21 12:53:00 +01:00
if err != nil {
2018-10-09 06:34:34 +02:00
panic(err)
2015-11-21 12:53:00 +01:00
}
2018-01-07 19:10:50 +01:00
info, err := bot.GetWebhookInfo()
2018-03-26 19:22:16 +02:00
2018-01-07 19:10:50 +01:00
if err != nil {
2018-10-09 06:34:34 +02:00
panic(err)
2018-01-07 19:10:50 +01:00
}
2018-03-26 19:22:16 +02:00
2018-01-07 19:10:50 +01:00
if info.LastErrorDate != 0 {
2018-03-26 19:22:16 +02:00
log.Printf("failed to set webhook: %s", info.LastErrorMessage)
2018-01-07 19:10:50 +01:00
}
2018-03-26 19:22:16 +02:00
updates := bot.ListenForWebhook("/" + bot.Token)
2015-11-21 12:53:00 +01:00
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
2015-11-21 15:26:28 +01:00
for update := range updates {
2015-11-21 12:53:00 +01:00
log.Printf("%+v\n", update)
}
}
func ExampleWebhookHandler() {
bot, err := NewBotAPI("MyAwesomeBotToken")
if err != nil {
panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
2021-08-20 21:31:52 +02:00
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))
if err != nil {
panic(err)
}
_, err = bot.Request(wh)
if err != nil {
panic(err)
}
info, err := bot.GetWebhookInfo()
if err != nil {
panic(err)
}
if info.LastErrorDate != 0 {
log.Printf("[Telegram callback failed]%s", info.LastErrorMessage)
}
http.HandleFunc("/"+bot.Token, func(w http.ResponseWriter, r *http.Request) {
2020-11-06 05:29:48 +01:00
update, err := bot.HandleUpdate(r)
if err != nil {
log.Printf("%+v\n", err.Error())
} else {
log.Printf("%+v\n", *update)
}
})
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
}
2018-03-26 19:22:16 +02:00
func ExampleInlineConfig() {
2018-10-09 06:34:34 +02:00
bot, err := NewBotAPI("MyAwesomeBotToken") // create new bot
if err != nil {
2018-10-09 06:34:34 +02:00
panic(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
2018-10-09 06:34:34 +02:00
u := NewUpdate(0)
u.Timeout = 60
2018-10-09 08:45:18 +02:00
updates := bot.GetUpdatesChan(u)
for update := range updates {
2016-05-22 17:16:28 +02:00
if update.InlineQuery == nil { // if no inline query, ignore it
continue
}
2018-10-09 06:34:34 +02:00
article := NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
article.Description = update.InlineQuery.Query
2018-10-09 06:34:34 +02:00
inlineConf := InlineConfig{
InlineQueryID: update.InlineQuery.ID,
IsPersonal: true,
CacheTime: 0,
Results: []interface{}{article},
}
if _, err := bot.Request(inlineConf); err != nil {
log.Println(err)
}
}
}
func TestDeleteMessage(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
2020-11-06 18:36:00 +01:00
msg.ParseMode = ModeMarkdown
message, _ := bot.Send(msg)
2018-10-09 06:34:34 +02:00
deleteMessageConfig := DeleteMessageConfig{
ChatID: message.Chat.ID,
MessageID: message.MessageID,
}
2017-12-29 07:50:45 +01:00
_, err := bot.Request(deleteMessageConfig)
if err != nil {
t.Error(err)
}
}
func TestPinChatMessage(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
2020-11-06 18:36:00 +01:00
msg.ParseMode = ModeMarkdown
message, _ := bot.Send(msg)
2018-10-09 06:34:34 +02:00
pinChatMessageConfig := PinChatMessageConfig{
2017-11-09 19:51:50 +01:00
ChatID: message.Chat.ID,
MessageID: message.MessageID,
DisableNotification: false,
}
2017-12-29 07:50:45 +01:00
_, err := bot.Request(pinChatMessageConfig)
if err != nil {
t.Error(err)
}
}
func TestUnpinChatMessage(t *testing.T) {
bot, _ := getBot(t)
2018-10-09 06:34:34 +02:00
msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
2020-11-06 18:36:00 +01:00
msg.ParseMode = ModeMarkdown
message, _ := bot.Send(msg)
// We need pin message to unpin something
2018-10-09 06:34:34 +02:00
pinChatMessageConfig := PinChatMessageConfig{
2017-11-09 19:51:50 +01:00
ChatID: message.Chat.ID,
MessageID: message.MessageID,
DisableNotification: false,
}
if _, err := bot.Request(pinChatMessageConfig); err != nil {
t.Error(err)
}
2018-10-09 06:34:34 +02:00
unpinChatMessageConfig := UnpinChatMessageConfig{
2020-11-06 18:36:00 +01:00
ChatID: message.Chat.ID,
MessageID: message.MessageID,
}
if _, err := bot.Request(unpinChatMessageConfig); err != nil {
t.Error(err)
}
2017-11-09 19:51:50 +01:00
}
2020-11-06 18:36:00 +01:00
func TestUnpinAllChatMessages(t *testing.T) {
bot, _ := getBot(t)
msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
msg.ParseMode = ModeMarkdown
message, _ := bot.Send(msg)
pinChatMessageConfig := PinChatMessageConfig{
ChatID: message.Chat.ID,
MessageID: message.MessageID,
DisableNotification: true,
}
if _, err := bot.Request(pinChatMessageConfig); err != nil {
t.Error(err)
}
unpinAllChatMessagesConfig := UnpinAllChatMessagesConfig{
ChatID: message.Chat.ID,
}
if _, err := bot.Request(unpinAllChatMessagesConfig); err != nil {
t.Error(err)
}
}
func TestPolls(t *testing.T) {
bot, _ := getBot(t)
poll := NewPoll(SupergroupChatID, "Are polls working?", "Yes", "No")
msg, err := bot.Send(poll)
if err != nil {
t.Error(err)
}
result, err := bot.StopPoll(NewStopPoll(SupergroupChatID, msg.MessageID))
if err != nil {
t.Error(err)
}
if result.Question != "Are polls working?" {
t.Error("Poll question did not match")
}
if !result.IsClosed {
t.Error("Poll did not end")
}
if result.Options[0].Text != "Yes" || result.Options[0].VoterCount != 0 || result.Options[1].Text != "No" || result.Options[1].VoterCount != 0 {
t.Error("Poll options were incorrect")
}
}
2020-03-30 22:35:53 +02:00
func TestSendDice(t *testing.T) {
bot, _ := getBot(t)
dice := NewSendDice(ChatID)
msg, err := bot.Send(dice)
if err != nil {
t.Error("Unable to send dice roll")
}
if msg.Dice == nil {
t.Error("Dice roll was not received")
}
}
2021-06-27 21:15:09 +02:00
func TestCommands(t *testing.T) {
2020-03-30 22:35:53 +02:00
bot, _ := getBot(t)
setCommands := NewSetMyCommands(BotCommand{
Command: "test",
Description: "a test command",
})
if _, err := bot.Request(setCommands); err != nil {
t.Error("Unable to set commands")
}
commands, err := bot.GetMyCommands()
2020-03-30 22:35:53 +02:00
if err != nil {
t.Error("Unable to get commands")
}
if len(commands) != 1 {
t.Error("Incorrect number of commands returned")
}
if commands[0].Command != "test" || commands[0].Description != "a test command" {
t.Error("Commands were incorrectly set")
}
2021-06-27 21:15:09 +02:00
setCommands = NewSetMyCommandsWithScope(NewBotCommandScopeAllPrivateChats(), BotCommand{
Command: "private",
Description: "a private command",
})
if _, err := bot.Request(setCommands); err != nil {
t.Error("Unable to set commands")
}
commands, err = bot.GetMyCommandsWithConfig(NewGetMyCommandsWithScope(NewBotCommandScopeAllPrivateChats()))
2021-06-27 21:15:09 +02:00
if err != nil {
t.Error("Unable to get commands")
}
if len(commands) != 1 {
t.Error("Incorrect number of commands returned")
}
if commands[0].Command != "private" || commands[0].Description != "a private command" {
t.Error("Commands were incorrectly set")
}
2020-03-30 22:35:53 +02:00
}
func TestEditMessageMedia(t *testing.T) {
bot, _ := getBot(t)
2021-08-20 21:31:52 +02:00
msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
msg.Caption = "Test"
m, err := bot.Send(msg)
if err != nil {
t.Error(err)
}
edit := EditMessageMediaConfig{
BaseEdit: BaseEdit{
ChatID: ChatID,
MessageID: m.MessageID,
},
2021-08-20 21:31:52 +02:00
Media: NewInputMediaVideo(FilePath("tests/video.mp4")),
}
_, err = bot.Request(edit)
if err != nil {
t.Error(err)
}
}
func TestPrepareInputMediaForParams(t *testing.T) {
media := []interface{}{
2021-08-20 21:31:52 +02:00
NewInputMediaPhoto(FilePath("tests/image.jpg")),
NewInputMediaVideo(FileID("test")),
}
prepared := prepareInputMediaForParams(media)
2021-08-20 21:31:52 +02:00
if media[0].(InputMediaPhoto).Media != FilePath("tests/image.jpg") {
t.Error("Original media was changed")
}
2021-08-20 21:31:52 +02:00
if prepared[0].(InputMediaPhoto).Media != FileID("attach://file-0") {
t.Error("New media was not replaced")
}
if prepared[1].(InputMediaVideo).Media != FileID("test") {
t.Error("Passthrough value was not the same")
}
}