1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Badiboy
7490aa0d26
Merge pull request #1400 from Badiboy/master
send_document param type fix
2021-12-25 16:26:50 +03:00
Badiboy
e59e2ee2ee send_document param type fix 2021-12-25 16:23:26 +03:00
Badiboy
f25dcad10c
Merge pull request #1399 from coder2020official/master
_make_request edited
2021-12-25 15:32:51 +03:00
_run
24a9491ec0 _make_request function edited 2021-12-25 16:04:29 +04:00
3 changed files with 57 additions and 22 deletions

View File

@ -1158,7 +1158,7 @@ class TeleBot:
allow_sending_without_reply))
def send_document(
self, chat_id: Union[int, str], data: Union[Any, str],
self, chat_id: Union[int, str], document: Union[Any, str],
reply_to_message_id: Optional[int]=None,
caption: Optional[str]=None,
reply_markup: Optional[REPLY_MARKUP_TYPES]=None,
@ -1169,11 +1169,12 @@ class TeleBot:
caption_entities: Optional[List[types.MessageEntity]]=None,
allow_sending_without_reply: Optional[bool]=None,
visible_file_name: Optional[str]=None,
disable_content_type_detection: Optional[bool]=None) -> types.Message:
disable_content_type_detection: Optional[bool]=None,
data: Optional[Union[Any, str]]=None) -> types.Message:
"""
Use this method to send general files.
:param chat_id: Unique identifier for the target chat or username of the target channel (in the format @channelusername)
:param data: (document) File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data
:param document: (document) File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data
:param reply_to_message_id: If the message is a reply, ID of the original message
:param caption: Document caption (may also be used when resending documents by file_id), 0-1024 characters after entities parsing
:param reply_markup:
@ -1185,13 +1186,17 @@ class TeleBot:
:param allow_sending_without_reply:
:param visible_file_name: allows to define file name that will be visible in the Telegram instead of original file name
:param disable_content_type_detection: Disables automatic server-side content type detection for files uploaded using multipart/form-data
:param data: function typo miss compatibility: do not use it
:return: API reply.
"""
parse_mode = self.parse_mode if (parse_mode is None) else parse_mode
if data and not(document):
# function typo miss compatibility
document = data
return types.Message.de_json(
apihelper.send_data(
self.token, chat_id, data, 'document',
self.token, chat_id, document, 'document',
reply_to_message_id = reply_to_message_id, reply_markup = reply_markup, parse_mode = parse_mode,
disable_notification = disable_notification, timeout = timeout, caption = caption, thumb = thumb,
caption_entities = caption_entities, allow_sending_without_reply = allow_sending_without_reply,

View File

@ -6,7 +6,7 @@ import re
import sys
import time
import traceback
from typing import Any, Callable, List, Optional, Union
from typing import Any, List, Optional, Union
# this imports are used to avoid circular import error
import telebot.util
@ -207,7 +207,7 @@ class AsyncTeleBot:
async def get_updates(self, offset: Optional[int]=None, limit: Optional[int]=None,
timeout: Optional[int]=None, allowed_updates: Optional[List]=None, request_timeout: Optional[int]=None) -> types.Update:
timeout: Optional[int]=None, allowed_updates: Optional[List]=None, request_timeout: Optional[int]=None) -> List[types.Update]:
json_updates = await asyncio_helper.get_updates(self.token, offset, limit, timeout, allowed_updates, request_timeout)
return [types.Update.de_json(ju) for ju in json_updates]
@ -249,7 +249,7 @@ class AsyncTeleBot:
Wrap polling with infinite loop and exception handling to avoid bot stops polling.
:param timeout: Request connection timeout
:param long_polling_timeout: Timeout in seconds for long polling (see API docs)
:param request_timeout: Timeout in seconds for long polling (see API docs)
:param skip_pending: skip old updates
:param logger_level: Custom logging level for infinity_polling logging.
Use logger levels from logging as a value. None/NOTSET = no error logging
@ -317,6 +317,14 @@ class AsyncTeleBot:
except asyncio.CancelledError:
return
except asyncio_helper.RequestTimeout as e:
logger.error(str(e))
if non_stop:
await asyncio.sleep(2)
continue
else:
return
except asyncio_helper.ApiTelegramException as e:
logger.error(str(e))
@ -1700,7 +1708,7 @@ class AsyncTeleBot:
allow_sending_without_reply))
async def send_document(
self, chat_id: Union[int, str], data: Union[Any, str],
self, chat_id: Union[int, str], document: Union[Any, str],
reply_to_message_id: Optional[int]=None,
caption: Optional[str]=None,
reply_markup: Optional[REPLY_MARKUP_TYPES]=None,
@ -1711,11 +1719,12 @@ class AsyncTeleBot:
caption_entities: Optional[List[types.MessageEntity]]=None,
allow_sending_without_reply: Optional[bool]=None,
visible_file_name: Optional[str]=None,
disable_content_type_detection: Optional[bool]=None) -> types.Message:
disable_content_type_detection: Optional[bool]=None,
data: Optional[Union[Any, str]]=None) -> types.Message:
"""
Use this method to send general files.
:param chat_id: Unique identifier for the target chat or username of the target channel (in the format @channelusername)
:param data: (document) File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data
:param document: (document) File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data
:param reply_to_message_id: If the message is a reply, ID of the original message
:param caption: Document caption (may also be used when resending documents by file_id), 0-1024 characters after entities parsing
:param reply_markup:
@ -1727,13 +1736,17 @@ class AsyncTeleBot:
:param allow_sending_without_reply:
:param visible_file_name: allows to async define file name that will be visible in the Telegram instead of original file name
:param disable_content_type_detection: Disables automatic server-side content type detection for files uploaded using multipart/form-data
:param data: function typo compatibility: do not use it
:return: API reply.
"""
parse_mode = self.parse_mode if (parse_mode is None) else parse_mode
if data and not(document):
# function typo miss compatibility
document = data
return types.Message.de_json(
await asyncio_helper.send_data(
self.token, chat_id, data, 'document',
self.token, chat_id, document, 'document',
reply_to_message_id = reply_to_message_id, reply_markup = reply_markup, parse_mode = parse_mode,
disable_notification = disable_notification, timeout = timeout, caption = caption, thumb = thumb,
caption_entities = caption_entities, allow_sending_without_reply = allow_sending_without_reply,

View File

@ -34,21 +34,32 @@ CONNECT_TIMEOUT = 15
READ_TIMEOUT = 30
LONG_POLLING_TIMEOUT = 10 # Should be positive, short polling should be used for testing purposes only (https://core.telegram.org/bots/api#getupdates)
REQUEST_TIMEOUT = 10
MAX_RETRIES = 3
logger = telebot.logger
RETRY_ON_ERROR = False
RETRY_TIMEOUT = 2
MAX_RETRIES = 15
async def _process_request(token, url, method='get', params=None, files=None, request_timeout=None):
params = compose_data(params, files)
if request_timeout is None:
request_timeout = REQUEST_TIMEOUT
timeout = aiohttp.ClientTimeout(total=request_timeout)
got_result = False
current_try=0
async with await session_manager._get_new_session() as session:
async with session.request(method=method, url=API_URL.format(token, url), data=params, timeout=request_timeout) as response:
logger.debug("Request: method={0} url={1} params={2} files={3} request_timeout={4}".format(method, url, params, files, request_timeout).replace(token, token.split(':')[0] + ":{TOKEN}"))
json_result = await _check_result(url, response)
if json_result:
return json_result['result']
while not got_result and current_try<MAX_RETRIES-1:
current_try +=1
try:
response = await session.request(method=method, url=API_URL.format(token, url), data=params, timeout=timeout)
logger.debug("Request: method={0} url={1} params={2} files={3} request_timeout={4} current_try={5}".format(method, url, params, files, request_timeout, current_try).replace(token, token.split(':')[0] + ":{TOKEN}"))
json_result = await _check_result(url, response)
if json_result:
return json_result['result']
except:
pass
if not got_result:
raise RequestTimeout("Request timeout. Request: method={0} url={1} params={2} files={3} request_timeout={4}".format(method, url, params, files, request_timeout, current_try))
def guess_filename(obj):
@ -1643,4 +1654,10 @@ class ApiTelegramException(ApiException):
function_name,
result)
self.result_json = result_json
self.error_code = result_json['error_code']
self.error_code = result_json['error_code']
class RequestTimeout(Exception):
"""
This class represents a request timeout.
"""
pass