Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .files.audio import Audio
from .files.voice import Voice
from .files.document import Document
from .files.animation import Animation
from .files.sticker import Sticker, StickerSet, MaskPosition
from .files.video import Video
from .files.contact import Contact
Expand All @@ -45,7 +46,6 @@
from .files.file import File
from .parsemode import ParseMode
from .messageentity import MessageEntity
from .games.animation import Animation
from .games.game import Game
from .games.callbackgame import CallbackGame
from .payment.shippingaddress import ShippingAddress
Expand Down Expand Up @@ -99,6 +99,8 @@
from .files.inputmedia import InputMedia
from .files.inputmediavideo import InputMediaVideo
from .files.inputmediaphoto import InputMediaPhoto
from .files.inputmediaanimation import InputMediaAnimation
from .files.inputmediadocument import InputMediaDocument
from .version import __version__ # flake8: noqa

__author__ = 'devs@python-telegram-bot.org'
Expand All @@ -125,5 +127,5 @@
'Game', 'GameHighScore', 'VideoNote', 'LabeledPrice', 'SuccessfulPayment', 'ShippingOption',
'ShippingAddress', 'PreCheckoutQuery', 'OrderInfo', 'Invoice', 'ShippingQuery', 'ChatPhoto',
'StickerSet', 'MaskPosition', 'CallbackGame', 'InputMedia', 'InputMediaPhoto',
'InputMediaVideo'
'InputMediaVideo', 'InputMediaAnimation', 'InputMediaDocument'
]
88 changes: 86 additions & 2 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

from telegram import (User, Message, Update, Chat, ChatMember, UserProfilePhotos, File,
ReplyMarkup, TelegramObject, WebhookInfo, GameHighScore, StickerSet,
PhotoSize, Audio, Document, Sticker, Video, Voice, VideoNote, Location,
Venue, Contact, InputFile)
PhotoSize, Audio, Document, Sticker, Video, Animation, Voice, VideoNote,
Location, Venue, Contact, InputFile)
from telegram.error import InvalidToken, TelegramError
from telegram.utils.helpers import to_timestamp
from telegram.utils.request import Request
Expand Down Expand Up @@ -773,6 +773,88 @@ def send_video_note(self,

return url, data

@log
@message
def send_animation(self,
chat_id,
animation,
duration=None,
width=None,
height=None,
thumb=None,
caption=None,
parse_mode=None,
disable_notification=False,
reply_to_message_id=None,
reply_markup=None,
timeout=20,
**kwargs):
"""
Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound).

Args:
chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username
of the target channel (in the format @channelusername).
animation (:obj:`str` | `filelike object` | :class:`telegram.Animation`): Animation to
send. Pass a file_id as String to send an animation that exists on the Telegram
servers (recommended), pass an HTTP URL as a String for Telegram to get an
animation from the Internet, or upload a new animation using multipart/form-data.
Lastly you can pass an existing :class:`telegram.Animation` object to send.
duration (:obj:`int`, optional): Duration of sent animation in seconds.
width (:obj:`int`, optional): Animation width.
height (:obj:`int`, optional): Animation height.
thumb (:obj:`str` | `filelike object` | :class:`telegram.PhotoSize`): Thumbnail of the
file sent. The thumbnail should be in JPEG format and less than 200 kB in size.
A thumbnail‘s width and height should not exceed 90. Ignored if the file is not
is passed as a string or file_id.
caption (:obj:`str`, optional): Animation caption (may also be used when resending
animations by file_id), 0-200 characters.
parse_mode (:obj:`str`, optional): Send Markdown or HTML, if you want Telegram apps to
show bold, italic, fixed-width text or inline URLs in the media caption. See the
constants in :class:`telegram.ParseMode` for the available modes.
disable_notification (:obj:`bool`, optional): Sends the message silently. Users will
receive a notification with no sound.
reply_to_message_id (:obj:`int`, optional): If the message is a reply, ID of the
original message.
reply_markup (:class:`telegram.ReplyMarkup`, optional): Additional interface options. A
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
to remove reply keyboard or to force a reply from the user.
timeout (:obj:`int` | :obj:`float`, optional): Send file timeout (default: 20 seconds).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.

Returns:
:class:`telegram.Message`: On success, the sent Message is returned.

Raises:
:class:`telegram.TelegramError`

"""
url = '{0}/sendAnimation'.format(self.base_url)

if isinstance(animation, Animation):
animation = animation.file_id
if InputFile.is_file(animation):
animation = InputFile(animation)

data = {'chat_id': chat_id, 'animation': animation}

if duration:
data['duration'] = duration
if width:
data['width'] = width
if height:
data['height'] = height
if thumb:
if InputFile.is_file(thumb):
thumb = InputFile(thumb)
data['thumb'] = thumb
if caption:
data['caption'] = caption
if parse_mode:
data['parse_mode'] = parse_mode

return url, data

@log
@message
def send_voice(self,
Expand Down Expand Up @@ -3132,6 +3214,8 @@ def __reduce__(self):
"""Alias for :attr:`send_sticker`"""
sendVideo = send_video
"""Alias for :attr:`send_video`"""
sendAnimation = send_animation
"""Alias for :attr:`send_animation`"""
sendVoice = send_voice
"""Alias for :attr:`send_voice`"""
sendVideoNote = send_video_note
Expand Down
13 changes: 13 additions & 0 deletions telegram/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@ def send_document(self, *args, **kwargs):
"""
return self.bot.send_document(self.id, *args, **kwargs)

def send_animation(self, *args, **kwargs):
"""Shortcut for::

bot.send_animation(Chat.id, *args, **kwargs)

Where Chat is the current instance.

Returns:
:class:`telegram.Message`: On success, instance representing the message posted.

"""
return self.bot.send_animation(self.id, *args, **kwargs)

def send_sticker(self, *args, **kwargs):
"""Shortcut for::

Expand Down
9 changes: 9 additions & 0 deletions telegram/ext/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ def filter(self, message):
document = _Document()
""":obj:`Filter`: Messages that contain :class:`telegram.Document`."""

class _Animation(BaseFilter):
name = 'Filters.animation'

def filter(self, message):
return bool(message.animation)

animation = _Animation()
""":obj:`Filter`: Messages that contain :class:`telegram.Animation`."""

class _Photo(BaseFilter):
name = 'Filters.photo'

Expand Down
12 changes: 12 additions & 0 deletions telegram/games/animation.py → telegram/files/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Animation(TelegramObject):

Attributes:
file_id (:obj:`str`): Unique file identifier.
width (:obj:`int`): Video width as defined by sender.
height (:obj:`int`): Video height as defined by sender.
duration (:obj:`int`): Duration of the video in seconds as defined by sender.
thumb (:class:`telegram.PhotoSize`): Optional. Animation thumbnail as defined
by sender.
file_name (:obj:`str`): Optional. Original animation filename as defined by sender.
Expand All @@ -34,6 +37,9 @@ class Animation(TelegramObject):

Args:
file_id (:obj:`str`): Unique file identifier.
width (:obj:`int`): Video width as defined by sender.
height (:obj:`int`): Video height as defined by sender.
duration (:obj:`int`): Duration of the video in seconds as defined by sender.
thumb (:class:`telegram.PhotoSize`, optional): Animation thumbnail as defined by sender.
file_name (:obj:`str`, optional): Original animation filename as defined by sender.
mime_type (:obj:`str`, optional): MIME type of the file as defined by sender.
Expand All @@ -43,12 +49,18 @@ class Animation(TelegramObject):

def __init__(self,
file_id,
width,
height,
duration,
thumb=None,
file_name=None,
mime_type=None,
file_size=None,
**kwargs):
self.file_id = file_id
self.width = width
self.height = height
self.duration = duration
self.thumb = thumb
self.file_name = file_name
self.mime_type = mime_type
Expand Down
99 changes: 99 additions & 0 deletions telegram/files/inputmediaanimation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2018
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram InputMediaAnimation."""
from telegram import InputMedia, Animation


class InputMediaAnimation(InputMedia):
"""Represents an animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent.

Attributes:
type (:obj:`str`): ``video``.
media (:obj:`str`): File to send. Pass a file_id to send a file that exists on the Telegram
servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet.
Lastly you can pass an existing :class:`telegram.Animation` object to send.
thumb (:obj:`str`): Optional. Thumbnail of the file sent. The thumbnail should be in JPEG
format and less than 200 kB in size. A thumbnail‘s width and height should not exceed
90. Ignored if the file is not uploaded using multipart/form-data. Thumbnails can’t be
reused and can be only uploaded as a new file, so you can pass
“attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data
under <file_attach_name>.
caption (:obj:`str`): Optional. Caption of the video to be sent, 0-200 characters.
parse_mode (:obj:`str`): Optional. Send Markdown or HTML, if you want Telegram apps to show
bold, italic, fixed-width text or inline URLs in the media caption.. See the constants
in :class:`telegram.ParseMode` for the available modes.
width (:obj:`int`): Optional. Animation width.
height (:obj:`int`): Optional. Animation height.
duration (:obj:`int`): Optional. Animation duration.


Args:
media (:obj:`str`): File to send. Pass a file_id to send a file that exists on the Telegram
servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet.
Lastly you can pass an existing :class:`telegram.Video` object to send.
thumb (:obj:`str`, optional): Thumbnail of the file sent. The thumbnail should be in JPEG
format and less than 200 kB in size. A thumbnail‘s width and height should not exceed
90. Ignored if the file is not uploaded using multipart/form-data. Thumbnails can’t be
reused and can be only uploaded as a new file, so you can pass
“attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data
under <file_attach_name>.
caption (:obj:`str`, optional): Caption of the video to be sent, 0-200 characters.
parse_mode (:obj:`str`, optional): Send Markdown or HTML, if you want Telegram apps to show
bold, italic, fixed-width text or inline URLs in the media caption.. See the constants
in :class:`telegram.ParseMode` for the available modes.
width (:obj:`int`, optional): Animation width.
height (:obj:`int`, optional): Animation height.
duration (:obj:`int`, optional): Animation duration.
supports_streaming (:obj:`bool`, optional): Pass True, if the uploaded video is suitable
for streaming.

Note:
When using a :class:`telegram.Animation` for the :attr:`media` attribute. It will take the
width, height and duration from that video, unless otherwise specified with the optional
arguments.
At the moment using a new file is not yet supported.
"""

# TODO: Make InputMediaPhoto, InputMediaVideo, InputMediaAnimation, send_media_group work with
# new files

def __init__(self, media, caption=None, parse_mode=None, width=None, height=None,
duration=None):
self.type = 'animation'

if isinstance(media, Animation):
self.media = media.file_id
self.width = media.width
self.height = media.height
self.duration = media.duration
elif hasattr(media, 'read'):
raise ValueError('Sending files is not supported (yet). Use file_id, url or Animation')
else:
self.media = media

if caption:
self.caption = caption
if parse_mode:
self.parse_mode = parse_mode
if width:
self.width = width
if height:
self.height = height
if duration:
self.duration = duration
1 change: 0 additions & 1 deletion telegram/files/inputmediavideo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram InputMediaPhoto."""
from telegram import InputMedia, Video, InputFile


Expand Down
Loading