2020-04-05 18:29:05 +00:00
|
|
|
package telebot
|
2020-04-05 18:32:38 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-25 20:08:51 +00:00
|
|
|
"errors"
|
2020-04-05 18:32:38 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-04-16 14:52:15 +00:00
|
|
|
"strconv"
|
2020-04-23 18:56:27 +00:00
|
|
|
"strings"
|
2020-04-05 18:32:38 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-09-26 11:29:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-05 18:32:38 +00:00
|
|
|
)
|
|
|
|
|
2020-04-16 14:52:15 +00:00
|
|
|
const (
|
|
|
|
photoID = "AgACAgIAAxkDAAIBV16Ybpg7l2jPgMUiiLJ3WaQOUqTrAAJorjEbh2TBSPSOinaCHfydQO_pki4AAwEAAwIAA3kAA_NQAAIYBA"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// required to test send and edit methods
|
2020-04-25 14:21:19 +00:00
|
|
|
token = os.Getenv("TELEBOT_SECRET")
|
2020-04-16 14:52:15 +00:00
|
|
|
chatID, _ = strconv.ParseInt(os.Getenv("CHAT_ID"), 10, 64)
|
2020-04-24 14:59:44 +00:00
|
|
|
userID, _ = strconv.Atoi(os.Getenv("USER_ID"))
|
2020-04-16 14:52:15 +00:00
|
|
|
|
|
|
|
b, _ = newTestBot() // cached bot instance to avoid getMe method flooding
|
2020-04-24 14:59:44 +00:00
|
|
|
to = &Chat{ID: chatID} // to chat recipient for send and edit methods
|
|
|
|
user = &User{ID: userID} // to user recipient for some special cases
|
2020-04-16 14:52:15 +00:00
|
|
|
)
|
2020-04-06 09:57:18 +00:00
|
|
|
|
2020-04-05 18:32:38 +00:00
|
|
|
func defaultSettings() Settings {
|
2020-04-25 14:21:19 +00:00
|
|
|
return Settings{Token: token}
|
2020-04-05 18:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestBot() (*Bot, error) {
|
|
|
|
return NewBot(defaultSettings())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewBot(t *testing.T) {
|
|
|
|
var pref Settings
|
|
|
|
_, err := NewBot(pref)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
pref.Token = "BAD TOKEN"
|
|
|
|
_, err = NewBot(pref)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
pref.URL = "BAD URL"
|
|
|
|
_, err = NewBot(pref)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2020-04-28 12:29:23 +00:00
|
|
|
b, err := NewBot(Settings{offline: true})
|
2020-04-25 14:21:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-05 18:32:38 +00:00
|
|
|
assert.NotNil(t, b.Me)
|
|
|
|
assert.Equal(t, DefaultApiURL, b.URL)
|
|
|
|
assert.Equal(t, http.DefaultClient, b.client)
|
|
|
|
assert.Equal(t, 100, cap(b.Updates))
|
|
|
|
|
|
|
|
pref = defaultSettings()
|
|
|
|
client := &http.Client{Timeout: time.Minute}
|
|
|
|
pref.URL = "http://api.telegram.org" // not https
|
|
|
|
pref.Client = client
|
|
|
|
pref.Poller = &LongPoller{Timeout: time.Second}
|
|
|
|
pref.Updates = 50
|
2020-06-07 20:12:49 +00:00
|
|
|
pref.ParseMode = ModeHTML
|
2020-04-28 12:29:23 +00:00
|
|
|
pref.offline = true
|
2020-04-05 18:32:38 +00:00
|
|
|
|
|
|
|
b, err = NewBot(pref)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-05 18:32:38 +00:00
|
|
|
assert.Equal(t, client, b.client)
|
|
|
|
assert.Equal(t, pref.URL, b.URL)
|
|
|
|
assert.Equal(t, pref.Poller, b.Poller)
|
|
|
|
assert.Equal(t, 50, cap(b.Updates))
|
2020-06-07 20:12:49 +00:00
|
|
|
assert.Equal(t, ModeHTML, b.parseMode)
|
2020-04-05 18:32:38 +00:00
|
|
|
}
|
2020-04-06 09:57:18 +00:00
|
|
|
|
2020-04-06 20:02:46 +00:00
|
|
|
func TestBotHandle(t *testing.T) {
|
2020-04-25 14:21:19 +00:00
|
|
|
if b == nil {
|
|
|
|
t.Skip("Cached bot instance is bad (probably wrong or empty TELEBOT_SECRET)")
|
|
|
|
}
|
|
|
|
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle("/start", func(c Context) error { return nil })
|
2020-10-01 14:34:29 +00:00
|
|
|
assert.Contains(t, b.handlers, "/start")
|
2020-04-06 09:57:18 +00:00
|
|
|
|
2020-05-22 11:52:25 +00:00
|
|
|
reply := ReplyButton{Text: "reply"}
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(&reply, func(c Context) error { return nil })
|
2020-05-22 11:52:25 +00:00
|
|
|
|
|
|
|
inline := InlineButton{Unique: "inline"}
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(&inline, func(c Context) error { return nil })
|
2020-05-22 11:52:25 +00:00
|
|
|
|
|
|
|
btnReply := (&ReplyMarkup{}).Text("btnReply")
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(&btnReply, func(c Context) error { return nil })
|
2020-05-22 11:52:25 +00:00
|
|
|
|
|
|
|
btnInline := (&ReplyMarkup{}).Data("", "btnInline")
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(&btnInline, func(c Context) error { return nil })
|
2020-05-22 11:52:25 +00:00
|
|
|
|
2020-10-01 14:34:29 +00:00
|
|
|
assert.Contains(t, b.handlers, btnReply.CallbackUnique())
|
|
|
|
assert.Contains(t, b.handlers, btnInline.CallbackUnique())
|
|
|
|
assert.Contains(t, b.handlers, reply.CallbackUnique())
|
|
|
|
assert.Contains(t, b.handlers, inline.CallbackUnique())
|
2020-04-06 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 20:02:46 +00:00
|
|
|
func TestBotStart(t *testing.T) {
|
2020-04-25 14:21:19 +00:00
|
|
|
if token == "" {
|
|
|
|
t.Skip("TELEBOT_SECRET is required")
|
|
|
|
}
|
|
|
|
|
2020-04-06 09:57:18 +00:00
|
|
|
pref := defaultSettings()
|
|
|
|
pref.Poller = &LongPoller{}
|
2020-04-16 14:52:15 +00:00
|
|
|
|
2020-04-06 09:57:18 +00:00
|
|
|
b, err := NewBot(pref)
|
2020-04-25 14:21:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-06 09:57:18 +00:00
|
|
|
|
2020-04-06 13:04:57 +00:00
|
|
|
// remove webhook to be sure that bot can poll
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, b.RemoveWebhook())
|
2020-04-06 09:57:18 +00:00
|
|
|
|
2020-05-01 19:40:24 +00:00
|
|
|
go b.Start()
|
|
|
|
b.Stop()
|
2020-04-06 09:57:18 +00:00
|
|
|
|
2020-05-01 19:40:24 +00:00
|
|
|
tp := newTestPoller()
|
2020-04-06 20:02:46 +00:00
|
|
|
go func() {
|
|
|
|
tp.updates <- Update{Message: &Message{Text: "/start"}}
|
|
|
|
}()
|
|
|
|
|
2020-04-06 09:57:18 +00:00
|
|
|
b, err = NewBot(pref)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-06 20:02:46 +00:00
|
|
|
b.Poller = tp
|
2020-04-06 09:57:18 +00:00
|
|
|
|
|
|
|
var ok bool
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle("/start", func(c Context) error {
|
|
|
|
assert.Equal(t, c.Text(), "/start")
|
2020-05-01 19:40:24 +00:00
|
|
|
tp.done <- struct{}{}
|
2020-04-06 09:57:18 +00:00
|
|
|
ok = true
|
2020-07-25 19:44:50 +00:00
|
|
|
return nil
|
2020-04-06 09:57:18 +00:00
|
|
|
})
|
|
|
|
|
2020-05-01 19:40:24 +00:00
|
|
|
go b.Start()
|
|
|
|
<-tp.done
|
|
|
|
b.Stop()
|
|
|
|
|
2020-04-06 09:57:18 +00:00
|
|
|
assert.True(t, ok)
|
|
|
|
}
|
2020-04-06 20:02:46 +00:00
|
|
|
|
2020-05-02 13:44:19 +00:00
|
|
|
func TestBotProcessUpdate(t *testing.T) {
|
2020-04-28 12:29:23 +00:00
|
|
|
b, err := NewBot(Settings{Synchronous: true, offline: true})
|
2020-04-16 14:52:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-06 20:02:46 +00:00
|
|
|
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle("/start", func(c Context) error {
|
|
|
|
assert.Equal(t, "/start", c.Text())
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle("hello", func(c Context) error {
|
|
|
|
assert.Equal(t, "hello", c.Text())
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnText, func(c Context) error {
|
|
|
|
assert.Equal(t, "text", c.Text())
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnPinned, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message())
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnPhoto, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Photo)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnVoice, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Voice)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnAudio, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Audio)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnAnimation, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Animation)
|
|
|
|
return nil
|
2020-05-06 17:31:30 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnDocument, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Document)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnSticker, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Sticker)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnVideo, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Video)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnVideoNote, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().VideoNote)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnContact, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Contact)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnLocation, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Location)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnVenue, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Venue)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:49:38 +00:00
|
|
|
b.Handle(OnInvoice, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Invoice)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
b.Handle(OnPayment, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().Payment)
|
|
|
|
return nil
|
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnAddedToGroup, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().GroupCreated)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnUserJoined, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().UserJoined)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnUserLeft, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().UserLeft)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnNewGroupTitle, func(c Context) error {
|
|
|
|
assert.Equal(t, "title", c.Message().NewGroupTitle)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnNewGroupPhoto, func(c Context) error {
|
|
|
|
assert.NotNil(t, c.Message().NewGroupPhoto)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnGroupPhotoDeleted, func(c Context) error {
|
|
|
|
assert.True(t, c.Message().GroupPhotoDeleted)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnMigration, func(c Context) error {
|
|
|
|
from, to := c.Migration()
|
2020-04-06 20:02:46 +00:00
|
|
|
assert.Equal(t, int64(1), from)
|
|
|
|
assert.Equal(t, int64(2), to)
|
2020-07-25 19:44:50 +00:00
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnEdited, func(c Context) error {
|
|
|
|
assert.Equal(t, "edited", c.Message().Text)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnChannelPost, func(c Context) error {
|
|
|
|
assert.Equal(t, "post", c.Message().Text)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnEditedChannelPost, func(c Context) error {
|
|
|
|
assert.Equal(t, "edited post", c.Message().Text)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnCallback, func(c Context) error {
|
|
|
|
if data := c.Callback().Data; data[0] != '\f' {
|
|
|
|
assert.Equal(t, "callback", data)
|
2020-04-06 20:02:46 +00:00
|
|
|
}
|
2020-07-25 19:44:50 +00:00
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle("\funique", func(c Context) error {
|
|
|
|
assert.Equal(t, "callback", c.Callback().Data)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnQuery, func(c Context) error {
|
2020-10-13 13:33:00 +00:00
|
|
|
assert.Equal(t, "query", c.Data())
|
2020-07-25 19:44:50 +00:00
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnChosenInlineResult, func(c Context) error {
|
|
|
|
assert.Equal(t, "result", c.ChosenInlineResult().ResultID)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:49:38 +00:00
|
|
|
b.Handle(OnShipping, func(c Context) error {
|
|
|
|
assert.Equal(t, "shipping", c.ShippingQuery().ID)
|
|
|
|
return nil
|
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnCheckout, func(c Context) error {
|
|
|
|
assert.Equal(t, "checkout", c.PreCheckoutQuery().ID)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnPoll, func(c Context) error {
|
|
|
|
assert.Equal(t, "poll", c.Poll().ID)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
2020-07-25 19:44:50 +00:00
|
|
|
b.Handle(OnPollAnswer, func(c Context) error {
|
|
|
|
assert.Equal(t, "poll", c.PollAnswer().PollID)
|
|
|
|
return nil
|
2020-04-06 20:02:46 +00:00
|
|
|
})
|
|
|
|
|
2020-04-28 15:51:38 +00:00
|
|
|
b.ProcessUpdate(Update{Message: &Message{Text: "/start"}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Text: "/start@other_bot"}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Text: "hello"}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Text: "text"}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{PinnedMessage: &Message{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Photo: &Photo{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Voice: &Voice{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Audio: &Audio{}}})
|
2020-05-06 17:31:30 +00:00
|
|
|
b.ProcessUpdate(Update{Message: &Message{Animation: &Animation{}}})
|
2020-04-28 15:51:38 +00:00
|
|
|
b.ProcessUpdate(Update{Message: &Message{Document: &Document{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Sticker: &Sticker{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Video: &Video{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{VideoNote: &VideoNote{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Contact: &Contact{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Location: &Location{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Venue: &Venue{}}})
|
2020-07-25 19:49:38 +00:00
|
|
|
b.ProcessUpdate(Update{Message: &Message{Invoice: &Invoice{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Payment: &Payment{}}})
|
2020-05-02 13:44:19 +00:00
|
|
|
b.ProcessUpdate(Update{Message: &Message{Dice: &Dice{}}})
|
2020-04-28 15:51:38 +00:00
|
|
|
b.ProcessUpdate(Update{Message: &Message{GroupCreated: true}})
|
2020-05-02 13:44:19 +00:00
|
|
|
b.ProcessUpdate(Update{Message: &Message{UserJoined: &User{ID: 1}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{UsersJoined: []User{{ID: 1}}}})
|
2020-04-28 15:51:38 +00:00
|
|
|
b.ProcessUpdate(Update{Message: &Message{UserLeft: &User{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{NewGroupTitle: "title"}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{NewGroupPhoto: &Photo{}}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{GroupPhotoDeleted: true}})
|
|
|
|
b.ProcessUpdate(Update{Message: &Message{Chat: &Chat{ID: 1}, MigrateTo: 2}})
|
|
|
|
b.ProcessUpdate(Update{EditedMessage: &Message{Text: "edited"}})
|
|
|
|
b.ProcessUpdate(Update{ChannelPost: &Message{Text: "post"}})
|
|
|
|
b.ProcessUpdate(Update{ChannelPost: &Message{PinnedMessage: &Message{}}})
|
|
|
|
b.ProcessUpdate(Update{EditedChannelPost: &Message{Text: "edited post"}})
|
|
|
|
b.ProcessUpdate(Update{Callback: &Callback{MessageID: "inline", Data: "callback"}})
|
|
|
|
b.ProcessUpdate(Update{Callback: &Callback{Data: "callback"}})
|
|
|
|
b.ProcessUpdate(Update{Callback: &Callback{Data: "\funique|callback"}})
|
|
|
|
b.ProcessUpdate(Update{Query: &Query{Text: "query"}})
|
|
|
|
b.ProcessUpdate(Update{ChosenInlineResult: &ChosenInlineResult{ResultID: "result"}})
|
2020-07-25 19:49:38 +00:00
|
|
|
b.ProcessUpdate(Update{ShippingQuery: &ShippingQuery{ID: "shipping"}})
|
2020-04-28 15:51:38 +00:00
|
|
|
b.ProcessUpdate(Update{PreCheckoutQuery: &PreCheckoutQuery{ID: "checkout"}})
|
|
|
|
b.ProcessUpdate(Update{Poll: &Poll{ID: "poll"}})
|
|
|
|
b.ProcessUpdate(Update{PollAnswer: &PollAnswer{PollID: "poll"}})
|
2020-04-06 20:02:46 +00:00
|
|
|
}
|
2020-04-16 14:52:15 +00:00
|
|
|
|
2020-07-25 20:08:51 +00:00
|
|
|
func TestBotOnError(t *testing.T) {
|
|
|
|
b, err := NewBot(Settings{Synchronous: true, offline: true})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
2020-07-25 21:00:06 +00:00
|
|
|
b.OnError = func(err error, c Context) {
|
|
|
|
assert.Equal(t, b, c.(*nativeContext).b)
|
2020-07-25 20:08:51 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
|
|
|
|
b.runHandler(func(c Context) error {
|
|
|
|
return errors.New("not nil")
|
2020-07-25 21:00:06 +00:00
|
|
|
}, &nativeContext{b: b})
|
2020-07-25 20:08:51 +00:00
|
|
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:57:28 +00:00
|
|
|
func TestBot(t *testing.T) {
|
2020-04-25 14:21:19 +00:00
|
|
|
if b == nil {
|
|
|
|
t.Skip("Cached bot instance is bad (probably wrong or empty TELEBOT_SECRET)")
|
|
|
|
}
|
2020-04-16 14:52:15 +00:00
|
|
|
if chatID == 0 {
|
2020-04-24 14:59:44 +00:00
|
|
|
t.Skip("CHAT_ID is required for Bot methods test")
|
2020-04-16 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.Send(to, nil)
|
2020-04-16 15:57:28 +00:00
|
|
|
assert.Equal(t, ErrUnsupportedWhat, err)
|
|
|
|
_, err = b.Edit(&Message{Chat: &Chat{}}, nil)
|
|
|
|
assert.Equal(t, ErrUnsupportedWhat, err)
|
2020-04-16 14:52:15 +00:00
|
|
|
|
2020-04-16 15:57:28 +00:00
|
|
|
_, err = b.Send(nil, "")
|
|
|
|
assert.Equal(t, ErrBadRecipient, err)
|
|
|
|
_, err = b.Forward(nil, nil)
|
|
|
|
assert.Equal(t, ErrBadRecipient, err)
|
2020-04-16 14:52:15 +00:00
|
|
|
|
2020-06-07 18:41:13 +00:00
|
|
|
photo := &Photo{
|
|
|
|
File: File{FileID: photoID},
|
|
|
|
Caption: t.Name(),
|
|
|
|
}
|
|
|
|
var msg *Message
|
2020-04-16 14:52:15 +00:00
|
|
|
|
2020-06-07 18:41:13 +00:00
|
|
|
t.Run("Send(what=Sendable)", func(t *testing.T) {
|
|
|
|
msg, err = b.Send(to, photo)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-16 14:52:15 +00:00
|
|
|
assert.NotNil(t, msg.Photo)
|
|
|
|
assert.Equal(t, photo.Caption, msg.Caption)
|
2020-06-07 18:41:13 +00:00
|
|
|
})
|
2020-05-06 17:31:30 +00:00
|
|
|
|
2020-06-09 20:28:28 +00:00
|
|
|
t.Run("SendAlbum()", func(t *testing.T) {
|
|
|
|
_, err = b.SendAlbum(nil, nil)
|
|
|
|
assert.Equal(t, ErrBadRecipient, err)
|
|
|
|
|
|
|
|
_, err = b.SendAlbum(to, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
msgs, err := b.SendAlbum(to, Album{photo, photo})
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-09 20:28:28 +00:00
|
|
|
assert.Len(t, msgs, 2)
|
|
|
|
assert.NotEmpty(t, msgs[0].AlbumID)
|
|
|
|
})
|
|
|
|
|
2020-06-07 20:12:49 +00:00
|
|
|
t.Run("EditCaption()+ParseMode", func(t *testing.T) {
|
|
|
|
b.parseMode = ModeHTML
|
2020-06-10 10:52:35 +00:00
|
|
|
|
|
|
|
edited, err := b.EditCaption(msg, "<b>new caption with html</b>")
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-10 10:52:35 +00:00
|
|
|
assert.Equal(t, "new caption with html", edited.Caption)
|
|
|
|
assert.Equal(t, EntityBold, edited.CaptionEntities[0].Type)
|
2020-06-07 20:12:49 +00:00
|
|
|
|
2020-06-10 10:52:35 +00:00
|
|
|
edited, err = b.EditCaption(msg, "*new caption with markdown*", ModeMarkdown)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-10 10:52:35 +00:00
|
|
|
assert.Equal(t, "new caption with markdown", edited.Caption)
|
|
|
|
assert.Equal(t, EntityBold, edited.CaptionEntities[0].Type)
|
|
|
|
|
|
|
|
b.parseMode = ModeDefault
|
2020-04-16 14:52:15 +00:00
|
|
|
})
|
2020-04-16 15:57:28 +00:00
|
|
|
|
2020-06-07 18:41:13 +00:00
|
|
|
t.Run("Edit(what=InputMedia)", func(t *testing.T) {
|
|
|
|
edited, err := b.Edit(msg, photo)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-09 19:26:59 +00:00
|
|
|
assert.Equal(t, edited.Photo.UniqueID, photo.UniqueID)
|
2020-06-07 18:41:13 +00:00
|
|
|
})
|
2020-04-16 15:57:28 +00:00
|
|
|
|
|
|
|
t.Run("Send(what=string)", func(t *testing.T) {
|
|
|
|
msg, err = b.Send(to, t.Name())
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-16 15:57:28 +00:00
|
|
|
assert.Equal(t, t.Name(), msg.Text)
|
|
|
|
|
|
|
|
rpl, err := b.Reply(msg, t.Name())
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-16 15:57:28 +00:00
|
|
|
assert.Equal(t, rpl.Text, msg.Text)
|
|
|
|
assert.NotNil(t, rpl.ReplyTo)
|
|
|
|
assert.Equal(t, rpl.ReplyTo, msg)
|
|
|
|
assert.True(t, rpl.IsReply())
|
|
|
|
|
|
|
|
fwd, err := b.Forward(to, msg)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-16 15:57:28 +00:00
|
|
|
assert.NotNil(t, msg, fwd)
|
|
|
|
assert.True(t, fwd.IsForwarded())
|
|
|
|
|
|
|
|
fwd.ID += 1 // nonexistent message
|
2020-05-05 19:24:40 +00:00
|
|
|
_, err = b.Forward(to, fwd)
|
2020-04-16 15:57:28 +00:00
|
|
|
assert.Equal(t, ErrToForwardNotFound, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Edit(what=string)", func(t *testing.T) {
|
|
|
|
msg, err = b.Edit(msg, t.Name())
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-16 15:57:28 +00:00
|
|
|
assert.Equal(t, t.Name(), msg.Text)
|
|
|
|
|
|
|
|
_, err = b.Edit(msg, msg.Text)
|
|
|
|
assert.Error(t, err) // message is not modified
|
|
|
|
})
|
|
|
|
|
2020-06-07 18:41:13 +00:00
|
|
|
t.Run("Edit(what=ReplyMarkup)", func(t *testing.T) {
|
|
|
|
good := &ReplyMarkup{
|
2020-05-22 11:52:25 +00:00
|
|
|
InlineKeyboard: [][]InlineButton{
|
|
|
|
{{
|
|
|
|
Data: "btn",
|
|
|
|
Text: "Hi Telebot!",
|
|
|
|
}},
|
|
|
|
},
|
2020-04-23 18:56:27 +00:00
|
|
|
}
|
2020-06-07 18:41:13 +00:00
|
|
|
bad := &ReplyMarkup{
|
2020-05-22 11:52:25 +00:00
|
|
|
InlineKeyboard: [][]InlineButton{
|
|
|
|
{{
|
|
|
|
Data: strings.Repeat("*", 65),
|
|
|
|
Text: "Bad Button",
|
|
|
|
}},
|
|
|
|
},
|
2020-04-23 18:56:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 18:41:13 +00:00
|
|
|
edited, err := b.Edit(msg, good)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-07 18:41:13 +00:00
|
|
|
assert.Equal(t, edited.ReplyMarkup.InlineKeyboard, good.InlineKeyboard)
|
2020-04-23 18:56:27 +00:00
|
|
|
|
2020-06-07 18:41:13 +00:00
|
|
|
edited, err = b.EditReplyMarkup(edited, nil)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-07 18:41:13 +00:00
|
|
|
assert.Nil(t, edited.ReplyMarkup.InlineKeyboard)
|
2020-04-23 18:56:27 +00:00
|
|
|
|
2020-06-07 18:41:13 +00:00
|
|
|
_, err = b.Edit(edited, bad)
|
2020-04-23 18:56:27 +00:00
|
|
|
assert.Equal(t, ErrButtonDataInvalid, err)
|
|
|
|
})
|
2020-04-26 12:55:04 +00:00
|
|
|
|
2020-06-07 18:41:13 +00:00
|
|
|
t.Run("Edit(what=Location)", func(t *testing.T) {
|
|
|
|
loc := &Location{Lat: 42, Lng: 69, LivePeriod: 60}
|
|
|
|
edited, err := b.Send(to, loc)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-07 18:41:13 +00:00
|
|
|
assert.NotNil(t, edited.Location)
|
|
|
|
|
|
|
|
loc = &Location{Lat: loc.Lng, Lng: loc.Lat}
|
|
|
|
edited, err = b.Edit(edited, *loc)
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-07 18:41:13 +00:00
|
|
|
assert.NotNil(t, edited.Location)
|
|
|
|
})
|
|
|
|
|
2020-06-09 20:28:28 +00:00
|
|
|
// should be the last
|
|
|
|
t.Run("Delete()", func(t *testing.T) {
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, b.Delete(msg))
|
2020-06-09 20:28:28 +00:00
|
|
|
})
|
|
|
|
|
2020-05-06 17:31:30 +00:00
|
|
|
t.Run("Notify()", func(t *testing.T) {
|
|
|
|
assert.Equal(t, ErrBadRecipient, b.Notify(nil, Typing))
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, b.Notify(to, Typing))
|
2020-05-06 17:31:30 +00:00
|
|
|
})
|
|
|
|
|
2020-06-09 20:28:28 +00:00
|
|
|
t.Run("Answer()", func(t *testing.T) {
|
|
|
|
assert.Error(t, b.Answer(&Query{}, &QueryResponse{
|
|
|
|
Results: Results{&ArticleResult{}},
|
|
|
|
}))
|
2020-05-06 17:31:30 +00:00
|
|
|
})
|
|
|
|
|
2020-06-09 20:28:28 +00:00
|
|
|
t.Run("Respond()", func(t *testing.T) {
|
|
|
|
assert.Error(t, b.Respond(&Callback{}, &CallbackResponse{}))
|
2020-04-26 12:55:04 +00:00
|
|
|
})
|
2020-05-21 10:38:21 +00:00
|
|
|
|
|
|
|
t.Run("Payments", func(t *testing.T) {
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
b.Accept(&PreCheckoutQuery{})
|
|
|
|
b.Accept(&PreCheckoutQuery{}, "error")
|
|
|
|
})
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
b.Ship(&ShippingQuery{})
|
|
|
|
b.Ship(&ShippingQuery{}, "error")
|
|
|
|
b.Ship(&ShippingQuery{}, ShippingOption{}, ShippingOption{})
|
|
|
|
assert.Equal(t, ErrUnsupportedWhat, b.Ship(&ShippingQuery{}, 0))
|
|
|
|
})
|
|
|
|
})
|
2020-06-09 20:28:28 +00:00
|
|
|
|
|
|
|
t.Run("Commands", func(t *testing.T) {
|
|
|
|
orig := []Command{{
|
|
|
|
Text: "test",
|
|
|
|
Description: "test command",
|
|
|
|
}}
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, b.SetCommands(orig))
|
2020-06-09 20:28:28 +00:00
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
cmds, err := b.Commands()
|
2020-09-26 11:29:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-09 20:28:28 +00:00
|
|
|
assert.Equal(t, orig, cmds)
|
|
|
|
})
|
2020-04-16 14:52:15 +00:00
|
|
|
}
|