2018-10-09 06:34:34 +02:00
|
|
|
package tgbotapi
|
2015-07-29 11:41:41 +02:00
|
|
|
|
|
|
|
import (
|
2015-11-21 12:44:26 +01:00
|
|
|
"io/ioutil"
|
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
|
|
|
|
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
|
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")
|
2015-11-20 18:26:12 +01:00
|
|
|
msg.ParseMode = "markdown"
|
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")
|
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 {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewPhotoUpload(ChatID, "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
|
|
|
|
|
|
|
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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewPhotoUpload(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")
|
2018-10-09 06:34:34 +02:00
|
|
|
reader := FileReader{Name: "image.jpg", Reader: f, Size: -1}
|
2015-11-21 12:36:43 +01:00
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewPhotoUpload(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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewPhotoUpload(ChatID, "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 {
|
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
|
|
|
|
|
|
|
msg := NewPhotoUploadToChannel(Channel, "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)
|
2019-09-01 21:00:09 +02:00
|
|
|
|
|
|
|
data, _ := ioutil.ReadFile("tests/image.jpg")
|
|
|
|
b := FileBytes{Name: "image.jpg", Bytes: data}
|
|
|
|
|
|
|
|
msg := NewPhotoUploadToChannel(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)
|
2019-09-01 21:00:09 +02:00
|
|
|
|
|
|
|
f, _ := os.Open("tests/image.jpg")
|
|
|
|
reader := FileReader{Name: "image.jpg", Reader: f, Size: -1}
|
|
|
|
|
|
|
|
msg := NewPhotoUploadToChannel(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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewPhotoShare(ChatID, 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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewDocumentUpload(ChatID, "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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewDocumentShare(ChatID, 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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewAudioUpload(ChatID, "tests/audio.mp3")
|
2015-11-20 18:26:12 +01:00
|
|
|
msg.Title = "TEST"
|
|
|
|
msg.Duration = 10
|
|
|
|
msg.Performer = "TEST"
|
2015-12-13 18:00:20 +01:00
|
|
|
msg.MimeType = "audio/mpeg"
|
|
|
|
msg.FileSize = 688
|
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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewAudioShare(ChatID, 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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewVoiceUpload(ChatID, "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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewVoiceShare(ChatID, 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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewVideoUpload(ChatID, "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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewVideoShare(ChatID, 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)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewVideoNoteUpload(ChatID, 240, "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)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewVideoNoteShare(ChatID, 240, 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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewStickerUpload(ChatID, "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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewStickerShare(ChatID, 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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewStickerUpload(ChatID, "tests/image.jpg")
|
|
|
|
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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
msg := NewStickerShare(ChatID, ExistingStickerFileID)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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 {
|
2016-11-25 06:50:35 +01:00
|
|
|
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)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
bot.Request(RemoveWebhookConfig{})
|
2015-11-21 12:22:08 +01:00
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
wh := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
|
2017-12-29 08:08:01 +01:00
|
|
|
_, 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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
bot.Request(RemoveWebhookConfig{})
|
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)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
bot.Request(RemoveWebhookConfig{})
|
2015-11-21 12:36:43 +01:00
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
wh := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
|
2017-12-29 08:08:01 +01:00
|
|
|
_, 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
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
bot.Request(RemoveWebhookConfig{})
|
2015-11-21 12:36:43 +01:00
|
|
|
}
|
|
|
|
|
2018-09-22 03:20:28 +02:00
|
|
|
func TestSendWithMediaGroup(t *testing.T) {
|
|
|
|
bot, _ := getBot(t)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
cfg := NewMediaGroup(ChatID, []interface{}{
|
2020-11-05 19:58:19 +01:00
|
|
|
NewInputMediaPhoto("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/image.jpg"),
|
|
|
|
NewInputMediaVideo("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/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 {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:58:19 +01:00
|
|
|
if len(messages) != 2 {
|
2018-12-25 22:44:01 +01:00
|
|
|
t.Error()
|
|
|
|
}
|
2018-09-22 03:20:28 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2015-07-29 11:41:41 +02:00
|
|
|
msg.ReplyToMessageID = update.Message.MessageID
|
|
|
|
|
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)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
_, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
|
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)
|
|
|
|
|
|
|
|
_, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
|
|
|
|
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")
|
2017-06-08 11:32:05 +02:00
|
|
|
msg.ParseMode = "markdown"
|
|
|
|
message, _ := bot.Send(msg)
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
deleteMessageConfig := DeleteMessageConfig{
|
2017-06-08 11:32:05 +02:00
|
|
|
ChatID: message.Chat.ID,
|
|
|
|
MessageID: message.MessageID,
|
|
|
|
}
|
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")
|
2017-08-05 11:37:43 +02:00
|
|
|
msg.ParseMode = "markdown"
|
|
|
|
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,
|
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")
|
2017-08-05 11:37:43 +02:00
|
|
|
msg.ParseMode = "markdown"
|
|
|
|
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,
|
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{
|
2017-11-09 19:51:50 +01:00
|
|
|
ChatID: message.Chat.ID,
|
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
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetCommands(t *testing.T) {
|
|
|
|
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()
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|