Skip to content

Commit fc61827

Browse files
committed
bot.py: allow specifying timeout for sendVideo operations
1 parent d2623d2 commit fc61827

2 files changed

Lines changed: 52 additions & 27 deletions

File tree

telegram/bot.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,28 +142,38 @@ def decorator(self, *args, **kwargs):
142142
decorator
143143
"""
144144
url, data = func(self, *args, **kwargs)
145+
return Bot._post_message(url, data, kwargs)
146+
return decorator
145147

146-
if not data.get('chat_id'):
147-
raise TelegramError('Invalid chat_id')
148+
@staticmethod
149+
def _post_message(url, data, kwargs, timeout=None, network_delay=2.):
150+
"""Posts a message to the telegram servers.
148151
149-
if kwargs.get('reply_to_message_id'):
150-
reply_to_message_id = kwargs.get('reply_to_message_id')
151-
data['reply_to_message_id'] = reply_to_message_id
152+
Returns:
153+
telegram.Message
152154
153-
if kwargs.get('reply_markup'):
154-
reply_markup = kwargs.get('reply_markup')
155-
if isinstance(reply_markup, ReplyMarkup):
156-
data['reply_markup'] = reply_markup.to_json()
157-
else:
158-
data['reply_markup'] = reply_markup
155+
"""
156+
if not data.get('chat_id'):
157+
raise TelegramError('Invalid chat_id')
159158

160-
result = request.post(url, data)
159+
if kwargs.get('reply_to_message_id'):
160+
reply_to_message_id = kwargs.get('reply_to_message_id')
161+
data['reply_to_message_id'] = reply_to_message_id
161162

162-
if result is True:
163-
return result
163+
if kwargs.get('reply_markup'):
164+
reply_markup = kwargs.get('reply_markup')
165+
if isinstance(reply_markup, ReplyMarkup):
166+
data['reply_markup'] = reply_markup.to_json()
167+
else:
168+
data['reply_markup'] = reply_markup
164169

165-
return Message.de_json(result)
166-
return decorator
170+
result = request.post(url, data, timeout=timeout,
171+
network_delay=network_delay)
172+
173+
if result is True:
174+
return result
175+
176+
return Message.de_json(result)
167177

168178
@log
169179
def getMe(self):
@@ -430,12 +440,12 @@ def sendSticker(self,
430440
return url, data
431441

432442
@log
433-
@message
434443
def sendVideo(self,
435444
chat_id,
436445
video,
437446
duration=None,
438447
caption=None,
448+
timeout=None,
439449
**kwargs):
440450
"""Use this method to send video files, Telegram clients support mp4
441451
videos (other formats may be sent as telegram.Document).
@@ -452,6 +462,9 @@ def sendVideo(self,
452462
caption:
453463
Video caption (may also be used when resending videos by file_id).
454464
[Optional]
465+
timeout:
466+
float. If this value is specified, use it as the definitive timeout
467+
(in seconds) for urlopen() operations. [Optional]
455468
reply_to_message_id:
456469
If the message is a reply, ID of the original message. [Optional]
457470
reply_markup:
@@ -473,7 +486,7 @@ def sendVideo(self,
473486
if caption:
474487
data['caption'] = caption
475488

476-
return url, data
489+
return self._post_message(url, data, kwargs, timeout=timeout)
477490

478491
@log
479492
@message

telegram/utils/request.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,37 @@ def get(url):
127127
@_try_except_req
128128
def post(url,
129129
data,
130+
timeout=None,
130131
network_delay=2.):
131132
"""Request an URL.
132133
Args:
133134
url:
134135
The web location we want to retrieve.
135136
data:
136137
A dict of (str, unicode) key/value pairs.
138+
timeout:
139+
float. If this value is specified, use it as the definitive timeout (in
140+
seconds) for urlopen() operations. [Optional]
137141
network_delay:
138-
Additional timeout in seconds to allow the response from Telegram to
139-
take some time.
142+
float. If using the timeout specified in `data` (which is a timeout for
143+
the Telegram servers operation), then `network_delay` as an extra delay
144+
(in seconds) to compensate for network latency.
145+
default: 2 [Optional]
146+
147+
Notes:
148+
If neither `timeout` nor `data['timeout']` is specified. The underlying
149+
defaults are used.
140150
141151
Returns:
142152
A JSON object.
153+
143154
"""
155+
urlopen_kwargs = {}
144156

145-
# Add time to the timeout of urlopen to allow data to be transferred over
146-
# the network.
147-
if 'timeout' in data:
148-
timeout = data['timeout'] + network_delay
149-
else:
150-
timeout = None
157+
if timeout is not None:
158+
urlopen_kwargs['timeout'] = timeout
159+
elif 'timeout' in data:
160+
urlopen_kwargs['timeout'] = data['timeout'] + network_delay
151161

152162
if InputFile.is_inputfile(data):
153163
data = InputFile(data)
@@ -160,7 +170,9 @@ def post(url,
160170
data=data.encode(),
161171
headers={'Content-Type': 'application/json'})
162172

163-
result = urlopen(request, timeout=timeout).read()
173+
with urlopen(request, **urlopen_kwargs) as handle:
174+
result = handle.read()
175+
164176
return _parse(result)
165177

166178

0 commit comments

Comments
 (0)