telegram-bot-api/bot_test.go

453 lines
8.3 KiB
Go
Raw Normal View History

package tgbotapi_test
import (
2015-11-22 00:40:39 +01:00
"github.com/Syfaro/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"
)
2015-11-21 13:34:30 +01:00
const (
TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
ChatID = 76918703
ReplyToMessageID = 35
ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"
ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
ExistingAudioFileID = "BQADAgADRgADjMcoCdXg3lSIN49lAg"
ExistingVoiceFileID = "AwADAgADWQADjMcoCeul6r_q52IyAg"
ExistingVideoFileID = "BAADAgADZgADjMcoCav432kYe0FRAg"
ExistingStickerFileID = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
)
func getBot(t *testing.T) (*tgbotapi.BotAPI, error) {
bot, err := tgbotapi.NewBotAPI(TestToken)
2015-11-21 13:34:30 +01:00
if err != nil {
t.Fail()
}
2015-11-21 13:34:30 +01:00
return bot, err
}
func TestNewBotAPI_notoken(t *testing.T) {
_, err := tgbotapi.NewBotAPI("")
2015-11-20 11:42:26 +01:00
if err == nil {
t.Fail()
}
}
func TestGetUpdates(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
u := tgbotapi.NewUpdate(0)
2015-11-21 13:34:30 +01:00
_, err := bot.GetUpdates(u)
if err != nil {
t.Fail()
}
}
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-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
func TestSendWithMessageReply(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
msg.ReplyToMessageID = ReplyToMessageID
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Fail()
}
}
func TestSendWithMessageForward(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.NewForward(ChatID, ChatID, ReplyToMessageID)
_, err := bot.Send(msg)
2015-11-20 16:30:50 +01:00
if err != nil {
t.Fail()
}
}
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
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
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")
b := tgbotapi.FileBytes{Name: "image.jpg", Bytes: data}
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
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")
reader := tgbotapi.FileReader{Name: "image.jpg", Reader: f, Size: -1}
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
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
2015-11-21 13:34:30 +01:00
msg := tgbotapi.NewPhotoUpload(ChatID, "tests/image.jpg")
msg.ReplyToMessageID = ReplyToMessageID
2015-11-20 17:43:56 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.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
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
func TestSendWithNewDocument(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.NewDocumentUpload(ChatID, "tests/image.jpg")
_, err := bot.Send(msg)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Fail()
}
}
func TestSendWithExistingDocument(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.NewDocumentShare(ChatID, ExistingDocumentFileID)
_, err := bot.Send(msg)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Fail()
}
}
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
2015-11-21 13:34:30 +01:00
msg := tgbotapi.NewAudioUpload(ChatID, "tests/audio.mp3")
2015-11-20 18:26:12 +01:00
msg.Title = "TEST"
msg.Duration = 10
msg.Performer = "TEST"
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-20 18:26:12 +01:00
if err != nil {
t.Fail()
}
}
func TestSendWithExistingAudio(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
func TestSendWithNewVoice(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
func TestSendWithExistingVoice(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
func TestSendWithLocation(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 18:26:12 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.Send(tgbotapi.NewLocation(ChatID, 40, 40))
2015-11-20 18:26:12 +01:00
if err != nil {
t.Fail()
}
}
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
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
func TestSendWithExistingVideo(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
func TestSendWithNewSticker(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
func TestSendWithExistingSticker(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.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 {
t.Fail()
}
}
func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
2015-11-21 11:17:34 +01:00
msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.Fail()
}
}
func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 11:17:34 +01:00
2015-11-21 13:34:30 +01:00
msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
2015-11-21 11:17:34 +01:00
msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
2015-11-21 13:34:30 +01:00
_, err := bot.Send(msg)
2015-11-21 11:17:34 +01:00
if err != nil {
t.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
2015-11-21 13:34:30 +01:00
file := tgbotapi.FileConfig{ExistingPhotoFileID}
2015-11-20 17:43:56 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.GetFile(file)
2015-11-20 17:43:56 +01:00
if err != nil {
t.Fail()
}
}
func TestSendChatConfig(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.Send(tgbotapi.NewChatAction(ChatID, tgbotapi.ChatTyping))
2015-11-20 17:43:56 +01:00
if err != nil {
t.Fail()
}
}
func TestGetUserProfilePhotos(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
2015-11-21 13:34:30 +01:00
_, err := bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(ChatID))
2015-11-20 17:43:56 +01:00
if err != nil {
t.Fail()
}
}
2015-11-21 12:22:08 +01:00
func TestListenForWebhook(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:22:08 +01:00
2015-11-21 15:26:28 +01:00
_, handler := bot.ListenForWebhook("/")
2015-11-21 12:22:08 +01:00
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 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:22:08 +01:00
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 13:34:30 +01:00
_, err := bot.SetWebhook(wh)
2015-11-21 12:22:08 +01:00
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) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-21 12:36:43 +01:00
bot.RemoveWebhook()
wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
2015-11-21 13:34:30 +01:00
_, err := bot.SetWebhook(wh)
2015-11-21 12:36:43 +01:00
if err != nil {
t.Fail()
}
bot.RemoveWebhook()
}
2015-11-20 17:43:56 +01:00
func TestUpdatesChan(t *testing.T) {
2015-11-21 13:34:30 +01:00
bot, _ := getBot(t)
2015-11-20 17:43:56 +01:00
var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
ucfg.Timeout = 60
2015-11-21 15:31:59 +01:00
_, err := bot.GetUpdatesChan(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
2015-11-21 15:31:59 +01:00
updates, err := bot.GetUpdatesChan(u)
2015-11-21 15:26:28 +01:00
for update := range 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)
}
}
2015-11-21 12:53:00 +01:00
func ExampleNewWebhook() {
bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
if err != nil {
log.Fatal(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
if err != nil {
log.Fatal(err)
}
2015-11-21 15:26:28 +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)
}
}