telebot: implement OnMedia endpoint

pull/477/head
Demian 3 years ago
parent e05ea9522c
commit 811e9555ed

@ -504,38 +504,46 @@ func (b *Bot) handle(end string, c Context) bool {
}
func (b *Bot) handleMedia(c Context) bool {
m := c.Message()
var (
m = c.Message()
fired = true
)
switch {
case m.Photo != nil:
b.handle(OnPhoto, c)
fired = b.handle(OnPhoto, c)
case m.Voice != nil:
b.handle(OnVoice, c)
fired = b.handle(OnVoice, c)
case m.Audio != nil:
b.handle(OnAudio, c)
fired = b.handle(OnAudio, c)
case m.Animation != nil:
b.handle(OnAnimation, c)
fired = b.handle(OnAnimation, c)
case m.Document != nil:
b.handle(OnDocument, c)
fired = b.handle(OnDocument, c)
case m.Sticker != nil:
b.handle(OnSticker, c)
fired = b.handle(OnSticker, c)
case m.Video != nil:
b.handle(OnVideo, c)
fired = b.handle(OnVideo, c)
case m.VideoNote != nil:
b.handle(OnVideoNote, c)
fired = b.handle(OnVideoNote, c)
case m.Contact != nil:
b.handle(OnContact, c)
fired = b.handle(OnContact, c)
case m.Location != nil:
b.handle(OnLocation, c)
fired = b.handle(OnLocation, c)
case m.Venue != nil:
b.handle(OnVenue, c)
fired = b.handle(OnVenue, c)
case m.Game != nil:
b.handle(OnGame, c)
fired = b.handle(OnGame, c)
case m.Dice != nil:
b.handle(OnDice, c)
fired = b.handle(OnDice, c)
default:
return false
}
if !fired {
return b.handle(OnMedia, c)
}
return true
}

@ -150,6 +150,12 @@ func TestBotProcessUpdate(t *testing.T) {
t.Fatal(err)
}
b.Handle(OnMedia, func(c Context) error {
assert.NotNil(t, c.Message().Photo)
return nil
})
b.ProcessUpdate(Update{Message: &Message{Photo: &Photo{}}})
b.Handle("/start", func(c Context) error {
assert.Equal(t, "/start", c.Text())
return nil
@ -210,6 +216,10 @@ func TestBotProcessUpdate(t *testing.T) {
assert.NotNil(t, c.Message().Venue)
return nil
})
b.Handle(OnDice, func(c Context) error {
assert.NotNil(t, c.Message().Dice)
return nil
})
b.Handle(OnInvoice, func(c Context) error {
assert.NotNil(t, c.Message().Invoice)
return nil
@ -508,7 +518,7 @@ func TestBot(t *testing.T) {
edited, err = b.EditReplyMarkup(edited, nil)
require.NoError(t, err)
assert.Nil(t, edited.ReplyMarkup.InlineKeyboard)
assert.Nil(t, edited.ReplyMarkup)
_, err = b.Edit(edited, bad)
assert.Equal(t, ErrButtonDataInvalid, err)

@ -59,13 +59,13 @@ const (
OnContact = "\acontact"
OnLocation = "\alocation"
OnVenue = "\avenue"
OnPinned = "\apinned"
OnDice = "\adice"
OnInvoice = "\ainvoice"
OnPayment = "\apayment"
OnGame = "\agame"
OnPoll = "\apoll"
OnPollAnswer = "\apoll_answer"
OnPinned = "\apinned"
// Will fire on channel posts.
OnChannelPost = "\achannel_post"
@ -90,6 +90,9 @@ const (
// upon switching as its ID will change.
OnMigration = "\amigration"
// Will fire on any unhandled media.
OnMedia = "\amedia"
// Will fire on callback requests.
OnCallback = "\acallback"

Loading…
Cancel
Save