telegram-bot-api/bot_test.go

548 lines
9.7 KiB
Go
Raw Normal View History

package tgbotapi_test
import (
2015-11-20 11:42:26 +01:00
"github.com/zhulik/telegram-bot-api"
2015-11-21 12:44:26 +01:00
"io/ioutil"
"log"
2015-11-21 12:22:08 +01:00
"net/http"
"net/http/httptest"
"os"
2015-11-21 12:22:08 +01:00
"strings"
"testing"
)
func TestMain(m *testing.M) {
botToken := os.Getenv("TELEGRAM_API_TOKEN")
if botToken == "" {
log.Panic("You must provide a TELEGRAM_API_TOKEN env variable to test!")
}
os.Exit(m.Run())
}
func TestNewBotAPI_notoken(t *testing.T) {
_, err := tgbotapi.NewBotAPI("")
2015-11-20 11:42:26 +01:00
if err == nil {
t.Fail()
}
}
func TestNewBotAPI_token(t *testing.T) {
_, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
}
func TestGetUpdates(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
u := tgbotapi.NewUpdate(0)
_, err = bot.GetUpdates(u)
if err != nil {
2015-11-21 12:22:08 +01:00
t.Log(err.Error())
t.Fail()
}
}
2015-11-20 16:30:50 +01:00
func TestSendWithMessage(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
2015-11-20 16:30:50 +01:00
msg := tgbotapi.NewMessage(76918703, "A test message from the test library in telegram-bot-api")
2015-11-20 18:26:12 +01:00
msg.ParseMode = "markdown"
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithMessageReply(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewMessage(76918703, "A test message from the test library in telegram-bot-api")
msg.ReplyToMessageID = 480
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithMessageForward(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewForward(76918703, 76918703, 480)
2015-11-20 16:30:50 +01:00
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
2015-11-20 17:43:56 +01:00
func TestSendWithNewPhoto(t *testing.T) {
2015-11-20 16:30:50 +01:00
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewPhotoUpload(76918703, "tests/image.jpg")
2015-11-20 18:26:12 +01:00
msg.Caption = "Test"
2015-11-20 16:30:50 +01:00
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
2015-11-21 12:36:43 +01:00
func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
data, _ := ioutil.ReadFile("tests/image.jpg")
b := tgbotapi.FileBytes{Name: "image.jpg", Bytes: data}
msg := tgbotapi.NewPhotoUpload(76918703, b)
msg.Caption = "Test"
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithNewPhotoWithFileReader(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
f, _ := os.Open("tests/image.jpg")
reader := tgbotapi.FileReader{Name: "image.jpg", Reader: f, Size: -1}
msg := tgbotapi.NewPhotoUpload(76918703, reader)
msg.Caption = "Test"
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
2015-11-20 18:26:12 +01:00
func TestSendWithNewPhotoReply(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewPhotoUpload(76918703, "tests/image.jpg")
msg.ReplyToMessageID = 480
2015-11-20 17:43:56 +01:00
2015-11-20 18:26:12 +01:00
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
2015-11-20 17:43:56 +01:00
func TestSendWithExistingPhoto(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewPhotoShare(76918703, "AgADAgADxKcxG4cBswqt13DnHOgbmBxDhCoABC0h01_AL4SKe20BAAEC")
2015-11-20 18:26:12 +01:00
msg.Caption = "Test"
2015-11-20 17:43:56 +01:00
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithNewDocument(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewDocumentUpload(76918703, "tests/image.jpg")
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithExistingDocument(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewDocumentShare(76918703, "BQADAgADBwADhwGzCjWgiUU4T8VNAg")
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
2015-11-20 18:26:12 +01:00
func TestSendWithNewAudio(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewAudioUpload(76918703, "tests/audio.mp3")
msg.Title = "TEST"
msg.Duration = 10
msg.Performer = "TEST"
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithExistingAudio(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewAudioShare(76918703, "BQADAgADMwADhwGzCkYFlCTpxiP6Ag")
msg.Title = "TEST"
msg.Duration = 10
msg.Performer = "TEST"
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithNewVoice(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewVoiceUpload(76918703, "tests/voice.ogg")
msg.Duration = 10
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithExistingVoice(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewVoiceShare(76918703, "AwADAgADIgADhwGzCigyMW_GUtWIAg")
msg.Duration = 10
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithLocation(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
_, err = bot.Send(tgbotapi.NewLocation(76918703, 40, 40))
if err != nil {
t.Fail()
}
}
2015-11-21 11:17:34 +01:00
func TestSendWithNewVideo(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewVideoUpload(76918703, "tests/video.mp4")
msg.Duration = 10
msg.Caption = "TEST"
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithExistingVideo(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewVideoShare(76918703, "BAADAgADRgADhwGzCopBeKJ7rv9SAg")
msg.Duration = 10
msg.Caption = "TEST"
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithNewSticker(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewStickerUpload(76918703, "tests/image.jpg")
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithExistingSticker(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewStickerShare(76918703, "BQADAgADUwADhwGzCmwtOypNFlrRAg")
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewStickerUpload(76918703, "tests/image.jpg")
msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewStickerShare(76918703, "BQADAgADUwADhwGzCmwtOypNFlrRAg")
msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
2015-11-20 17:43:56 +01:00
func TestGetFile(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
file := tgbotapi.FileConfig{"BQADAgADBwADhwGzCjWgiUU4T8VNAg"}
_, err = bot.GetFile(file)
if err != nil {
t.Fail()
}
}
func TestSendChatConfig(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
2015-11-21 12:44:26 +01:00
_, err = bot.Send(tgbotapi.NewChatAction(76918703, tgbotapi.ChatTyping))
2015-11-20 17:43:56 +01:00
if err != nil {
t.Fail()
}
}
func TestGetUserProfilePhotos(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
_, err = bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(76918703))
if err != nil {
t.Fail()
}
}
2015-11-21 12:22:08 +01:00
func TestListenForWebhook(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
handler := bot.ListenForWebhook("/")
req, _ := http.NewRequest("GET", "", strings.NewReader("{}"))
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Home page didn't return %v", http.StatusOK)
}
}
2015-11-21 12:36:43 +01:00
func TestSetWebhookWithCert(t *testing.T) {
2015-11-21 12:22:08 +01:00
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
2015-11-21 12:26:39 +01:00
bot.RemoveWebhook()
2015-11-21 12:22:08 +01:00
2015-11-21 12:26:39 +01:00
wh := tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
2015-11-21 12:22:08 +01:00
_, err = bot.SetWebhook(wh)
if err != nil {
t.Fail()
}
2015-11-21 12:26:39 +01:00
bot.RemoveWebhook()
2015-11-21 12:22:08 +01:00
}
2015-11-21 12:36:43 +01:00
func TestSetWebhookWithoutCert(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
bot.RemoveWebhook()
wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
_, err = bot.SetWebhook(wh)
if err != nil {
t.Fail()
}
bot.RemoveWebhook()
}
2015-11-20 17:43:56 +01:00
func TestUpdatesChan(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
ucfg.Timeout = 60
err = bot.UpdatesChan(ucfg)
2015-11-20 18:26:12 +01:00
2015-11-20 17:43:56 +01:00
if err != nil {
t.Fail()
}
}
func ExampleNewBotAPI() {
bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
err = bot.UpdatesChan(u)
for update := range bot.Updates {
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
msg.ReplyToMessageID = update.Message.MessageID
2015-11-20 16:30:50 +01:00
bot.Send(msg)
}
}