Skip to content

Commit d271711

Browse files
authored
Support for animations (#129)
* Add _get_file_args function in ChatMixin (cherry picked from commit 54b8b6c) * Better syntax for _get_file_args() (cherry picked from commit e344f4c) * Fix PEP-8 (cherry picked from commit 79530ec) * Support for animations / new methods send_gif and reply_with_gif * Add Animation in objects/__init__.py * Add documentation and changelog * Fix indentation error * Fix (stupid) import error
1 parent 1e6c615 commit d271711

File tree

6 files changed

+238
-4
lines changed

6 files changed

+238
-4
lines changed

botogram/objects/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222

2323
from .chats import User, Chat, UserProfilePhotos, Permissions
2424
from .media import PhotoSize, Photo, Audio, Voice, Document, Sticker, \
25-
Video, VideoNote, Contact, Location, Venue
25+
Video, VideoNote, Animation, Contact, Location, Venue
2626
from .messages import Message
2727
from .markup import ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply
2828
from .polls import Poll, PollOption
2929
from .updates import Update, Updates
30-
from .mixins import Album
30+
from .mixins import Album
3131

3232
__all__ = [
3333
# Chats-related objects
@@ -44,6 +44,7 @@
4444
"Sticker",
4545
"Video",
4646
"VideoNote",
47+
"Animation",
4748
"Contact",
4849
"Location",
4950
"Venue",

botogram/objects/media.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,27 @@ class Video(BaseObject, mixins.FileMixin):
187187
_check_equality_ = "file_id"
188188

189189

190+
class Animation(BaseObject, mixins.FileMixin):
191+
"""Telegram API representation of an animation
192+
193+
https://core.telegram.org/bots/api#animation
194+
"""
195+
196+
required = {
197+
"file_id": str,
198+
"width": int,
199+
"height": int,
200+
"duration": int,
201+
}
202+
optional = {
203+
"thumb": PhotoSize,
204+
"file_name": str,
205+
"mime_type": str,
206+
"file_size": int,
207+
}
208+
_check_equality_ = "file_id"
209+
210+
190211
class Contact(BaseObject):
191212
"""Telegram API representation of a contact
192213

botogram/objects/messages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .. import utils
2626
from .chats import User, Chat
2727
from .media import Audio, Voice, Document, Photo, Sticker, Video, VideoNote, \
28-
Contact, Location, Venue
28+
Animation, Contact, Location, Venue
2929
from .polls import Poll
3030

3131

@@ -347,6 +347,7 @@ def from_(self):
347347
"sticker": Sticker,
348348
"video": Video,
349349
"video_note": VideoNote,
350+
"animation": Animation,
350351
"caption": str,
351352
"contact": Contact,
352353
"location": Location,

botogram/objects/mixins.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,37 @@ def send_video_note(self, path=None, file_id=None, duration=None,
241241
return self._api.call("sendVideoNote", args, files,
242242
expect=_objects().Message)
243243

244+
@_require_api
245+
def send_gif(self, path=None, file_id=None, url=None, duration=None,
246+
width=None, height=None, caption=None, thumb=None,
247+
reply_to=None, extra=None, attach=None,
248+
notify=True, syntax=None):
249+
"""Send an animation"""
250+
args = self._get_call_args(reply_to, extra, attach, notify)
251+
if duration is not None:
252+
args["duration"] = duration
253+
if caption is not None:
254+
args["caption"] = caption
255+
if syntax is not None:
256+
syntax = syntaxes.guess_syntax(caption, syntax)
257+
args["parse_mode"] = syntax
258+
if width is not None:
259+
args["width"] = width
260+
if height is not None:
261+
args["height"] = height
262+
263+
files = dict()
264+
args["animation"], files["animation"] = self._get_file_args(path,
265+
file_id,
266+
url)
267+
if files["animation"] is None:
268+
del files["animation"]
269+
if thumb is not None:
270+
files["thumb"] = thumb
271+
272+
return self._api.call("sendAnimation", args, files,
273+
expect=_objects().Message)
274+
244275
@_require_api
245276
def send_file(self, path=None, file_id=None, url=None, thumb=None,
246277
reply_to=None, extra=None, attach=None,
@@ -523,6 +554,10 @@ def reply_with_video_note(self, *args, **kwargs):
523554
"""Reply with a video note to the current message"""
524555
return self.chat.send_video_note(*args, reply_to=self, **kwargs)
525556

557+
@_require_api
558+
def reply_with_gif(self, *args, **kwargs):
559+
return self.chat.send_gif(*args, reply_to=self, **kwargs)
560+
526561
@_require_api
527562
def reply_with_file(self, *args, **kwargs):
528563
"""Reply with a generic file to the current chat"""

docs/api/telegram.rst

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,41 @@ about its business.
363363

364364
.. versionadded:: 0.6
365365

366+
.. py:method:: send_gif([path=None, file_id=None, duration=None, width=None, height=None, caption=None, thumb=None, reply_to=None, attach=None, extra=None, notify=True])
367+
368+
Send an animation to the user. You can specify the animation by passing its *path*,
369+
its *url*, or its Telegram *file_id*. Only one of these arguments must be passed.
370+
371+
You may optionally specify the *duration*, the *width* and the *height* of the GIF.
372+
If the GIF track you're sending is in reply to another message,
373+
set *reply_to* to the ID of the other :py:class:`~botogram.Message`.
374+
375+
The *attach* parameter allows you to attach extra things like
376+
:ref:`buttons <buttons>` to the message.
377+
378+
The *notify* parameter is for defining if your message should trigger
379+
a notification on the client side (yes by default).
380+
381+
:param str path: The path to the animation
382+
:param str file_id: The Telegram *file_id* of the animation
383+
:param int duration: The animation duration, in seconds
384+
:param str width: The animation width, in pixels
385+
:param str height: The animation height, in pixels
386+
:param str caption: The caption of the video
387+
:param str thumb: The path to the thumb
388+
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
389+
:param object attach: An extra thing to attach to the message.
390+
:param object extra: An extra reply interface object to attach
391+
:param bool notify: If you want to trigger the client notification.
392+
:returns: The message you sent
393+
:rtype: ~botogram.Message
394+
395+
.. deprecated:: 0.4
396+
397+
The *extra* parameter is now deprecated
398+
399+
.. versionadded:: 0.7
400+
366401
.. py:method:: send_file([path=None, file_id=None, url=None, thumb=None, reply_to=None, attach=None, extra=None, notify=True, caption=None, syntax=None])
367402
368403
Send a generic file to the user. You can specify the file by passing its *path*,
@@ -1210,6 +1245,41 @@ about its business.
12101245

12111246
.. versionadded:: 0.6
12121247

1248+
.. py:method:: send_gif([path=None, file_id=None, duration=None, width=None, height=None, caption=None, thumb=None, reply_to=None, attach=None, extra=None, notify=True])
1249+
1250+
Send an animation to the user. You can specify the animation by passing its *path*,
1251+
its *url*, or its Telegram *file_id*. Only one of these arguments must be passed.
1252+
1253+
You may optionally specify the *duration*, the *width* and the *height* of the GIF.
1254+
If the GIF track you're sending is in reply to another message,
1255+
set *reply_to* to the ID of the other :py:class:`~botogram.Message`.
1256+
1257+
The *attach* parameter allows you to attach extra things like
1258+
:ref:`buttons <buttons>` to the message.
1259+
1260+
The *notify* parameter is for defining if your message should trigger
1261+
a notification on the client side (yes by default).
1262+
1263+
:param str path: The path to the animation
1264+
:param str file_id: The Telegram *file_id* of the animation
1265+
:param int duration: The animation duration, in seconds
1266+
:param str width: The animation width, in pixels
1267+
:param str height: The animation height, in pixels
1268+
:param str caption: The caption of the video
1269+
:param str thumb: The path to the thumb
1270+
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
1271+
:param object attach: An extra thing to attach to the message.
1272+
:param object extra: An extra reply interface object to attach
1273+
:param bool notify: If you want to trigger the client notification.
1274+
:returns: The message you sent
1275+
:rtype: ~botogram.Message
1276+
1277+
.. deprecated:: 0.4
1278+
1279+
The *extra* parameter is now deprecated
1280+
1281+
.. versionadded:: 0.7
1282+
12131283
.. py:method:: send_file([path=None, file_id=None, url=None, thumb=None, reply_to=None, attach=None, extra=None, notify=True, caption=None, syntax=None])
12141284
12151285
Send a generic file to the chat. You can specify the video by passing its *path*,
@@ -1728,6 +1798,14 @@ about its business.
17281798
file.
17291799

17301800
*This attribute can be None if it's not provided by Telegram.*
1801+
1802+
.. py:attribute:: animation
1803+
1804+
A :py:class:`~botogram.Animation~ object, for when this message is an animation
1805+
file.
1806+
1807+
*This attribute can be None if it's not provided by Telegram.*
1808+
17311809
.. py:attribute:: caption
17321810
17331811
A caption for when this message is a photo or video file.
@@ -2137,7 +2215,7 @@ about its business.
21372215

21382216
.. py:method:: reply_with_video_note([path=None, file_id=None, duration=None, length=None, thumb=None, attach=None, extra=None, notify=True])
21392217
2140-
Reply with the video note to the user. You can specify the video note by passing its *path*,
2218+
Reply with the video note to the user. You can specify the video note by passing its *path*,
21412219
or its Telegram *file_id*. Only one of these arguments must be passed.
21422220

21432221
You may optionally specify the *duration* and the *length* of the video.
@@ -2168,6 +2246,41 @@ about its business.
21682246

21692247
.. versionadded:: 0.6
21702248

2249+
.. py:method:: reply_with_gif([path=None, file_id=None, duration=None, width=None, height=None, caption=None, thumb=None, attach=None, extra=None, notify=True])
2250+
2251+
Reply with an animation to the message. You can specify the animation by passing its *path*,
2252+
its *url*, or its Telegram *file_id*. Only one of these arguments must be passed.
2253+
2254+
You may optionally specify the *duration*, the *width* and the *height* of the GIF.
2255+
If the GIF track you're sending is in reply to another message,
2256+
set *reply_to* to the ID of the other :py:class:`~botogram.Message`.
2257+
2258+
The *attach* parameter allows you to attach extra things like
2259+
:ref:`buttons <buttons>` to the message.
2260+
2261+
The *notify* parameter is for defining if your message should trigger
2262+
a notification on the client side (yes by default).
2263+
2264+
:param str path: The path to the animation
2265+
:param str file_id: The Telegram *file_id* of the animation
2266+
:param int duration: The animation duration, in seconds
2267+
:param str width: The animation width, in pixels
2268+
:param str height: The animation height, in pixels
2269+
:param str caption: The caption of the video
2270+
:param str thumb: The path to the thumb
2271+
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
2272+
:param object attach: An extra thing to attach to the message.
2273+
:param object extra: An extra reply interface object to attach
2274+
:param bool notify: If you want to trigger the client notification.
2275+
:returns: The message you sent
2276+
:rtype: ~botogram.Message
2277+
2278+
.. deprecated:: 0.4
2279+
2280+
The *extra* parameter is now deprecated
2281+
2282+
.. versionadded:: 0.7
2283+
21712284
.. py:method:: reply_with_file(path, [thumb=None, attach=None, extra=None, notify=True])
21722285
21732286
Reply with the generic file found in the *path* to the chat. If the file
@@ -2709,6 +2822,60 @@ about its business.
27092822
:param str path: The file name path locating where the video note should be saved.
27102823

27112824

2825+
.. py:class:: botogram.Animation
2826+
2827+
This class represents an animation file.
2828+
2829+
.. py:attribute:: file_id
2830+
2831+
The string ID of the file.
2832+
2833+
.. py:attribute:: length
2834+
2835+
The integer length of the animation as defined by the sender.
2836+
2837+
.. py:attribute:: width
2838+
2839+
The integer width of the video as defined by the sender.
2840+
2841+
.. py:attribute:: height
2842+
2843+
The integer height of the video as defined by the sender.
2844+
2845+
.. py:attribute:: thumb
2846+
2847+
A :py:class:`~botogram.PhotoSize` object representing a thumbnail image of
2848+
the animation as defined by the sender.
2849+
2850+
*This attribute can be None if it's not provided by Telegram.*
2851+
2852+
.. py:attribute:: file_name
2853+
2854+
The original animation filename as defined by the sender.
2855+
2856+
*This attribute can be None if it's not provided by Telegram.*
2857+
2858+
.. py:attribute:: mime_type
2859+
2860+
The MIME type of the file as defined by the sender.
2861+
2862+
*This attribute can be None if it's not provided by Telegram.*
2863+
2864+
.. py:attribute:: file_size
2865+
2866+
The integer size of the animation file
2867+
2868+
*This attribute can be None if it's not provided by Telegram.*
2869+
2870+
.. py:method:: save(path)
2871+
2872+
Save the animation to a file located by *path*. Be aware that Telegram can
2873+
not provide the name of the original file sent by its sender. This should
2874+
be generated as part of the path.
2875+
2876+
:param str path: The file name path locating where the video note should be saved.
2877+
2878+
27122879
.. py:class:: botogram.Voice
27132880
27142881
This class represents a voice message.

docs/changelog/0.7.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ Release description not yet written.
1919
New features
2020
------------
2121

22+
23+
* Added support for animations (GIFs)
24+
25+
* New :py:class:`botogram.Animation` class
26+
* New attribute :py:attr:`botogram.Message.animation`
27+
* New method :py:meth:`botogram.Chat.send_gif`
28+
* New method :py:meth:`botogram.User.send_gif`
29+
* New method :py:meth:`botogram.Message.reply_with_gif`
30+
2231
* Added support for polls
2332

2433
* New :py:class:`botogram.Poll`

0 commit comments

Comments
 (0)