2018-10-09 06:34:34 +02:00
|
|
|
package tgbotapi
|
2015-07-29 11:41:41 +02:00
|
|
|
|
|
|
|
import (
|
2015-11-21 12:22:08 +01:00
|
|
|
"net/http"
|
2015-07-29 11:41:41 +02:00
|
|
|
"os"
|
|
|
|
"testing"
|
2016-11-25 06:50:35 +01:00
|
|
|
"time"
|
2015-07-29 11:41:41 +02:00
|
|
|
)
|
|
|
|
|
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"
|
2017-08-05 12:50:44 +02:00
|
|
|
SupergroupChatID = -1001120141283
|
2017-05-22 00:04:12 +02:00
|
|
|
ReplyToMessageID = 35
|
2021-08-20 21:41:04 +02:00
|
|
|
ExistingPhotoFileID = "AgACAgIAAxkDAAEBFUZhIALQ9pZN4BUe8ZSzUU_2foSo1AACnrMxG0BucEhezsBWOgcikQEAAwIAA20AAyAE"
|
2017-05-22 00:04:12 +02:00
|
|
|
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
|
2015-07-29 11:41:41 +02:00
|
|
|
|
2020-11-06 05:29:48 +01:00
|
|
|
logger := testLogger{t}
|
|
|
|
SetLogger(logger)
|
|
|
|
|
2015-11-21 13:34:30 +01:00
|
|
|
if err != nil {
|
2016-11-25 06:50:35 +01:00
|
|
|
t.Error(err)
|
2015-07-29 11:41:41 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 13:34:30 +01:00
|
|
|
return bot, err
|
2015-07-29 11:41:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewBotAPI_notoken(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
_, err := NewBotAPI("")
|
2015-07-29 11:41:41 +02:00
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
if err == nil {
|
2016-11-25 06:50:35 +01:00
|
|
|
t.Error(err)
|
2015-07-29 11:41:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUpdates(t *testing.T) {
|
2015-11-21 13:34:30 +01:00
|
|
|
bot, _ := getBot(t)
|
2015-07-29 11:41:41 +02:00
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
u := NewUpdate(0)
|
2015-07-29 11:41:41 +02:00
|
|
|
|
2015-11-21 13:34:30 +01:00
|
|
|
_, err := bot.GetUpdates(u)
|
2015-07-29 11:41:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2016-11-25 06:50:35 +01:00
|
|
|
t.Error(err)
|
2015-07-29 11:41:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 16:30:50 +01:00
|
|
|
func TestSendWithMessage(t *testing.T) {
|
2015-11-21 13:34:30 +01:00
|
|
|
bot, _ := getBot(t)
|
2015-07-29 11:41:41 +02:00
|
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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")
|
2024-01-05 21:05:00 +01:00
|
|
|
msg.ReplyParameters.MessageID = ReplyToMessageID
|
2015-11-21 13:34:30 +01:00
|
|
|
_, err := bot.Send(msg)
|
2015-11-20 18:26:12 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
t.Error(err)
|
2015-11-20 16:30:50 +01:00
|
|
|
}
|
2015-07-29 11:41:41 +02: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
|
|
|
|
2022-10-19 10:47:42 +02:00
|
|
|
data, _ := os.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
|
|
|
|
2020-07-26 02:29:40 +02: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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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
|
|
|
|
2020-07-26 02:29:40 +02: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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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"))
|
2024-01-05 21:05:00 +01:00
|
|
|
msg.ReplyParameters.MessageID = 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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
t.Error(err)
|
2015-11-20 18:26:12 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-20 17:43:56 +01:00
|
|
|
|
2019-09-01 21:00:09 +02:00
|
|
|
func TestSendNewPhotoToChannel(t *testing.T) {
|
2020-07-21 09:28:39 +02:00
|
|
|
bot, _ := getBot(t)
|
2019-09-01 21:00:09 +02:00
|
|
|
|
2021-08-20 21:31:52 +02:00
|
|
|
msg := NewPhotoToChannel(Channel, FilePath("tests/image.jpg"))
|
2019-09-01 21:00:09 +02:00
|
|
|
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)
|
2019-09-01 21:00:09 +02:00
|
|
|
|
2022-10-19 10:47:42 +02:00
|
|
|
data, _ := os.ReadFile("tests/image.jpg")
|
2019-09-01 21:00:09 +02:00
|
|
|
b := FileBytes{Name: "image.jpg", Bytes: data}
|
|
|
|
|
2020-07-26 02:29:40 +02:00
|
|
|
msg := NewPhotoToChannel(Channel, b)
|
2019-09-01 21:00:09 +02:00
|
|
|
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)
|
2019-09-01 21:00:09 +02:00
|
|
|
|
|
|
|
f, _ := os.Open("tests/image.jpg")
|
2020-07-26 23:06:22 +02:00
|
|
|
reader := FileReader{Name: "image.jpg", Reader: f}
|
2019-09-01 21:00:09 +02:00
|
|
|
|
2020-07-26 02:29:40 +02:00
|
|
|
msg := NewPhotoToChannel(Channel, reader)
|
2019-09-01 21:00:09 +02:00
|
|
|
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
|
|
|
|
2020-07-26 02:29:40 +02: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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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"))
|
2020-07-26 02:29:40 +02:00
|
|
|
_, 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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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
|
|
|
|
2020-07-26 02:29:40 +02: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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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
|
|
|
|
2020-07-26 02:29:40 +02: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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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
|
|
|
|
2020-07-26 02:29:40 +02: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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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
|
|
|
|
2020-07-26 02:29:40 +02: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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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)
|
|
|
|
|
2020-07-26 02:29:40 +02:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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
|
|
|
|
2020-07-26 02:29:40 +02: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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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
|
|
|
|
2020-07-26 02:29:40 +02: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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
t.Error(err)
|
2015-11-20 17:43:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 03:16:31 +01:00
|
|
|
// TODO: identify why this isn't working
|
|
|
|
// func TestSendEditMessage(t *testing.T) {
|
|
|
|
// bot, _ := getBot(t)
|
|
|
|
|
|
|
|
// msg, err := bot.Send(NewMessage(ChatID, "Testing editing."))
|
|
|
|
// if err != nil {
|
|
|
|
// t.Error(err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// edit := EditMessageTextConfig{
|
|
|
|
// BaseEdit: BaseEdit{
|
|
|
|
// 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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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
|
|
|
|
2016-11-25 06:50:35 +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"))
|
2020-09-30 22:19:29 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
_, err = bot.Request(wh)
|
|
|
|
|
2015-11-21 12:22:08 +01:00
|
|
|
if err != nil {
|
2016-11-25 06:50:35 +01:00
|
|
|
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
|
|
|
|
2016-11-25 06:50:35 +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
|
|
|
|
2020-09-30 22:19:29 +02: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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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)
|
|
|
|
}
|
2020-01-13 08:42:35 +01:00
|
|
|
if info.MaxConnections == 0 {
|
2020-07-21 10:37:08 +02:00
|
|
|
t.Errorf("Expected maximum connections to be greater than 0")
|
2020-01-13 08:42:35 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-02-20 19:49:00 +01:00
|
|
|
func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
|
2018-09-22 03:20:28 +02:00
|
|
|
bot, _ := getBot(t)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
cfg := NewMediaGroup(ChatID, []interface{}{
|
2021-03-10 18:33:02 +01:00
|
|
|
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-09-22 03:20:28 +02:00
|
|
|
})
|
2018-12-25 22:44:01 +01:00
|
|
|
|
|
|
|
messages, err := bot.SendMediaGroup(cfg)
|
2018-09-22 03:20:28 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-12-25 22:44:01 +01:00
|
|
|
|
|
|
|
if messages == nil {
|
2020-07-26 02:29:40 +02:00
|
|
|
t.Error("No received messages")
|
2018-12-25 22:44:01 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 02:29:40 +02:00
|
|
|
if len(messages) != len(cfg.Media) {
|
|
|
|
t.Errorf("Different number of messages: %d", len(messages))
|
2018-12-25 22:44:01 +01:00
|
|
|
}
|
2018-09-22 03:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 19:49:00 +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")),
|
2021-02-20 19:49:00 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
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")),
|
2021-02-20 19:49:00 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 11:41:41 +02:00
|
|
|
func ExampleNewBotAPI() {
|
2018-10-09 06:34:34 +02:00
|
|
|
bot, err := NewBotAPI("MyAwesomeBotToken")
|
2015-07-29 11:41:41 +02:00
|
|
|
if err != nil {
|
2018-10-09 06:34:34 +02:00
|
|