telegram-bot-api/bot_test.go

557 lines
10 KiB
Go
Raw Normal View History

package tgbotapi_test
import (
2016-01-04 01:12:03 +01:00
"github.com/go-telegram-bot-api/telegram-bot-api"
2015-11-21 12:44:26 +01:00
"io/ioutil"
"log"
2015-11-21 12:22:08 +01:00
"net/http"
"os"
"testing"
"time"
)
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.Error(err)
2015-11-21 13:34:30 +01:00
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.Error(err)
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.Error(err)
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.Error(err)
2015-11-20 18:26:12 +01:00
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.Error(err)
2015-11-20 18:26:12 +01:00
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.Error(err)
2015-11-20 16:30:50 +01:00
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.Error(err)
2015-11-20 16:30:50 +01:00
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.Error(err)
2015-11-21 12:36:43 +01:00
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.Error(err)
2015-11-21 12:36:43 +01:00
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.Error(err)
2015-11-20 18:26:12 +01:00
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.Error(err)
2015-11-20 17:43:56 +01:00
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.Error(err)
2015-11-20 17:43:56 +01:00
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.Error(err)
2015-11-20 17:43:56 +01:00
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"
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 {
t.Error(err)
2015-11-20 18:26:12 +01:00
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.Error(err)
2015-11-20 18:26:12 +01:00
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.Error(err)
2015-11-20 18:26:12 +01:00
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.Error(err)
2015-11-20 18:26:12 +01:00
t.Fail()
}
}
2016-04-14 00:06:18 +02:00
func TestSendWithContact(t *testing.T) {
bot, _ := getBot(t)
contact := tgbotapi.NewContact(ChatID, "5551234567", "Test")
if _, err := bot.Send(contact); err != nil {
t.Error(err)
2016-04-14 00:06:18 +02:00
t.Fail()
}
}
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
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.Error(err)
2015-11-20 18:26:12 +01:00
t.Fail()
}
}
2016-04-14 00:06:18 +02:00
func TestSendWithVenue(t *testing.T) {
bot, _ := getBot(t)
venue := tgbotapi.NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
if _, err := bot.Send(venue); err != nil {
t.Error(err)
2016-04-14 00:06:18 +02:00
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.Error(err)
2015-11-21 11:17:34 +01:00
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.Error(err)
2015-11-21 11:17:34 +01:00
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.Error(err)
2015-11-21 11:17:34 +01:00
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.Error(err)
2015-11-21 11:17:34 +01:00
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")
msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{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.Error(err)
2015-11-21 11:17:34 +01:00
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)
msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{true, 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 {
t.Error(err)
2015-11-21 11:17:34 +01:00
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.Error(err)
2015-11-20 17:43:56 +01:00
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.Error(err)
2015-11-20 17:43:56 +01:00
t.Fail()
}
}
2016-04-12 19:53:19 +02:00
func TestSendEditMessage(t *testing.T) {
bot, _ := getBot(t)
msg, err := bot.Send(tgbotapi.NewMessage(ChatID, "Testing editing."))
if err != nil {
t.Error(err)
2016-04-12 19:53:19 +02:00
t.Fail()
}
edit := tgbotapi.EditMessageTextConfig{
BaseEdit: tgbotapi.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
t.Fail()
}
}
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
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.Error(err)
2015-11-20 17:43:56 +01:00
t.Fail()
}
}
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
time.Sleep(time.Second * 2)
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.Error(err)
2015-11-21 12:22:08 +01:00
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
time.Sleep(time.Second * 2)
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.Error(err)
2015-11-21 12:36:43 +01:00
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.Error(err)
2015-11-20 17:43:56 +01:00
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 {
2016-05-22 17:16:28 +02:00
if update.Message == nil {
continue
}
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)
}
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)
}
}
func ExampleAnswerInlineQuery() {
bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") // create new bot
if err != nil {
log.Panic(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
2016-05-22 17:16:28 +02:00
if update.InlineQuery == nil { // if no inline query, ignore it
continue
}
article := tgbotapi.NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
article.Description = update.InlineQuery.Query
inlineConf := tgbotapi.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
IsPersonal: true,
CacheTime: 0,
Results: []interface{}{article},
}
if _, err := bot.AnswerInlineQuery(inlineConf); err != nil {
log.Println(err)
}
}
}