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
|
|
|
panic(err)
|
2015-07-29 11:41:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bot.Debug = true
|
|
|
|
|
|
|
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
u := NewUpdate(0)
|
2015-07-29 11:41:41 +02:00
|
|
|
u.Timeout = 60
|
|
|
|
|
2018-10-09 08:45:18 +02:00
|
|
|
updates := bot.GetUpdatesChan(u)
|
2015-07-29 11:41:41 +02:00
|
|
|
|
2017-01-26 18:43:28 +01:00
|
|
|
// Optional: wait for updates and clear them if you don't want to handle
|
|
|
|
// a large backlog of old messages
|
2016-12-16 02:07:54 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-07-29 11:41:41 +02:00
|
|
|
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)
|
2024-01-05 21:05:00 +01:00
|
|
|
msg.ReplyParameters.MessageID = update.Message.MessageID
|
2015-07-29 11:41:41 +02:00
|
|
|
|
2015-11-20 16:30:50 +01:00
|
|
|
bot.Send(msg)
|
2015-07-29 11:41:41 +02:00
|
|
|
}
|
|
|
|
}
|
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"))
|
2020-09-30 22:19:29 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-01-04 06:05:36 +01: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)
|
|
|
|
}
|
|
|
|
}
|
2016-02-28 17:14:08 +01:00
|
|
|
|
2019-09-18 02:34:37 +02:00
|
|
|
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"))
|
2020-09-30 22:19:29 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = bot.Request(wh)
|
2019-09-18 02:34:37 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-09-18 02:34:37 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
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
|
2016-02-28 17:14:08 +01:00
|
|
|
if err != nil {
|
2018-10-09 06:34:34 +02:00
|
|
|
panic(err)
|
2016-02-28 17:14:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
u := NewUpdate(0)
|
2016-02-28 17:14:08 +01:00
|
|
|
u.Timeout = 60
|
|
|
|
|
2018-10-09 08:45:18 +02:00
|
|
|
updates := bot.GetUpdatesChan(u)
|
2016-02-28 17:14:08 +01:00
|
|
|
|
|
|
|
for update := range updates {
|
2016-05-22 17:16:28 +02:00
|
|
|
if update.InlineQuery == nil { // if no inline query, ignore it
|
2016-02-28 17:14:08 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
article := NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
|
2016-06-02 22:41:55 +02:00
|
|
|
article.Description = update.InlineQuery.Query
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
inlineConf := InlineConfig{
|
2016-06-02 22:41:55 +02:00
|
|
|
InlineQueryID: update.InlineQuery.ID,
|
|
|
|
IsPersonal: true,
|
|
|
|
CacheTime: 0,
|
|
|
|
Results: []interface{}{article},
|
|
|
|
}
|
|
|
|
|
2017-12-29 08:08:01 +01:00
|
|
|
if _, err := bot.Request(inlineConf); err != nil {
|
2016-06-02 22:41:55 +02:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-08 11:32:05 +02:00
|
|
|
|
|
|
|
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
|
2017-06-08 11:32:05 +02:00
|
|
|
message, _ := bot.Send(msg)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
deleteMessageConfig := DeleteMessageConfig{
|
2024-01-05 21:05:00 +01:00
|
|
|
BaseChatMessage: BaseChatMessage{
|
|
|
|
ChatConfig: ChatConfig{
|
|
|
|
ChatID: message.Chat.ID,
|
|
|
|
},
|
|
|
|
MessageID: message.MessageID,
|
|
|
|
},
|
2017-06-08 11:32:05 +02:00
|
|
|
}
|
2017-12-29 07:50:45 +01:00
|
|
|
_, err := bot.Request(deleteMessageConfig)
|
2017-06-08 11:32:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2017-08-05 11:37:43 +02:00
|
|
|
|
|
|
|
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
|
2017-08-05 11:37:43 +02:00
|
|
|
message, _ := bot.Send(msg)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
pinChatMessageConfig := PinChatMessageConfig{
|
2024-01-05 21:05:00 +01:00
|
|
|
BaseChatMessage: BaseChatMessage{
|
|
|
|
ChatConfig: ChatConfig{
|
|
|
|
ChatID: ChatID,
|
|
|
|
},
|
|
|
|
MessageID: message.MessageID,
|
|
|
|
},
|
2017-08-05 11:37:43 +02:00
|
|
|
DisableNotification: false,
|
|
|
|
}
|
2017-12-29 07:50:45 +01:00
|
|
|
_, err := bot.Request(pinChatMessageConfig)
|
2017-08-05 11:37:43 +02:00
|
|
|
|
|
|
|
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
|
2017-08-05 11:37:43 +02:00
|
|
|
message, _ := bot.Send(msg)
|
|
|
|
|
|
|
|
// We need pin message to unpin something
|
2018-10-09 06:34:34 +02:00
|
|
|
pinChatMessageConfig := PinChatMessageConfig{
|
2024-01-05 21:05:00 +01:00
|
|
|
BaseChatMessage: BaseChatMessage{
|
|
|
|
ChatConfig: ChatConfig{
|
|
|
|
ChatID: message.Chat.ID,
|
|
|
|
},
|
|
|
|
MessageID: message.MessageID,
|
|
|
|
},
|
2017-08-05 11:37:43 +02:00
|
|
|
DisableNotification: false,
|
|
|
|
}
|
2018-03-26 19:39:07 +02:00
|
|
|
|
|
|
|
if _, err := bot.Request(pinChatMessageConfig); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-08-05 11:37:43 +02:00
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
unpinChatMessageConfig := UnpinChatMessageConfig{
|
2024-01-05 21:05:00 +01:00
|
|
|
BaseChatMessage: BaseChatMessage{
|
|
|
|
ChatConfig: ChatConfig{
|
|
|
|
ChatID: message.Chat.ID,
|
|
|
|
},
|
|
|
|
MessageID: message.MessageID,
|
|
|
|
},
|
2017-08-05 11:37:43 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 19:39:07 +02:00
|
|
|
if _, err := bot.Request(unpinChatMessageConfig); err != nil {
|
2017-08-05 11:37:43 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-11-09 19:51:50 +01:00
|
|
|
}
|
2019-04-14 21:46:45 +02: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{
|
2024-01-05 21:05:00 +01:00
|
|
|
BaseChatMessage: BaseChatMessage{
|
|
|
|
ChatConfig: ChatConfig{
|
|
|
|
ChatID: message.Chat.ID,
|
|
|
|
},
|
|
|
|
MessageID: message.MessageID,
|
|
|
|
},
|
2020-11-06 18:36:00 +01:00
|
|
|
DisableNotification: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := bot.Request(pinChatMessageConfig); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
unpinAllChatMessagesConfig := UnpinAllChatMessagesConfig{
|
2024-01-05 21:05:00 +01:00
|
|
|
ChatConfig: ChatConfig{ChatID: message.Chat.ID},
|
2020-11-06 18:36:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := bot.Request(unpinAllChatMessagesConfig); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 21:46:45 +02:00
|
|
|
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)
|
|
|
|
|
2021-12-04 03:16:31 +01:00
|
|
|
dice := NewDice(ChatID)
|
2020-03-30 22:35:53 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-08-20 07:27:42 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-08-20 07:27:42 +02:00
|
|
|
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
|
|
|
}
|
2020-07-26 22:51:33 +02:00
|
|
|
|
2021-08-20 21:41:04 +02:00
|
|
|
// TODO: figure out why test is failing
|
2021-08-20 22:08:34 +02:00
|
|
|
//
|
2021-08-20 21:41:04 +02:00
|
|
|
// func TestEditMessageMedia(t *testing.T) {
|
|
|
|
// bot, _ := getBot(t)
|
2020-07-26 22:51:33 +02:00
|
|
|
|
2021-08-20 22:08:34 +02:00
|
|
|
// msg := NewPhoto(ChatID, "tests/image.jpg")
|
2021-08-20 21:41:04 +02:00
|
|
|
// msg.Caption = "Test"
|
|
|
|
// m, err := bot.Send(msg)
|
2020-07-26 22:51:33 +02:00
|
|
|
|
2021-08-20 21:41:04 +02:00
|
|
|
// if err != nil {
|
|
|
|
// t.Error(err)
|
|
|
|
// }
|
2020-07-26 22:51:33 +02:00
|
|
|
|
2021-08-20 21:41:04 +02:00
|
|
|
// edit := EditMessageMediaConfig{
|
|
|
|
// BaseEdit: BaseEdit{
|
|
|
|
// ChatID: ChatID,
|
|
|
|
// MessageID: m.MessageID,
|
|
|
|
// },
|
|
|
|
// Media: NewInputMediaVideo(FilePath("tests/video.mp4")),
|
|
|
|
// }
|
2020-07-26 22:51:33 +02:00
|
|
|
|
2021-08-20 21:41:04 +02:00
|
|
|
// _, err = bot.Request(edit)
|
|
|
|
// if err != nil {
|
|
|
|
// t.Error(err)
|
|
|
|
// }
|
|
|
|
// }
|
2020-07-26 22:51:33 +02:00
|
|
|
|
|
|
|
func TestPrepareInputMediaForParams(t *testing.T) {
|
|
|
|
media := []interface{}{
|
2021-08-20 21:31:52 +02:00
|
|
|
NewInputMediaPhoto(FilePath("tests/image.jpg")),
|
2020-07-26 22:51:33 +02:00
|
|
|
NewInputMediaVideo(FileID("test")),
|
|
|
|
}
|
|
|
|
|
|
|
|
prepared := prepareInputMediaForParams(media)
|
|
|
|
|
2021-08-20 21:31:52 +02:00
|
|
|
if media[0].(InputMediaPhoto).Media != FilePath("tests/image.jpg") {
|
2020-07-26 22:51:33 +02:00
|
|
|
t.Error("Original media was changed")
|
|
|
|
}
|
|
|
|
|
2021-08-20 21:41:04 +02:00
|
|
|
if prepared[0].(InputMediaPhoto).Media != fileAttach("attach://file-0") {
|
2020-07-26 22:51:33 +02:00
|
|
|
t.Error("New media was not replaced")
|
|
|
|
}
|
|
|
|
|
|
|
|
if prepared[1].(InputMediaVideo).Media != FileID("test") {
|
|
|
|
t.Error("Passthrough value was not the same")
|
|
|
|
}
|
|
|
|
}
|