2015-09-05 16:56:06 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2021-01-03 06:10:24 +01:00
|
|
|
# Copyright (C) 2015-2021
|
2016-01-05 14:12:03 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-09-05 16:56:06 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2017-09-01 08:43:08 +02:00
|
|
|
"""This module contains methods to make POST and GET requests."""
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
import logging
|
2017-01-06 22:48:34 +01:00
|
|
|
import os
|
|
|
|
import socket
|
2017-07-29 13:30:27 +02:00
|
|
|
import sys
|
2017-05-22 23:21:51 +02:00
|
|
|
import warnings
|
2015-09-05 16:56:06 +02:00
|
|
|
|
2016-08-26 09:40:46 +02:00
|
|
|
try:
|
|
|
|
import ujson as json
|
|
|
|
except ImportError:
|
2020-10-06 19:28:40 +02:00
|
|
|
import json # type: ignore[no-redef]
|
2015-09-05 16:56:06 +02:00
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
from typing import Any, Union
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
import certifi
|
2017-09-25 20:58:10 +02:00
|
|
|
|
2017-05-22 23:21:51 +02:00
|
|
|
try:
|
|
|
|
import telegram.vendor.ptb_urllib3.urllib3 as urllib3
|
|
|
|
import telegram.vendor.ptb_urllib3.urllib3.contrib.appengine as appengine
|
|
|
|
from telegram.vendor.ptb_urllib3.urllib3.connection import HTTPConnection
|
2018-09-10 21:08:05 +02:00
|
|
|
from telegram.vendor.ptb_urllib3.urllib3.fields import RequestField
|
2020-10-31 16:33:34 +01:00
|
|
|
from telegram.vendor.ptb_urllib3.urllib3.util.timeout import Timeout
|
2017-08-11 23:58:41 +02:00
|
|
|
except ImportError: # pragma: no cover
|
2020-02-02 21:56:25 +01:00
|
|
|
try:
|
2020-10-06 19:28:40 +02:00
|
|
|
import urllib3 # type: ignore[no-redef]
|
|
|
|
import urllib3.contrib.appengine as appengine # type: ignore[no-redef]
|
|
|
|
from urllib3.connection import HTTPConnection # type: ignore[no-redef]
|
|
|
|
from urllib3.fields import RequestField # type: ignore[no-redef]
|
2020-10-31 16:33:34 +01:00
|
|
|
from urllib3.util.timeout import Timeout # type: ignore[no-redef]
|
2020-10-09 17:22:07 +02:00
|
|
|
|
|
|
|
warnings.warn(
|
|
|
|
'python-telegram-bot is using upstream urllib3. This is allowed but not '
|
|
|
|
'supported by python-telegram-bot maintainers.'
|
|
|
|
)
|
2020-02-02 21:56:25 +01:00
|
|
|
except ImportError:
|
|
|
|
warnings.warn(
|
|
|
|
"python-telegram-bot wasn't properly installed. Please refer to README.rst on "
|
2020-10-09 17:22:07 +02:00
|
|
|
"how to properly install."
|
|
|
|
)
|
2020-02-02 21:56:25 +01:00
|
|
|
raise
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
# pylint: disable=C0412
|
|
|
|
from telegram import InputFile, InputMedia, TelegramError
|
2020-10-09 17:22:07 +02:00
|
|
|
from telegram.error import (
|
|
|
|
BadRequest,
|
|
|
|
ChatMigrated,
|
|
|
|
Conflict,
|
2020-10-31 16:33:34 +01:00
|
|
|
InvalidToken,
|
|
|
|
NetworkError,
|
|
|
|
RetryAfter,
|
|
|
|
TimedOut,
|
|
|
|
Unauthorized,
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2020-10-06 19:28:40 +02:00
|
|
|
from telegram.utils.types import JSONDict
|
2021-05-29 16:18:16 +02:00
|
|
|
from telegram.utils.deprecate import set_new_attribute_deprecated
|
2018-09-10 21:08:05 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
def _render_part(self: RequestField, name: str, value: str) -> str: # pylint: disable=W0613
|
2021-05-27 09:38:17 +02:00
|
|
|
r"""
|
2018-09-10 21:08:05 +02:00
|
|
|
Monkey patch urllib3.urllib3.fields.RequestField to make it *not* support RFC2231 compliant
|
|
|
|
Content-Disposition headers since telegram servers don't understand it. Instead just escape
|
2020-06-15 18:20:51 +02:00
|
|
|
\\ and " and replace any \n and \r with a space.
|
2018-09-10 21:08:05 +02:00
|
|
|
"""
|
2021-01-17 23:24:20 +01:00
|
|
|
value = value.replace('\\', '\\\\').replace('"', '\\"')
|
|
|
|
value = value.replace('\r', ' ').replace('\n', ' ')
|
|
|
|
return f'{name}="{value}"'
|
2018-09-10 21:08:05 +02:00
|
|
|
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
RequestField._render_part = _render_part # type: ignore # pylint: disable=W0212
|
2018-09-10 21:08:05 +02:00
|
|
|
|
2021-03-13 15:14:10 +01:00
|
|
|
logging.getLogger('telegram.vendor.ptb_urllib3.urllib3').setLevel(logging.WARNING)
|
2016-06-19 23:40:34 +02:00
|
|
|
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
USER_AGENT = 'Python Telegram Bot (https://github.com/python-telegram-bot/python-telegram-bot)'
|
|
|
|
|
2015-09-05 16:56:06 +02:00
|
|
|
|
2020-06-15 18:20:51 +02:00
|
|
|
class Request:
|
2015-09-05 16:56:06 +02:00
|
|
|
"""
|
2016-09-06 15:38:07 +02:00
|
|
|
Helper class for python-telegram-bot which provides methods to perform POST & GET towards
|
2021-04-30 10:47:41 +02:00
|
|
|
Telegram servers.
|
2015-09-07 20:54:12 +02:00
|
|
|
|
2015-09-05 16:56:06 +02:00
|
|
|
Args:
|
2021-04-30 10:47:41 +02:00
|
|
|
con_pool_size (:obj:`int`): Number of connections to keep in the connection pool.
|
|
|
|
proxy_url (:obj:`str`): The URL to the proxy server. For example: `http://127.0.0.1:3128`.
|
|
|
|
urllib3_proxy_kwargs (:obj:`dict`): Arbitrary arguments passed as-is to
|
|
|
|
:obj:`urllib3.ProxyManager`. This value will be ignored if :attr:`proxy_url` is not
|
|
|
|
set.
|
|
|
|
connect_timeout (:obj:`int` | :obj:`float`): The maximum amount of time (in seconds) to
|
|
|
|
wait for a connection attempt to a server to succeed. :obj:`None` will set an
|
|
|
|
infinite timeout for connection attempts. Defaults to ``5.0``.
|
|
|
|
read_timeout (:obj:`int` | :obj:`float`): The maximum amount of time (in seconds) to wait
|
|
|
|
between consecutive read operations for a response from the server. :obj:`None` will
|
|
|
|
set an infinite timeout. This value is usually overridden by the various
|
|
|
|
:class:`telegram.Bot` methods. Defaults to ``5.0``.
|
2016-05-29 23:26:18 +02:00
|
|
|
|
2015-09-05 16:56:06 +02:00
|
|
|
"""
|
2015-09-07 20:54:12 +02:00
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ('_connect_timeout', '_con_pool_size', '_con_pool', '__dict__')
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
con_pool_size: int = 1,
|
|
|
|
proxy_url: str = None,
|
|
|
|
urllib3_proxy_kwargs: JSONDict = None,
|
|
|
|
connect_timeout: float = 5.0,
|
|
|
|
read_timeout: float = 5.0,
|
|
|
|
):
|
2016-09-06 15:38:07 +02:00
|
|
|
if urllib3_proxy_kwargs is None:
|
2021-04-05 13:25:27 +02:00
|
|
|
urllib3_proxy_kwargs = {}
|
2016-09-06 15:38:07 +02:00
|
|
|
|
2017-01-06 22:48:34 +01:00
|
|
|
self._connect_timeout = connect_timeout
|
|
|
|
|
2017-07-29 13:30:27 +02:00
|
|
|
sockopts = HTTPConnection.default_socket_options + [
|
2020-10-09 17:22:07 +02:00
|
|
|
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
|
|
|
]
|
2017-07-29 13:30:27 +02:00
|
|
|
|
|
|
|
# TODO: Support other platforms like mac and windows.
|
|
|
|
if 'linux' in sys.platform:
|
2020-10-09 17:22:07 +02:00
|
|
|
sockopts.append(
|
2021-01-23 13:40:19 +01:00
|
|
|
(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 120) # pylint: disable=no-member
|
|
|
|
)
|
2020-10-09 17:22:07 +02:00
|
|
|
sockopts.append(
|
2021-01-23 13:40:19 +01:00
|
|
|
(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 30) # pylint: disable=no-member
|
|
|
|
)
|
2020-10-09 17:22:07 +02:00
|
|
|
sockopts.append(
|
2021-01-23 13:40:19 +01:00
|
|
|
(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 8) # pylint: disable=no-member
|
|
|
|
)
|
2017-07-29 13:30:27 +02:00
|
|
|
|
2017-08-12 15:45:38 +02:00
|
|
|
self._con_pool_size = con_pool_size
|
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
kwargs = dict(
|
|
|
|
maxsize=con_pool_size,
|
|
|
|
cert_reqs='CERT_REQUIRED',
|
|
|
|
ca_certs=certifi.where(),
|
2017-07-29 13:30:27 +02:00
|
|
|
socket_options=sockopts,
|
2020-10-09 17:22:07 +02:00
|
|
|
timeout=urllib3.Timeout(connect=self._connect_timeout, read=read_timeout, total=None),
|
|
|
|
)
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
# Set a proxy according to the following order:
|
|
|
|
# * proxy defined in proxy_url (+ urllib3_proxy_kwargs)
|
|
|
|
# * proxy set in `HTTPS_PROXY` env. var.
|
|
|
|
# * proxy set in `https_proxy` env. var.
|
|
|
|
# * None (if no proxy is configured)
|
|
|
|
|
|
|
|
if not proxy_url:
|
|
|
|
proxy_url = os.environ.get('HTTPS_PROXY') or os.environ.get('https_proxy')
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
self._con_pool: Union[
|
|
|
|
urllib3.PoolManager,
|
|
|
|
appengine.AppEngineManager,
|
|
|
|
'SOCKSProxyManager', # noqa: F821
|
|
|
|
urllib3.ProxyManager,
|
|
|
|
] = None # type: ignore
|
2016-09-06 15:38:07 +02:00
|
|
|
if not proxy_url:
|
2017-05-22 23:21:51 +02:00
|
|
|
if appengine.is_appengine_sandbox():
|
2017-04-27 10:59:35 +02:00
|
|
|
# Use URLFetch service if running in App Engine
|
2020-10-06 19:28:40 +02:00
|
|
|
self._con_pool = appengine.AppEngineManager()
|
2017-04-27 10:59:35 +02:00
|
|
|
else:
|
2020-10-06 19:28:40 +02:00
|
|
|
self._con_pool = urllib3.PoolManager(**kwargs)
|
2016-09-06 15:38:07 +02:00
|
|
|
else:
|
|
|
|
kwargs.update(urllib3_proxy_kwargs)
|
2017-02-25 19:47:56 +01:00
|
|
|
if proxy_url.startswith('socks'):
|
2017-04-29 13:42:36 +02:00
|
|
|
try:
|
2020-10-31 16:33:34 +01:00
|
|
|
# pylint: disable=C0415
|
2017-05-22 23:21:51 +02:00
|
|
|
from telegram.vendor.ptb_urllib3.urllib3.contrib.socks import SOCKSProxyManager
|
2020-10-31 16:33:34 +01:00
|
|
|
except ImportError as exc:
|
|
|
|
raise RuntimeError('PySocks is missing') from exc
|
2020-10-06 19:28:40 +02:00
|
|
|
self._con_pool = SOCKSProxyManager(proxy_url, **kwargs)
|
2017-02-25 19:47:56 +01:00
|
|
|
else:
|
|
|
|
mgr = urllib3.proxy_from_url(proxy_url, **kwargs)
|
|
|
|
if mgr.proxy.auth:
|
|
|
|
# TODO: what about other auth types?
|
|
|
|
auth_hdrs = urllib3.make_headers(proxy_basic_auth=mgr.proxy.auth)
|
|
|
|
mgr.proxy_headers.update(auth_hdrs)
|
2016-09-06 15:38:07 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
self._con_pool = mgr
|
2016-09-06 15:38:07 +02:00
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
def __setattr__(self, key: str, value: object) -> None:
|
|
|
|
set_new_attribute_deprecated(self, key, value)
|
|
|
|
|
2017-08-12 15:45:38 +02:00
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def con_pool_size(self) -> int:
|
2017-08-12 15:45:38 +02:00
|
|
|
"""The size of the connection pool used."""
|
|
|
|
return self._con_pool_size
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def stop(self) -> None:
|
2021-05-27 09:38:17 +02:00
|
|
|
"""Performs cleanup on shutdown."""
|
2020-10-06 19:28:40 +02:00
|
|
|
self._con_pool.clear() # type: ignore
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2020-10-06 19:28:40 +02:00
|
|
|
def _parse(json_data: bytes) -> Union[JSONDict, bool]:
|
2016-09-06 15:38:07 +02:00
|
|
|
"""Try and parse the JSON returned from Telegram.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: A JSON parsed as Python dict with results - on error this dict will be empty.
|
|
|
|
|
|
|
|
"""
|
2020-02-02 23:12:27 +01:00
|
|
|
decoded_s = json_data.decode('utf-8', 'replace')
|
2016-09-06 15:38:07 +02:00
|
|
|
try:
|
|
|
|
data = json.loads(decoded_s)
|
2020-10-31 16:33:34 +01:00
|
|
|
except ValueError as exc:
|
|
|
|
raise TelegramError('Invalid server response') from exc
|
2016-09-06 15:38:07 +02:00
|
|
|
|
2017-09-25 20:58:10 +02:00
|
|
|
if not data.get('ok'): # pragma: no cover
|
2016-09-06 15:38:07 +02:00
|
|
|
description = data.get('description')
|
|
|
|
parameters = data.get('parameters')
|
|
|
|
if parameters:
|
|
|
|
migrate_to_chat_id = parameters.get('migrate_to_chat_id')
|
|
|
|
if migrate_to_chat_id:
|
|
|
|
raise ChatMigrated(migrate_to_chat_id)
|
2016-10-04 00:27:45 +02:00
|
|
|
retry_after = parameters.get('retry_after')
|
|
|
|
if retry_after:
|
|
|
|
raise RetryAfter(retry_after)
|
2016-09-06 15:38:07 +02:00
|
|
|
if description:
|
|
|
|
return description
|
|
|
|
|
|
|
|
return data['result']
|
|
|
|
|
2021-01-30 11:38:54 +01:00
|
|
|
def _request_wrapper(self, *args: object, **kwargs: Any) -> bytes:
|
2016-09-06 15:38:07 +02:00
|
|
|
"""Wraps urllib3 request for handling known exceptions.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
args: unnamed arguments, passed to urllib3 request.
|
2021-04-30 10:47:41 +02:00
|
|
|
kwargs: keyword arguments, passed to urllib3 request.
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
Returns:
|
2020-10-06 19:28:40 +02:00
|
|
|
bytes: A non-parsed JSON text.
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
TelegramError
|
|
|
|
|
|
|
|
"""
|
2017-01-03 22:41:52 +01:00
|
|
|
# Make sure to hint Telegram servers that we reuse connections by sending
|
|
|
|
# "Connection: keep-alive" in the HTTP headers.
|
|
|
|
if 'headers' not in kwargs:
|
|
|
|
kwargs['headers'] = {}
|
|
|
|
kwargs['headers']['connection'] = 'keep-alive'
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
# Also set our user agent
|
|
|
|
kwargs['headers']['user-agent'] = USER_AGENT
|
2017-01-03 22:41:52 +01:00
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
try:
|
|
|
|
resp = self._con_pool.request(*args, **kwargs)
|
2020-10-31 16:33:34 +01:00
|
|
|
except urllib3.exceptions.TimeoutError as error:
|
|
|
|
raise TimedOut() from error
|
2016-09-06 15:38:07 +02:00
|
|
|
except urllib3.exceptions.HTTPError as error:
|
|
|
|
# HTTPError must come last as its the base urllib3 exception class
|
|
|
|
# TODO: do something smart here; for now just raise NetworkError
|
2020-11-23 22:09:29 +01:00
|
|
|
raise NetworkError(f'urllib3 HTTPError {error}') from error
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
if 200 <= resp.status <= 299:
|
|
|
|
# 200-299 range are HTTP success statuses
|
|
|
|
return resp.data
|
|
|
|
|
|
|
|
try:
|
2020-10-06 19:28:40 +02:00
|
|
|
message = str(self._parse(resp.data))
|
2016-09-06 15:38:07 +02:00
|
|
|
except ValueError:
|
2017-06-21 22:34:35 +02:00
|
|
|
message = 'Unknown HTTPError'
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
if resp.status in (401, 403):
|
2017-05-12 17:38:36 +02:00
|
|
|
raise Unauthorized(message)
|
2020-10-31 16:33:34 +01:00
|
|
|
if resp.status == 400:
|
2017-01-07 22:09:06 +01:00
|
|
|
raise BadRequest(message)
|
2020-10-31 16:33:34 +01:00
|
|
|
if resp.status == 404:
|
2016-11-09 14:36:42 +01:00
|
|
|
raise InvalidToken()
|
2020-10-31 16:33:34 +01:00
|
|
|
if resp.status == 409:
|
2018-10-29 22:08:52 +01:00
|
|
|
raise Conflict(message)
|
2020-10-31 16:33:34 +01:00
|
|
|
if resp.status == 413:
|
2020-10-09 17:22:07 +02:00
|
|
|
raise NetworkError(
|
|
|
|
'File too large. Check telegram api limits '
|
|
|
|
'https://core.telegram.org/bots/api#senddocument'
|
|
|
|
)
|
2020-10-31 16:33:34 +01:00
|
|
|
if resp.status == 502:
|
2016-09-06 15:38:07 +02:00
|
|
|
raise NetworkError('Bad Gateway')
|
2020-11-23 22:09:29 +01:00
|
|
|
raise NetworkError(f'{message} ({resp.status})')
|
2016-09-06 15:38:07 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def post(self, url: str, data: JSONDict, timeout: float = None) -> Union[JSONDict, bool]:
|
2016-09-06 15:38:07 +02:00
|
|
|
"""Request an URL.
|
2017-01-06 22:48:34 +01:00
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
Args:
|
2017-08-02 04:56:07 +02:00
|
|
|
url (:obj:`str`): The web location we want to retrieve.
|
2021-04-30 10:47:41 +02:00
|
|
|
data (Dict[:obj:`str`, :obj:`str` | :obj:`int`], optional): A dict of key/value pairs.
|
2020-06-30 22:07:38 +02:00
|
|
|
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
|
|
|
|
the read timeout from the server (instead of the one specified during creation of
|
|
|
|
the connection pool).
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A JSON object.
|
|
|
|
|
|
|
|
"""
|
2017-01-06 22:48:34 +01:00
|
|
|
urlopen_kwargs = {}
|
|
|
|
|
|
|
|
if timeout is not None:
|
|
|
|
urlopen_kwargs['timeout'] = Timeout(read=timeout, connect=self._connect_timeout)
|
|
|
|
|
2020-06-30 22:07:38 +02:00
|
|
|
if data is None:
|
|
|
|
data = {}
|
2016-09-06 15:38:07 +02:00
|
|
|
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
# Are we uploading files?
|
|
|
|
files = False
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
# pylint: disable=R1702
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
for key, val in data.copy().items():
|
|
|
|
if isinstance(val, InputFile):
|
|
|
|
# Convert the InputFile to urllib3 field format
|
|
|
|
data[key] = val.field_tuple
|
|
|
|
files = True
|
|
|
|
elif isinstance(val, (float, int)):
|
|
|
|
# Urllib3 doesn't like floats it seems
|
|
|
|
data[key] = str(val)
|
|
|
|
elif key == 'media':
|
|
|
|
# One media or multiple
|
|
|
|
if isinstance(val, InputMedia):
|
|
|
|
# Attach and set val to attached name
|
|
|
|
data[key] = val.to_json()
|
2020-10-06 19:28:40 +02:00
|
|
|
if isinstance(val.media, InputFile): # type: ignore
|
|
|
|
data[val.media.attach] = val.media.field_tuple # type: ignore
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
else:
|
|
|
|
# Attach and set val to attached name for all
|
|
|
|
media = []
|
2020-10-31 16:33:34 +01:00
|
|
|
for med in val:
|
|
|
|
media_dict = med.to_dict()
|
2020-09-19 16:50:34 +02:00
|
|
|
media.append(media_dict)
|
2020-10-31 16:33:34 +01:00
|
|
|
if isinstance(med.media, InputFile):
|
|
|
|
data[med.media.attach] = med.media.field_tuple
|
2020-09-19 16:50:34 +02:00
|
|
|
# if the file has a thumb, we also need to attach it to the data
|
|
|
|
if "thumb" in media_dict:
|
2020-10-31 16:33:34 +01:00
|
|
|
data[med.thumb.attach] = med.thumb.field_tuple
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
data[key] = json.dumps(media)
|
|
|
|
files = True
|
2020-11-29 16:20:46 +01:00
|
|
|
elif isinstance(val, list):
|
|
|
|
# In case we're sending files, we need to json-dump lists manually
|
|
|
|
# As we can't know if that's the case, we just json-dump here
|
|
|
|
data[key] = json.dumps(val)
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
|
|
|
|
# Use multipart upload if we're uploading files, otherwise use JSON
|
|
|
|
if files:
|
|
|
|
result = self._request_wrapper('POST', url, fields=data, **urlopen_kwargs)
|
2016-09-06 15:38:07 +02:00
|
|
|
else:
|
2020-10-09 17:22:07 +02:00
|
|
|
result = self._request_wrapper(
|
|
|
|
'POST',
|
|
|
|
url,
|
|
|
|
body=json.dumps(data).encode('utf-8'),
|
|
|
|
headers={'Content-Type': 'application/json'},
|
|
|
|
**urlopen_kwargs,
|
|
|
|
)
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
return self._parse(result)
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def retrieve(self, url: str, timeout: float = None) -> bytes:
|
2016-12-18 03:05:00 +01:00
|
|
|
"""Retrieve the contents of a file by its URL.
|
2017-01-06 22:48:34 +01:00
|
|
|
|
2016-12-18 03:05:00 +01:00
|
|
|
Args:
|
2017-08-02 04:56:07 +02:00
|
|
|
url (:obj:`str`): The web location we want to retrieve.
|
|
|
|
timeout (:obj:`int` | :obj:`float`): If this value is specified, use it as the read
|
|
|
|
timeout from the server (instead of the one specified during creation of the
|
|
|
|
connection pool).
|
2017-01-06 22:48:34 +01:00
|
|
|
|
2016-12-18 03:05:00 +01:00
|
|
|
"""
|
2017-01-06 22:48:34 +01:00
|
|
|
urlopen_kwargs = {}
|
|
|
|
if timeout is not None:
|
|
|
|
urlopen_kwargs['timeout'] = Timeout(read=timeout, connect=self._connect_timeout)
|
|
|
|
|
|
|
|
return self._request_wrapper('GET', url, **urlopen_kwargs)
|
2016-12-18 03:05:00 +01:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def download(self, url: str, filename: str, timeout: float = None) -> None:
|
2016-09-06 15:38:07 +02:00
|
|
|
"""Download a file by its URL.
|
2018-03-05 12:17:56 +01:00
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
Args:
|
2021-04-30 10:47:41 +02:00
|
|
|
url (:obj:`str`): The web location we want to retrieve.
|
|
|
|
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
|
|
|
|
the read timeout from the server (instead of the one specified during creation of
|
|
|
|
the connection pool).
|
2020-10-06 19:28:40 +02:00
|
|
|
filename (:obj:`str`): The filename within the path to download the file.
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
"""
|
2017-01-06 22:48:34 +01:00
|
|
|
buf = self.retrieve(url, timeout=timeout)
|
2016-09-06 15:38:07 +02:00
|
|
|
with open(filename, 'wb') as fobj:
|
|
|
|
fobj.write(buf)
|