Skip to content

Commit 5963649

Browse files
committed
Adding sendVideo and minor fixes
1 parent c3ea91e commit 5963649

File tree

7 files changed

+117
-7
lines changed

7 files changed

+117
-7
lines changed

telegram/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from audio import Audio
1616
from document import Document
1717
from sticker import Sticker
18-
# from video import Video
18+
from video import Video
1919
# from contact import Contact
2020
# from location import Location
2121
# from inputfile import InputFile

telegram/bot.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,24 @@ def sendSticker(self,
239239
sticker,
240240
reply_to_message_id=None,
241241
reply_markup=None):
242+
"""Use this method to send .webp stickers.
243+
244+
Args:
245+
chat_id:
246+
Unique identifier for the message recipient — User or GroupChat id.
247+
sticker:
248+
Sticker to send. You can either pass a file_id as String to resend
249+
a sticker that is already on the Telegram servers, or upload a new
250+
sticker using multipart/form-data.
251+
reply_to_message_id:
252+
If the message is a reply, ID of the original message. [Optional]
253+
reply_markup:
254+
Additional interface options. A JSON-serialized object for a
255+
custom reply keyboard, instructions to hide keyboard or to force a
256+
reply from the user. [Optional]
257+
Returns:
258+
A telegram.Message instance representing the message posted.
259+
"""
242260

243261
url = '%s/sendSticker' % (self.base_url)
244262

@@ -255,9 +273,46 @@ def sendSticker(self,
255273

256274
return Message.newFromJsonDict(data)
257275

258-
def sendVideo(self):
276+
def sendVideo(self,
277+
chat_id,
278+
video,
279+
reply_to_message_id=None,
280+
reply_markup=None):
281+
"""Use this method to send video files, Telegram clients support mp4
282+
videos (other formats may be sent as telegram.Document).
283+
284+
Args:
285+
chat_id:
286+
Unique identifier for the message recipient — User or GroupChat id.
287+
video:
288+
Video to send. You can either pass a file_id as String to resend a
289+
video that is already on the Telegram servers, or upload a new
290+
video file using multipart/form-data.
291+
reply_to_message_id:
292+
If the message is a reply, ID of the original message. [Optional]
293+
reply_markup:
294+
Additional interface options. A JSON-serialized object for a
295+
custom reply keyboard, instructions to hide keyboard or to force a
296+
reply from the user. [Optional]
297+
Returns:
298+
A telegram.Message instance representing the message posted.
299+
"""
300+
259301
url = '%s/sendVideo' % (self.base_url)
260302

303+
data = {'chat_id': chat_id,
304+
'video': video}
305+
306+
if reply_to_message_id:
307+
data['reply_to_message_id'] = reply_to_message_id
308+
if reply_markup:
309+
data['reply_markup'] = reply_markup
310+
311+
json_data = self._requestUrl(url, 'POST', data=data)
312+
data = self._parseAndCheckTelegram(json_data.content)
313+
314+
return Message.newFromJsonDict(data)
315+
261316
def sendLocation(self):
262317
url = '%s/sendLocation' % (self.base_url)
263318

telegram/groupchat.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def __init__(self, **kwargs):
1111
for (param, default) in param_defaults.iteritems():
1212
setattr(self, param, kwargs.get(param, default))
1313

14+
@property
15+
def chat_id(self):
16+
return self.id
17+
1418
@staticmethod
1519
def newFromJsonDict(data):
1620
return GroupChat(id=data.get('id', None),

telegram/message.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ def newFromJsonDict(data):
8585
else:
8686
sticker = None
8787

88+
if 'video' in data:
89+
from telegram import Video
90+
video = Video.newFromJsonDict(data['video'])
91+
else:
92+
video = None
93+
8894
if 'new_chat_participant' in data:
8995
from telegram import User
9096
new_chat_participant = User.newFromJsonDict(
@@ -113,7 +119,7 @@ def newFromJsonDict(data):
113119
document=document,
114120
photo=photo,
115121
sticker=sticker,
116-
video=data.get('video', None),
122+
video=video,
117123
contact=data.get('contact', None),
118124
location=data.get('location', None),
119125
new_chat_participant=new_chat_participant,

telegram/user.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class User(object):
55
def __init__(self, **kwargs):
66
param_defaults = {
77
'id': None,
8-
# 'chat_id' TODO
98
'first_name': None,
109
'last_name': None,
1110
'username': None
@@ -14,6 +13,10 @@ def __init__(self, **kwargs):
1413
for (param, default) in param_defaults.iteritems():
1514
setattr(self, param, kwargs.get(param, default))
1615

16+
@property
17+
def chat_id(self):
18+
return self.id
19+
1720
@staticmethod
1821
def newFromJsonDict(data):
1922
return User(id=data.get('id', None),

telegram/video.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
3+
4+
class Video(object):
5+
def __init__(self, **kwargs):
6+
param_defaults = {
7+
'file_id': None,
8+
'width': None,
9+
'height': None,
10+
'duration': None,
11+
'thumb': None,
12+
'mime_type': None,
13+
'file_size': None,
14+
'caption': None
15+
}
16+
17+
for (param, default) in param_defaults.iteritems():
18+
setattr(self, param, kwargs.get(param, default))
19+
20+
@staticmethod
21+
def newFromJsonDict(data):
22+
if 'thumb' in data:
23+
from telegram import PhotoSize
24+
thumb = PhotoSize.newFromJsonDict(data['thumb'])
25+
else:
26+
thumb = None
27+
28+
return Video(file_id=data.get('file_id', None),
29+
width=data.get('width', None),
30+
height=data.get('height', None),
31+
duration=data.get('duration', None),
32+
thumb=thumb,
33+
mime_type=data.get('mime_type', None),
34+
file_size=data.get('file_size', None),
35+
caption=data.get('caption', None))

tests/test_bot.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def testGetUpdates(self):
3333
'''Test the telegram.Bot getUpdates method'''
3434
print 'Testing getUpdates'
3535
updates = self._bot.getUpdates()
36-
self.assertEqual(129566481, updates[0].update_id)
36+
self.assertEqual(129566520, updates[0].update_id)
3737

3838
def testForwardMessage(self):
3939
'''Test the telegram.Bot forwardMessage method'''
@@ -86,9 +86,16 @@ def testResendDocument(self):
8686
chat_id=12173560)
8787
self.assertEqual(u'BQADAQADHAADNTwtBxZxUGKyxYbYAg', message.document.file_id)
8888

89-
def testSendSticker(self):
89+
def testResendSticker(self):
9090
'''Test the telegram.Bot sendSticker method'''
91-
print 'Testing sendSticket'
91+
print 'Testing sendSticker - Resend'
9292
message = self._bot.sendSticker(sticker=str('BQADAQADHAADyIsGAAFZfq1bphjqlgI'),
9393
chat_id=12173560)
9494
self.assertEqual(39518, message.sticker.file_size)
95+
96+
def testResendVideo(self):
97+
'''Test the telegram.Bot sendVideo method'''
98+
print 'Testing sendVideo - Resend'
99+
message = self._bot.sendVideo(video=str('BAADAQADIgEAAvjAuQABOuTB937fPTgC'),
100+
chat_id=12173560)
101+
self.assertEqual(4, message.video.duration)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy