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

Added InputFile

This commit is contained in:
_run 2022-08-01 12:40:43 +05:00
parent 914c5fdf0c
commit 65b353ffae
2 changed files with 32 additions and 0 deletions

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import time import time
from datetime import datetime from datetime import datetime
import os
try: try:
import ujson as json import ujson as json
@ -87,6 +88,14 @@ def _make_request(token, method_name, method='get', params=None, files=None):
logger.debug("Request: method={0} url={1} params={2} files={3}".format(method, request_url, params, files).replace(token, token.split(':')[0] + ":{TOKEN}")) logger.debug("Request: method={0} url={1} params={2} files={3}".format(method, request_url, params, files).replace(token, token.split(':')[0] + ":{TOKEN}"))
read_timeout = READ_TIMEOUT read_timeout = READ_TIMEOUT
connect_timeout = CONNECT_TIMEOUT connect_timeout = CONNECT_TIMEOUT
if files:
# process types.InputFile
for key, value in files.items():
if isinstance(value, types.InputFile):
files[key] = value.file
if files and format_header_param: if files and format_header_param:
fields.format_header_param = _no_encode(format_header_param) fields.format_header_param = _no_encode(format_header_param)
if params: if params:

View File

@ -1,6 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from io import IOBase
import logging import logging
import os
from pathlib import Path
from typing import Dict, List, Optional, Union from typing import Dict, List, Optional, Union
from abc import ABC from abc import ABC
@ -6601,3 +6604,23 @@ class ChatAdministratorRights(JsonDeserializable, JsonSerializable, Dictionaryab
class InputFile:
def __init__(self, file) -> None:
self._file, self.file_name = self._resolve_file(file)
def _resolve_file(self, file):
if isinstance(file, str):
_file = open(file, 'rb')
return _file, os.path.basename(file.name)
elif isinstance(file, IOBase):
return file, os.path.basename(file.name)
elif isinstance(file, Path):
_file = open(file, 'rb')
return _file, os.path.basename(file.name)
else:
raise TypeError("File must be a string or a file-like object(pathlib.Path, io.IOBase).")
@property
def file(self):
return self._file