mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-23 06:50:29 +01:00
Merge remote-tracking branch 'origin/master' into unittest-bot2.0
This commit is contained in:
commit
d47787876a
3 changed files with 132 additions and 40 deletions
152
telegram/bot.py
152
telegram/bot.py
|
@ -21,8 +21,8 @@
|
||||||
"""This module contains a object that represents a Telegram Bot."""
|
"""This module contains a object that represents a Telegram Bot."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import functools
|
|
||||||
|
|
||||||
|
import functools
|
||||||
from telegram import User, Message, Update, UserProfilePhotos, File, \
|
from telegram import User, Message, Update, UserProfilePhotos, File, \
|
||||||
ReplyMarkup, TelegramObject, NullHandler
|
ReplyMarkup, TelegramObject, NullHandler
|
||||||
from telegram.utils import request
|
from telegram.utils import request
|
||||||
|
@ -823,7 +823,8 @@ class Bot(TelegramObject):
|
||||||
@message
|
@message
|
||||||
def sendChatAction(self,
|
def sendChatAction(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
action):
|
action,
|
||||||
|
**kwargs):
|
||||||
"""Use this method when you need to tell the user that something is
|
"""Use this method when you need to tell the user that something is
|
||||||
happening on the bot's side. The status is set for 5 seconds or less
|
happening on the bot's side. The status is set for 5 seconds or less
|
||||||
(when a message arrives from your bot, Telegram clients clear its
|
(when a message arrives from your bot, Telegram clients clear its
|
||||||
|
@ -858,7 +859,8 @@ class Bot(TelegramObject):
|
||||||
is_personal=None,
|
is_personal=None,
|
||||||
next_offset=None,
|
next_offset=None,
|
||||||
switch_pm_text=None,
|
switch_pm_text=None,
|
||||||
switch_pm_parameter=None):
|
switch_pm_parameter=None,
|
||||||
|
**kwargs):
|
||||||
"""Use this method to send answers to an inline query. No more than
|
"""Use this method to send answers to an inline query. No more than
|
||||||
50 results per query are allowed.
|
50 results per query are allowed.
|
||||||
|
|
||||||
|
@ -884,6 +886,14 @@ class Bot(TelegramObject):
|
||||||
switch_pm_parameter (Optional[str]): Parameter for the start
|
switch_pm_parameter (Optional[str]): Parameter for the start
|
||||||
message sent to the bot when user presses the switch button.
|
message sent to the bot when user presses the switch button.
|
||||||
|
|
||||||
|
Keyword Args:
|
||||||
|
timeout (Optional[float]): If this value is specified, use it as
|
||||||
|
the definitive timeout (in seconds) for urlopen() operations.
|
||||||
|
network_delay (Optional[float]): If using the timeout (which is
|
||||||
|
a `timeout` for the Telegram servers operation),
|
||||||
|
then `network_delay` as an extra delay (in seconds) to
|
||||||
|
compensate for network latency. Defaults to 2.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: On success, `True` is returned.
|
bool: On success, `True` is returned.
|
||||||
|
|
||||||
|
@ -910,7 +920,9 @@ class Bot(TelegramObject):
|
||||||
if switch_pm_parameter:
|
if switch_pm_parameter:
|
||||||
data['switch_pm_parameter'] = switch_pm_parameter
|
data['switch_pm_parameter'] = switch_pm_parameter
|
||||||
|
|
||||||
result = request.post(url, data)
|
result = request.post(url, data,
|
||||||
|
timeout=kwargs.get('timeout'),
|
||||||
|
network_delay=kwargs.get('network_delay'))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -918,7 +930,8 @@ class Bot(TelegramObject):
|
||||||
def getUserProfilePhotos(self,
|
def getUserProfilePhotos(self,
|
||||||
user_id,
|
user_id,
|
||||||
offset=None,
|
offset=None,
|
||||||
limit=100):
|
limit=100,
|
||||||
|
**kwargs):
|
||||||
"""Use this method to get a list of profile pictures for a user.
|
"""Use this method to get a list of profile pictures for a user.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -931,6 +944,14 @@ class Bot(TelegramObject):
|
||||||
Limits the number of photos to be retrieved. Values between 1-100
|
Limits the number of photos to be retrieved. Values between 1-100
|
||||||
are accepted. Defaults to 100. [Optional]
|
are accepted. Defaults to 100. [Optional]
|
||||||
|
|
||||||
|
Keyword Args:
|
||||||
|
timeout (Optional[float]): If this value is specified, use it as
|
||||||
|
the definitive timeout (in seconds) for urlopen() operations.
|
||||||
|
network_delay (Optional[float]): If using the timeout (which is
|
||||||
|
a `timeout` for the Telegram servers operation),
|
||||||
|
then `network_delay` as an extra delay (in seconds) to
|
||||||
|
compensate for network latency. Defaults to 2.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list[:class:`telegram.UserProfilePhotos`]: A list of
|
list[:class:`telegram.UserProfilePhotos`]: A list of
|
||||||
:class:`telegram.UserProfilePhotos` objects are returned.
|
:class:`telegram.UserProfilePhotos` objects are returned.
|
||||||
|
@ -949,13 +970,16 @@ class Bot(TelegramObject):
|
||||||
if limit:
|
if limit:
|
||||||
data['limit'] = limit
|
data['limit'] = limit
|
||||||
|
|
||||||
result = request.post(url, data)
|
result = request.post(url, data,
|
||||||
|
timeout=kwargs.get('timeout'),
|
||||||
|
network_delay=kwargs.get('network_delay'))
|
||||||
|
|
||||||
return UserProfilePhotos.de_json(result)
|
return UserProfilePhotos.de_json(result)
|
||||||
|
|
||||||
@log
|
@log
|
||||||
def getFile(self,
|
def getFile(self,
|
||||||
file_id):
|
file_id,
|
||||||
|
**kwargs):
|
||||||
"""Use this method to get basic info about a file and prepare it for
|
"""Use this method to get basic info about a file and prepare it for
|
||||||
downloading. For the moment, bots can download files of up to 20MB in
|
downloading. For the moment, bots can download files of up to 20MB in
|
||||||
size.
|
size.
|
||||||
|
@ -964,6 +988,14 @@ class Bot(TelegramObject):
|
||||||
file_id:
|
file_id:
|
||||||
File identifier to get info about.
|
File identifier to get info about.
|
||||||
|
|
||||||
|
Keyword Args:
|
||||||
|
timeout (Optional[float]): If this value is specified, use it as
|
||||||
|
the definitive timeout (in seconds) for urlopen() operations.
|
||||||
|
network_delay (Optional[float]): If using the timeout (which is
|
||||||
|
a `timeout` for the Telegram servers operation),
|
||||||
|
then `network_delay` as an extra delay (in seconds) to
|
||||||
|
compensate for network latency. Defaults to 2.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:class:`telegram.File`: On success, a :class:`telegram.File`
|
:class:`telegram.File`: On success, a :class:`telegram.File`
|
||||||
object is returned.
|
object is returned.
|
||||||
|
@ -977,7 +1009,9 @@ class Bot(TelegramObject):
|
||||||
|
|
||||||
data = {'file_id': file_id}
|
data = {'file_id': file_id}
|
||||||
|
|
||||||
result = request.post(url, data)
|
result = request.post(url, data,
|
||||||
|
timeout=kwargs.get('timeout'),
|
||||||
|
network_delay=kwargs.get('network_delay'))
|
||||||
|
|
||||||
if result.get('file_path'):
|
if result.get('file_path'):
|
||||||
result['file_path'] = '%s/%s' % (self.base_file_url,
|
result['file_path'] = '%s/%s' % (self.base_file_url,
|
||||||
|
@ -988,7 +1022,8 @@ class Bot(TelegramObject):
|
||||||
@log
|
@log
|
||||||
def kickChatMember(self,
|
def kickChatMember(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
user_id):
|
user_id,
|
||||||
|
**kwargs):
|
||||||
"""Use this method to kick a user from a group or a supergroup. In the
|
"""Use this method to kick a user from a group or a supergroup. In the
|
||||||
case of supergroups, the user will not be able to return to the group
|
case of supergroups, the user will not be able to return to the group
|
||||||
on their own using invite links, etc., unless unbanned first. The bot
|
on their own using invite links, etc., unless unbanned first. The bot
|
||||||
|
@ -1001,6 +1036,14 @@ class Bot(TelegramObject):
|
||||||
user_id:
|
user_id:
|
||||||
Unique identifier of the target user.
|
Unique identifier of the target user.
|
||||||
|
|
||||||
|
Keyword Args:
|
||||||
|
timeout (Optional[float]): If this value is specified, use it as
|
||||||
|
the definitive timeout (in seconds) for urlopen() operations.
|
||||||
|
network_delay (Optional[float]): If using the timeout (which is
|
||||||
|
a `timeout` for the Telegram servers operation),
|
||||||
|
then `network_delay` as an extra delay (in seconds) to
|
||||||
|
compensate for network latency. Defaults to 2.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: On success, `True` is returned.
|
bool: On success, `True` is returned.
|
||||||
|
|
||||||
|
@ -1014,14 +1057,17 @@ class Bot(TelegramObject):
|
||||||
data = {'chat_id': chat_id,
|
data = {'chat_id': chat_id,
|
||||||
'user_id': user_id}
|
'user_id': user_id}
|
||||||
|
|
||||||
result = request.post(url, data)
|
result = request.post(url, data,
|
||||||
|
timeout=kwargs.get('timeout'),
|
||||||
|
network_delay=kwargs.get('network_delay'))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@log
|
@log
|
||||||
def unbanChatMember(self,
|
def unbanChatMember(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
user_id):
|
user_id,
|
||||||
|
**kwargs):
|
||||||
"""Use this method to unban a previously kicked user in a supergroup.
|
"""Use this method to unban a previously kicked user in a supergroup.
|
||||||
The user will not return to the group automatically, but will be able
|
The user will not return to the group automatically, but will be able
|
||||||
to join via link, etc. The bot must be an administrator in the group
|
to join via link, etc. The bot must be an administrator in the group
|
||||||
|
@ -1034,6 +1080,14 @@ class Bot(TelegramObject):
|
||||||
user_id:
|
user_id:
|
||||||
Unique identifier of the target user.
|
Unique identifier of the target user.
|
||||||
|
|
||||||
|
Keyword Args:
|
||||||
|
timeout (Optional[float]): If this value is specified, use it as
|
||||||
|
the definitive timeout (in seconds) for urlopen() operations.
|
||||||
|
network_delay (Optional[float]): If using the timeout (which is
|
||||||
|
a `timeout` for the Telegram servers operation),
|
||||||
|
then `network_delay` as an extra delay (in seconds) to
|
||||||
|
compensate for network latency. Defaults to 2.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: On success, `True` is returned.
|
bool: On success, `True` is returned.
|
||||||
|
|
||||||
|
@ -1047,7 +1101,9 @@ class Bot(TelegramObject):
|
||||||
data = {'chat_id': chat_id,
|
data = {'chat_id': chat_id,
|
||||||
'user_id': user_id}
|
'user_id': user_id}
|
||||||
|
|
||||||
result = request.post(url, data)
|
result = request.post(url, data,
|
||||||
|
timeout=kwargs.get('timeout'),
|
||||||
|
network_delay=kwargs.get('network_delay'))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -1055,7 +1111,8 @@ class Bot(TelegramObject):
|
||||||
def answerCallbackQuery(self,
|
def answerCallbackQuery(self,
|
||||||
callback_query_id,
|
callback_query_id,
|
||||||
text=None,
|
text=None,
|
||||||
show_alert=False):
|
show_alert=False,
|
||||||
|
**kwargs):
|
||||||
"""Use this method to send answers to callback queries sent from
|
"""Use this method to send answers to callback queries sent from
|
||||||
inline keyboards. The answer will be displayed to the user as a
|
inline keyboards. The answer will be displayed to the user as a
|
||||||
notification at the top of the chat screen or as an alert.
|
notification at the top of the chat screen or as an alert.
|
||||||
|
@ -1069,6 +1126,14 @@ class Bot(TelegramObject):
|
||||||
by the client instead of a notification at the top of the chat
|
by the client instead of a notification at the top of the chat
|
||||||
screen. Defaults to `False`.
|
screen. Defaults to `False`.
|
||||||
|
|
||||||
|
Keyword Args:
|
||||||
|
timeout (Optional[float]): If this value is specified, use it as
|
||||||
|
the definitive timeout (in seconds) for urlopen() operations.
|
||||||
|
network_delay (Optional[float]): If using the timeout (which is
|
||||||
|
a `timeout` for the Telegram servers operation),
|
||||||
|
then `network_delay` as an extra delay (in seconds) to
|
||||||
|
compensate for network latency. Defaults to 2.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: On success, `True` is returned.
|
bool: On success, `True` is returned.
|
||||||
|
|
||||||
|
@ -1086,7 +1151,9 @@ class Bot(TelegramObject):
|
||||||
if show_alert:
|
if show_alert:
|
||||||
data['show_alert'] = show_alert
|
data['show_alert'] = show_alert
|
||||||
|
|
||||||
result = request.post(url, data)
|
result = request.post(url, data,
|
||||||
|
timeout=kwargs.get('timeout'),
|
||||||
|
network_delay=kwargs.get('network_delay'))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -1098,7 +1165,8 @@ class Bot(TelegramObject):
|
||||||
inline_message_id=None,
|
inline_message_id=None,
|
||||||
parse_mode=None,
|
parse_mode=None,
|
||||||
disable_web_page_preview=None,
|
disable_web_page_preview=None,
|
||||||
reply_markup=None):
|
reply_markup=None,
|
||||||
|
**kwargs):
|
||||||
"""Use this method to edit text messages sent by the bot or via the bot
|
"""Use this method to edit text messages sent by the bot or via the bot
|
||||||
(for inline bots).
|
(for inline bots).
|
||||||
|
|
||||||
|
@ -1123,6 +1191,14 @@ class Bot(TelegramObject):
|
||||||
reply_markup:
|
reply_markup:
|
||||||
A JSON-serialized object for an inline keyboard.
|
A JSON-serialized object for an inline keyboard.
|
||||||
|
|
||||||
|
Keyword Args:
|
||||||
|
timeout (Optional[float]): If this value is specified, use it as
|
||||||
|
the definitive timeout (in seconds) for urlopen() operations.
|
||||||
|
network_delay (Optional[float]): If using the timeout (which is
|
||||||
|
a `timeout` for the Telegram servers operation),
|
||||||
|
then `network_delay` as an extra delay (in seconds) to
|
||||||
|
compensate for network latency. Defaults to 2.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:class:`telegram.Message`: On success, if edited message is sent by
|
:class:`telegram.Message`: On success, if edited message is sent by
|
||||||
the bot, the edited message is returned, otherwise `True` is
|
the bot, the edited message is returned, otherwise `True` is
|
||||||
|
@ -1153,7 +1229,9 @@ class Bot(TelegramObject):
|
||||||
else:
|
else:
|
||||||
data['reply_markup'] = reply_markup
|
data['reply_markup'] = reply_markup
|
||||||
|
|
||||||
result = request.post(url, data)
|
result = request.post(url, data,
|
||||||
|
timeout=kwargs.get('timeout'),
|
||||||
|
network_delay=kwargs.get('network_delay'))
|
||||||
|
|
||||||
return Message.de_json(result)
|
return Message.de_json(result)
|
||||||
|
|
||||||
|
@ -1271,8 +1349,7 @@ class Bot(TelegramObject):
|
||||||
def getUpdates(self,
|
def getUpdates(self,
|
||||||
offset=None,
|
offset=None,
|
||||||
limit=100,
|
limit=100,
|
||||||
timeout=0,
|
**kwargs):
|
||||||
network_delay=2.):
|
|
||||||
"""Use this method to receive incoming updates using long polling.
|
"""Use this method to receive incoming updates using long polling.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -1285,14 +1362,14 @@ class Bot(TelegramObject):
|
||||||
limit:
|
limit:
|
||||||
Limits the number of updates to be retrieved. Values between 1-100
|
Limits the number of updates to be retrieved. Values between 1-100
|
||||||
are accepted. Defaults to 100.
|
are accepted. Defaults to 100.
|
||||||
timeout:
|
|
||||||
Timeout in seconds for long polling. Defaults to 0, i.e. usual
|
Keyword Args:
|
||||||
short polling.
|
timeout (Optional[float]): If this value is specified, use it as
|
||||||
network_delay:
|
the definitive timeout (in seconds) for urlopen() operations.
|
||||||
Additional timeout in seconds to allow the response from Telegram
|
network_delay (Optional[float]): If using the timeout (which is
|
||||||
to take some time when using long polling. Defaults to 2, which
|
a `timeout` for the Telegram servers operation),
|
||||||
should be enough for most connections. Increase it if it takes very
|
then `network_delay` as an extra delay (in seconds) to
|
||||||
long for data to be transmitted from and to the Telegram servers.
|
compensate for network latency. Defaults to 2.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list[:class:`telegram.Message`]: A list of :class:`telegram.Update`
|
list[:class:`telegram.Message`]: A list of :class:`telegram.Update`
|
||||||
|
@ -1311,10 +1388,10 @@ class Bot(TelegramObject):
|
||||||
data['offset'] = offset
|
data['offset'] = offset
|
||||||
if limit:
|
if limit:
|
||||||
data['limit'] = limit
|
data['limit'] = limit
|
||||||
if timeout or timeout == 0:
|
|
||||||
data['timeout'] = timeout
|
|
||||||
|
|
||||||
result = request.post(url, data, network_delay=network_delay)
|
result = request.post(url, data,
|
||||||
|
timeout=kwargs.get('timeout'),
|
||||||
|
network_delay=kwargs.get('network_delay'))
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
|
@ -1327,7 +1404,8 @@ class Bot(TelegramObject):
|
||||||
@log
|
@log
|
||||||
def setWebhook(self,
|
def setWebhook(self,
|
||||||
webhook_url=None,
|
webhook_url=None,
|
||||||
certificate=None):
|
certificate=None,
|
||||||
|
**kwargs):
|
||||||
"""Use this method to specify a url and receive incoming updates via an
|
"""Use this method to specify a url and receive incoming updates via an
|
||||||
outgoing webhook. Whenever there is an update for the bot, we will send
|
outgoing webhook. Whenever there is an update for the bot, we will send
|
||||||
an HTTPS POST request to the specified url, containing a
|
an HTTPS POST request to the specified url, containing a
|
||||||
|
@ -1335,10 +1413,18 @@ class Bot(TelegramObject):
|
||||||
give up after a reasonable amount of attempts.
|
give up after a reasonable amount of attempts.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
url:
|
webhook_url:
|
||||||
HTTPS url to send updates to.
|
HTTPS url to send updates to.
|
||||||
Use an empty string to remove webhook integration
|
Use an empty string to remove webhook integration
|
||||||
|
|
||||||
|
Keyword Args:
|
||||||
|
timeout (Optional[float]): If this value is specified, use it as
|
||||||
|
the definitive timeout (in seconds) for urlopen() operations.
|
||||||
|
network_delay (Optional[float]): If using the timeout (which is
|
||||||
|
a `timeout` for the Telegram servers operation),
|
||||||
|
then `network_delay` as an extra delay (in seconds) to
|
||||||
|
compensate for network latency. Defaults to 2.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: On success, `True` is returned.
|
bool: On success, `True` is returned.
|
||||||
|
|
||||||
|
@ -1356,7 +1442,9 @@ class Bot(TelegramObject):
|
||||||
if certificate:
|
if certificate:
|
||||||
data['certificate'] = certificate
|
data['certificate'] = certificate
|
||||||
|
|
||||||
result = request.post(url, data)
|
result = request.post(url, data,
|
||||||
|
timeout=kwargs.get('timeout'),
|
||||||
|
network_delay=kwargs.get('network_delay'))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
|
@ -61,10 +61,13 @@ def run_async(func):
|
||||||
"""
|
"""
|
||||||
A wrapper to run a thread in a thread pool
|
A wrapper to run a thread in a thread pool
|
||||||
"""
|
"""
|
||||||
result = func(*pargs, **kwargs)
|
try:
|
||||||
semaphore.release()
|
result = func(*pargs, **kwargs)
|
||||||
with async_lock:
|
finally:
|
||||||
async_threads.remove(current_thread())
|
semaphore.release()
|
||||||
|
|
||||||
|
with async_lock:
|
||||||
|
async_threads.remove(current_thread())
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
|
|
|
@ -301,11 +301,12 @@ class Updater(object):
|
||||||
if use_ssl:
|
if use_ssl:
|
||||||
self._check_ssl_cert(cert, key)
|
self._check_ssl_cert(cert, key)
|
||||||
|
|
||||||
if not webhook_url:
|
# DO NOT CHANGE: Only set webhook if SSL is handled by library
|
||||||
webhook_url = self._gen_webhook_url(listen, port, url_path)
|
if not webhook_url:
|
||||||
|
webhook_url = self._gen_webhook_url(listen, port, url_path)
|
||||||
|
|
||||||
self._set_webhook(webhook_url, bootstrap_retries,
|
self._set_webhook(webhook_url, bootstrap_retries,
|
||||||
open(cert, 'rb') if use_ssl else None)
|
open(cert, 'rb'))
|
||||||
|
|
||||||
self.httpd.serve_forever(poll_interval=1)
|
self.httpd.serve_forever(poll_interval=1)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue