Skip to content

Commit 43a30e7

Browse files
authored
Merge pull request eternnoir#338 from Kylmakalle/feature-BotAPIV3
Video Note support.
2 parents 791a183 + 3f5596d commit 43a30e7

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ To start the bot, simply open up a terminal and enter `python echo_bot.py` to ru
123123
All types are defined in types.py. They are all completely in line with the [Telegram API's definition of the types](https://core.telegram.org/bots/api#available-types), except for the Message's `from` field, which is renamed to `from_user` (because `from` is a Python reserved token). Thus, attributes such as `message_id` can be accessed directly with `message.message_id`. Note that `message.chat` can be either an instance of `User` or `GroupChat` (see [How can I distinguish a User and a GroupChat in message.chat?](#how-can-i-distinguish-a-user-and-a-groupchat-in-messagechat)).
124124

125125
The Message object also has a `content_type`attribute, which defines the type of the Message. `content_type` can be one of the following strings:
126-
`text`, `audio`, `document`, `photo`, `sticker`, `video`, `voice`, `location`, `contact`, `new_chat_member`, `left_chat_member`, `new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`, `supergroup_chat_created`, `channel_chat_created`, `migrate_to_chat_id`, `migrate_from_chat_id`, `pinned_message`.
126+
`text`, `audio`, `document`, `photo`, `sticker`, `video`, `video_note`, `voice`, `location`, `contact`, `new_chat_member`, `left_chat_member`, `new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`, `supergroup_chat_created`, `channel_chat_created`, `migrate_to_chat_id`, `migrate_from_chat_id`, `pinned_message`.
127127

128128
### Methods
129129

@@ -284,6 +284,11 @@ video = open('/tmp/video.mp4', 'rb')
284284
tb.send_video(chat_id, video)
285285
tb.send_video(chat_id, "FILEID")
286286

287+
# sendVideoNote
288+
videonote = open('/tmp/videonote.mp4', 'rb')
289+
tb.send_video(chat_id, videonote)
290+
tb.send_video(chat_id, "FILEID")
291+
287292
# sendLocation
288293
tb.send_location(chat_id, lat, lon)
289294

telebot/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TeleBot:
3939
sendDocument
4040
sendSticker
4141
sendVideo
42+
sendVideoNote
4243
sendLocation
4344
sendChatAction
4445
getUserProfilePhotos
@@ -510,6 +511,22 @@ def send_video(self, chat_id, data, duration=None, caption=None, reply_to_messag
510511
apihelper.send_video(self.token, chat_id, data, duration, caption, reply_to_message_id, reply_markup,
511512
disable_notification, timeout))
512513

514+
def send_video_note(self, chat_id, data, duration=None, length=None, reply_to_message_id=None, reply_markup=None,
515+
disable_notification=None, timeout=None):
516+
"""
517+
Use this method to send video files, Telegram clients support mp4 videos.
518+
:param chat_id: Integer : Unique identifier for the message recipient — User or GroupChat id
519+
:param data: InputFile or String : Video note to send. You can either pass a file_id as String to resend a video that is already on the Telegram server
520+
:param duration: Integer : Duration of sent video in seconds
521+
:param length: Integer : Video width and height, Can't be None and should be in range of (0, 640)
522+
:param reply_to_message_id:
523+
:param reply_markup:
524+
:return:
525+
"""
526+
return types.Message.de_json(
527+
apihelper.send_video_note(self.token, chat_id, data, duration, length, reply_to_message_id, reply_markup,
528+
disable_notification, timeout))
529+
513530
def send_location(self, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None,
514531
disable_notification=None):
515532
"""
@@ -559,7 +576,7 @@ def send_chat_action(self, chat_id, action):
559576
its typing status).
560577
:param chat_id:
561578
:param action: One of the following strings: 'typing', 'upload_photo', 'record_video', 'upload_video',
562-
'record_audio', 'upload_audio', 'upload_document', 'find_location'.
579+
'record_audio', 'upload_audio', 'upload_document', 'find_location', 'record_video_note', 'upload_video_note'.
563580
:return: API reply. :type: boolean
564581
"""
565582
return apihelper.send_chat_action(self.token, chat_id, action)
@@ -953,6 +970,10 @@ def send_sticker(self, *args, **kwargs):
953970
def send_video(self, *args, **kwargs):
954971
return TeleBot.send_video(self, *args, **kwargs)
955972

973+
@util.async()
974+
def send_video_note(self, *args, **kwargs):
975+
return TeleBot.send_video_note(self, *args, **kwargs)
976+
956977
@util.async()
957978
def send_location(self, *args, **kwargs):
958979
return TeleBot.send_location(self, *args, **kwargs)

telebot/apihelper.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,32 @@ def send_voice(token, chat_id, voice, caption=None, duration=None, reply_to_mess
328328
return _make_request(token, method_url, params=payload, files=files, method='post')
329329

330330

331+
def send_video_note(token, chat_id, data, duration=None, length=None, reply_to_message_id=None, reply_markup=None,
332+
disable_notification=None, timeout=None):
333+
method_url = r'sendVideoNote'
334+
payload = {'chat_id': chat_id}
335+
files = None
336+
if not util.is_string(data):
337+
files = {'video_note': data}
338+
else:
339+
payload['video_note'] = data
340+
if duration:
341+
payload['duration'] = duration
342+
if length:
343+
payload['length'] = length
344+
else:
345+
payload['length'] = 639 # seems like it is MAX length size
346+
if reply_to_message_id:
347+
payload['reply_to_message_id'] = reply_to_message_id
348+
if reply_markup:
349+
payload['reply_markup'] = _convert_markup(reply_markup)
350+
if disable_notification:
351+
payload['disable_notification'] = disable_notification
352+
if timeout:
353+
payload['connect-timeout'] = timeout
354+
return _make_request(token, method_url, params=payload, files=files, method='post')
355+
356+
331357
def send_audio(token, chat_id, audio, caption=None, duration=None, performer=None, title=None, reply_to_message_id=None,
332358
reply_markup=None, disable_notification=None, timeout=None):
333359
method_url = r'sendAudio'
@@ -525,7 +551,6 @@ def answer_callback_query(token, callback_query_id, text=None, show_alert=None,
525551
"""
526552
Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned.
527553
Alternatively, the user can be redirected to the specified Game URL. For this option to work, you must first create a game for your bot via BotFather and accept the terms. Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter.
528-
529554
:param token: Bot's token (you don't need to fill this)
530555
:param callback_query_id: Unique identifier for the query to be answered
531556
:param text: (Optional) Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters

telebot/types.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ def de_json(cls, json_string):
263263
if 'video' in obj:
264264
opts['video'] = Video.de_json(obj['video'])
265265
content_type = 'video'
266+
if 'video_note' in obj:
267+
opts['video_note'] = VideoNote.de_json(obj['video_note'])
268+
content_type = 'video_note'
266269
if 'voice' in obj:
267270
opts['voice'] = Audio.de_json(obj['voice'])
268271
content_type = 'voice'
@@ -280,6 +283,9 @@ def de_json(cls, json_string):
280283
if 'new_chat_member' in obj:
281284
opts['new_chat_member'] = User.de_json(obj['new_chat_member'])
282285
content_type = 'new_chat_member'
286+
if 'new_chat_members' in obj:
287+
opts['new_chat_members'] = obj['new_chat_members']
288+
content_type = 'new_chat_members'
283289
if 'left_chat_member' in obj:
284290
opts['left_chat_member'] = User.de_json(obj['left_chat_member'])
285291
content_type = 'left_chat_member'
@@ -342,6 +348,7 @@ def __init__(self, message_id, from_user, date, chat, content_type, options):
342348
self.photo = None
343349
self.sticker = None
344350
self.video = None
351+
self.video_note = None
345352
self.voice = None
346353
self.caption = None
347354
self.contact = None
@@ -507,6 +514,27 @@ def __init__(self, file_id, width, height, duration, thumb=None, mime_type=None,
507514
self.file_size = file_size
508515

509516

517+
class VideoNote(JsonDeserializable):
518+
@classmethod
519+
def de_json(cls, json_string):
520+
obj = cls.check_json(json_string)
521+
file_id = obj['file_id']
522+
length = obj['length']
523+
duration = obj['duration']
524+
thumb = None
525+
if 'thumb' in obj:
526+
thumb = PhotoSize.de_json(obj['thumb'])
527+
file_size = obj.get('file_size')
528+
return cls(file_id, length, duration, thumb, file_size)
529+
530+
def __init__(self, file_id, length, duration, thumb=None, file_size=None):
531+
self.file_id = file_id
532+
self.length = length
533+
self.duration = duration
534+
self.thumb = thumb
535+
self.file_size = file_size
536+
537+
510538
class Contact(JsonDeserializable):
511539
@classmethod
512540
def de_json(cls, json_string):

0 commit comments

Comments
 (0)