Skip to content

Commit ae33d33

Browse files
authored
Merge pull request python-telegram-bot#625 from python-telegram-bot/videonote
Add VideoNote
2 parents 706f79f + a5bfc52 commit ae33d33

File tree

7 files changed

+371
-23
lines changed

7 files changed

+371
-23
lines changed

telegram/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
from .inputcontactmessagecontent import InputContactMessageContent
8585
from .webhookinfo import WebhookInfo
8686
from .gamehighscore import GameHighScore
87+
from .videonote import VideoNote
8788
from .update import Update
8889
from .bot import Bot
8990
from .constants import (MAX_MESSAGE_LENGTH, MAX_CAPTION_LENGTH, SUPPORTED_WEBHOOK_PORTS,
@@ -113,5 +114,5 @@
113114
'Video', 'Voice', 'MAX_MESSAGE_LENGTH', 'MAX_CAPTION_LENGTH', 'SUPPORTED_WEBHOOK_PORTS',
114115
'MAX_FILESIZE_DOWNLOAD', 'MAX_FILESIZE_UPLOAD', 'MAX_MESSAGES_PER_SECOND_PER_CHAT',
115116
'MAX_MESSAGES_PER_SECOND', 'MAX_MESSAGES_PER_MINUTE_PER_GROUP', 'WebhookInfo', 'Animation',
116-
'Game', 'GameHighScore'
117+
'Game', 'GameHighScore', 'VideoNote'
117118
]

telegram/bot.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,66 @@ def send_voice(self,
671671
timeout=timeout,
672672
**kwargs)
673673

674+
@log
675+
def send_video_note(self,
676+
chat_id,
677+
video_note,
678+
duration=None,
679+
length=None,
680+
disable_notification=False,
681+
reply_to_message_id=None,
682+
reply_markup=None,
683+
timeout=20.,
684+
**kwargs):
685+
"""As of v.4.0, Telegram clients support rounded square mp4 videos of up to 1 minute
686+
long. Use this method to send video messages
687+
688+
Args:
689+
chat_id (int|str): Unique identifier for the message recipient - Chat id.
690+
video_note (InputFile|str): Video note to send. Pass a file_id as String to send a
691+
video note that exists on the Telegram servers (recommended) or upload a new video.
692+
Sending video notes by a URL is currently unsupported
693+
duration (Optional[int]): Duration of sent audio in seconds.
694+
length (Optional[int]): Video width and height
695+
disable_notification (Optional[bool]): Sends the message silently. iOS users will not
696+
receive a notification, Android users will receive a notification with no sound.
697+
reply_to_message_id (Optional[int]): If the message is a reply, ID of the original
698+
message.
699+
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional interface options. A
700+
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
701+
to remove reply keyboard or to force a reply from the user.
702+
timeout (Optional[int|float]): Send file timeout (default: 20 seconds).
703+
**kwargs (dict): Arbitrary keyword arguments.
704+
705+
Returns:
706+
:class:`telegram.Message`: On success, instance representing the message posted.
707+
708+
Raises:
709+
:class:`telegram.TelegramError`
710+
711+
"""
712+
url = '{0}/sendVideoNote'.format(self.base_url)
713+
714+
data = {'chat_id': chat_id, 'video_note': video_note}
715+
716+
if duration is not None:
717+
data['duration'] = duration
718+
if length is not None:
719+
data['length'] = length
720+
721+
return self._message_wrapper(
722+
url,
723+
data,
724+
chat_id=chat_id,
725+
video_note=video_note,
726+
duration=duration,
727+
length=length,
728+
disable_notification=disable_notification,
729+
reply_to_message_id=reply_to_message_id,
730+
reply_markup=reply_markup,
731+
timeout=timeout,
732+
**kwargs)
733+
674734
@log
675735
@message
676736
def send_location(self,
@@ -1787,6 +1847,7 @@ def __reduce__(self):
17871847
sendSticker = send_sticker
17881848
sendVideo = send_video
17891849
sendVoice = send_voice
1850+
sendVideoNote = send_video_note
17901851
sendLocation = send_location
17911852
sendVenue = send_venue
17921853
sendContact = send_contact

telegram/chataction.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ class ChatAction(object):
3131
UPLOAD_AUDIO = 'upload_audio'
3232
UPLOAD_DOCUMENT = 'upload_document'
3333
FIND_LOCATION = 'find_location'
34+
RECORD_VIDEO_NOTE = 'record_video_note'
35+
UPLOAD_VIDEO_NOTE = 'upload_video_note'

telegram/inputfile.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535

3636
DEFAULT_MIME_TYPE = 'application/octet-stream'
3737
USER_AGENT = 'Python Telegram Bot (https://github.com/python-telegram-bot/python-telegram-bot)'
38-
FILE_TYPES = ('audio', 'document', 'photo', 'sticker', 'video', 'voice', 'certificate')
38+
FILE_TYPES = ('audio', 'document', 'photo', 'sticker', 'video', 'voice', 'certificate',
39+
'video_note')
3940

4041

4142
class InputFile(object):
@@ -45,27 +46,11 @@ def __init__(self, data):
4546
self.data = data
4647
self.boundary = choose_boundary()
4748

48-
if 'audio' in data:
49-
self.input_name = 'audio'
50-
self.input_file = data.pop('audio')
51-
elif 'document' in data:
52-
self.input_name = 'document'
53-
self.input_file = data.pop('document')
54-
elif 'photo' in data:
55-
self.input_name = 'photo'
56-
self.input_file = data.pop('photo')
57-
elif 'sticker' in data:
58-
self.input_name = 'sticker'
59-
self.input_file = data.pop('sticker')
60-
elif 'video' in data:
61-
self.input_name = 'video'
62-
self.input_file = data.pop('video')
63-
elif 'voice' in data:
64-
self.input_name = 'voice'
65-
self.input_file = data.pop('voice')
66-
elif 'certificate' in data:
67-
self.input_name = 'certificate'
68-
self.input_file = data.pop('certificate')
49+
for t in FILE_TYPES:
50+
if t in data:
51+
self.input_name = t
52+
self.input_file = data.pop(t)
53+
break
6954
else:
7055
raise TelegramError('Unknown inputfile type')
7156

telegram/message.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
User, Video, Voice, Venue, MessageEntity, Game)
2727
from telegram.utils.deprecate import warn_deprecate_obj
2828
from telegram.utils.helpers import escape_html, escape_markdown
29+
from telegram.videonote import VideoNote
2930

3031

3132
class Message(TelegramObject):
@@ -52,6 +53,8 @@ class Message(TelegramObject):
5253
sticker (:class:`telegram.Sticker`):
5354
video (:class:`telegram.Video`):
5455
voice (:class:`telegram.Voice`):
56+
video_note (:class:`telegram.VideoNote`): Message is a video note, information about the
57+
video message
5558
caption (str):
5659
contact (:class:`telegram.Contact`):
5760
location (:class:`telegram.Location`):
@@ -92,6 +95,7 @@ class Message(TelegramObject):
9295
sticker (Optional[:class:`telegram.Sticker`]):
9396
video (Optional[:class:`telegram.Video`]):
9497
voice (Optional[:class:`telegram.Voice`]):
98+
video_note (Optional[:class:`telegram.VideoNote`]):
9599
caption (Optional[str]):
96100
contact (Optional[:class:`telegram.Contact`]):
97101
location (Optional[:class:`telegram.Location`]):
@@ -144,6 +148,7 @@ def __init__(self,
144148
pinned_message=None,
145149
forward_from_message_id=None,
146150
bot=None,
151+
video_note=None,
147152
**kwargs):
148153
# Required
149154
self.message_id = int(message_id)
@@ -165,6 +170,7 @@ def __init__(self,
165170
self.sticker = sticker
166171
self.video = video
167172
self.voice = voice
173+
self.video_note = video_note
168174
self.caption = caption
169175
self.contact = contact
170176
self.location = location
@@ -223,6 +229,7 @@ def de_json(data, bot):
223229
data['sticker'] = Sticker.de_json(data.get('sticker'), bot)
224230
data['video'] = Video.de_json(data.get('video'), bot)
225231
data['voice'] = Voice.de_json(data.get('voice'), bot)
232+
data['video_note'] = VideoNote.de_json(data.get('video_note'), bot)
226233
data['contact'] = Contact.de_json(data.get('contact'), bot)
227234
data['location'] = Location.de_json(data.get('location'), bot)
228235
data['venue'] = Venue.de_json(data.get('venue'), bot)
@@ -415,6 +422,23 @@ def reply_video(self, *args, **kwargs):
415422
self._quote(kwargs)
416423
return self.bot.sendVideo(self.chat_id, *args, **kwargs)
417424

425+
def reply_video_note(self, *args, **kwargs):
426+
"""
427+
Shortcut for ``bot.send_video_note(update.message.chat_id, *args, **kwargs)``
428+
429+
Keyword Args:
430+
quote (Optional[bool]): If set to ``True``, the video is sent as an actual reply to
431+
this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter
432+
will be ignored. Default: ``True`` in group chats and ``False`` in private chats.
433+
434+
Returns:
435+
:class:`telegram.Message`: On success, instance representing the message posted.
436+
437+
"""
438+
439+
self._quote(kwargs)
440+
return self.bot.send_video_note(self.chat_id, *args, **kwargs)
441+
418442
def reply_voice(self, *args, **kwargs):
419443
"""
420444
Shortcut for ``bot.sendVoice(update.message.chat_id, *args, **kwargs)``

telegram/videonote.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015-2017
5+
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser Public License
18+
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
"""This module contains an object that represents a Telegram VideoNote."""
20+
21+
from telegram import PhotoSize, TelegramObject
22+
23+
24+
class VideoNote(TelegramObject):
25+
"""This object represents a Telegram VideoNote.
26+
27+
Attributes:
28+
file_id (str): Unique identifier for this file
29+
length (int): Video width and height as defined by sender
30+
duration (int): Duration of the video in seconds as defined by sender
31+
thumb (Optional[:class:`telegram.PhotoSize`]): Video thumbnail
32+
file_size (Optional[int]): File size
33+
"""
34+
35+
def __init__(self, file_id, length, duration, thumb=None, file_size=None, **kwargs):
36+
# Required
37+
self.file_id = str(file_id)
38+
self.length = int(length)
39+
self.duration = int(duration)
40+
# Optionals
41+
self.thumb = thumb
42+
self.file_size = file_size
43+
44+
self._id_attrs = (self.file_id,)
45+
46+
@staticmethod
47+
def de_json(data, bot):
48+
"""
49+
Args:
50+
data (dict):
51+
bot (telegram.Bot):
52+
53+
Returns:
54+
telegram.VideoNote:
55+
"""
56+
if not data:
57+
return None
58+
59+
data = super(VideoNote, VideoNote).de_json(data, bot)
60+
61+
data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
62+
63+
return VideoNote(**data)

0 commit comments

Comments
 (0)