Skip to content

Commit

Permalink
Merge pull request tucnak#250 from fullpipe/add-animation
Browse files Browse the repository at this point in the history
Add sendAnimation
  • Loading branch information
tucnak authored Mar 29, 2020
2 parents 7effb48 + 4e97ab5 commit 74866a0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
21 changes: 21 additions & 0 deletions media.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ func (v *Video) MediaFile() *File {
return &v.File
}

// Animation object represents a animation file.
type Animation struct {
File

Width int `json:"width"`
Height int `json:"height"`

Duration int `json:"duration,omitempty"`

// (Optional)
Caption string `json:"caption,omitempty"`
Thumbnail *Photo `json:"thumb,omitempty"`
MIME string `json:"mime_type,omitempty"`
FileName string `json:"file_name,omitempty"`
}

// MediaFile returns &Animation.File
func (v *Animation) MediaFile() *File {
return &v.File
}

// Voice object represents a voice note.
type Voice struct {
File
Expand Down
3 changes: 3 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ type Message struct {
// For a video, information about it.
Video *Video `json:"video"`

// For a animation, information about it.
Animation *Animation `json:"animation"`

// For a contact, contact information itself.
Contact *Contact `json:"contact"`

Expand Down
39 changes: 39 additions & 0 deletions sendable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package telebot
import (
"encoding/json"
"fmt"
"path/filepath"
"strconv"
)

Expand Down Expand Up @@ -166,6 +167,44 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
return msg, nil
}

// 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(),
"caption": a.Caption,
"file_name": a.FileName,
}

if a.Duration != 0 {
params["duration"] = strconv.Itoa(a.Duration)
}
if a.Width != 0 {
params["width"] = strconv.Itoa(a.Width)
}
if a.Height != 0 {
params["height"] = strconv.Itoa(a.Height)
}

// file_name is required, without file_name GIFs sent as document
if params["file_name"] == "" && a.File.OnDisk() {
params["file_name"] = filepath.Base(a.File.FileLocal)
}

embedSendOptions(params, opt)

msg, err := b.sendObject(&a.File, "animation", params, nil)
if err != nil {
return nil, err
}

msg.Animation.File.stealRef(&a.File)
*a = *msg.Animation
a.Caption = msg.Caption

return msg, nil
}

// Send delivers media through bot b to recipient.
func (v *Voice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{
Expand Down

0 comments on commit 74866a0

Please sign in to comment.