tests: add animation test

pull/449/head^2
Demian 3 years ago
parent 9a08d98f4e
commit 24189b3669

@ -2,6 +2,8 @@ package telebot
import (
"errors"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
@ -13,10 +15,6 @@ import (
"github.com/stretchr/testify/require"
)
const (
photoID = "AgACAgIAAxkDAAIBV16Ybpg7l2jPgMUiiLJ3WaQOUqTrAAJorjEbh2TBSPSOinaCHfydQO_pki4AAwEAAwIAA3kAA_NQAAIYBA"
)
var (
// required to test send and edit methods
token = os.Getenv("TELEBOT_SECRET")
@ -378,7 +376,7 @@ func TestBot(t *testing.T) {
assert.Equal(t, ErrBadRecipient, err)
photo := &Photo{
File: File{FileID: photoID},
File: FromURL("https://telegra.ph/file/65c5237b040ebf80ec278.jpg"),
Caption: t.Name(),
}
var msg *Message
@ -423,8 +421,38 @@ func TestBot(t *testing.T) {
edited, err := b.Edit(msg, photo)
require.NoError(t, err)
assert.Equal(t, edited.Photo.UniqueID, photo.UniqueID)
resp, err := http.Get("https://telegra.ph/file/274e5eb26f348b10bd8ee.mp4")
require.NoError(t, err)
defer resp.Body.Close()
file, err := ioutil.TempFile("", "")
require.NoError(t, err)
_, err = io.Copy(file, resp.Body)
require.NoError(t, err)
animation := &Animation{
File: FromDisk(file.Name()),
Caption: t.Name(),
FileName: "animation.gif",
}
msg, err := b.Send(msg.Chat, animation)
require.NoError(t, err)
if msg.Animation != nil {
assert.Equal(t, msg.Animation.FileID, animation.FileID)
} else {
assert.Equal(t, msg.Document.FileID, animation.FileID)
}
_, err = b.Edit(edited, animation)
require.NoError(t, err)
})
t.Run("Edit(what=Animation)", func(t *testing.T) {})
t.Run("Send(what=string)", func(t *testing.T) {
msg, err = b.Send(to, t.Name())
require.NoError(t, err)

@ -165,7 +165,6 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
}
// Send delivers animation through bot b to recipient.
// @see https://core.telegram.org/bots/api#sendanimation
func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{
"chat_id": to.Recipient(),
@ -184,29 +183,29 @@ func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, erro
params["height"] = strconv.Itoa(a.Height)
}
// file_name is required, without file_name GIFs sent as document
// file_name is required, without it animation sends as a document
if params["file_name"] == "" && a.File.OnDisk() {
params["file_name"] = filepath.Base(a.File.FileLocal)
}
msg, err := b.sendObject(&a.File, "animation", params, nil)
msg, err := b.sendObject(&a.File, "animation", params, thumbnailToFilemap(a.Thumbnail))
if err != nil {
return nil, err
}
if msg.Animation != nil {
msg.Animation.File.stealRef(&a.File)
if anim := msg.Animation; anim != nil {
anim.File.stealRef(&a.File)
*a = *msg.Animation
} else {
} else if doc := msg.Document; doc != nil {
*a = Animation{
File: msg.Document.File,
Thumbnail: msg.Document.Thumbnail,
MIME: msg.Document.MIME,
FileName: msg.Document.FileName,
File: doc.File,
Thumbnail: doc.Thumbnail,
MIME: doc.MIME,
FileName: doc.FileName,
}
}
a.Caption = msg.Caption
a.Caption = msg.Caption
return msg, nil
}

Loading…
Cancel
Save