Skip to content

Commit

Permalink
detect file name
Browse files Browse the repository at this point in the history
  • Loading branch information
z44d committed Jun 27, 2024
1 parent 006042b commit 47c8646
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions tgram/tgbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .methods import TelegramBotMethods
from .decorators import Decorators
from .errors import APIException
from .utils import API_URL
from .utils import API_URL, get_file_name
from .sync import wrap
from concurrent.futures.thread import ThreadPoolExecutor

Expand Down Expand Up @@ -155,7 +155,9 @@ async def _send_request(self, method: str, **kwargs) -> Any:
)
else:
value = str(value)
data.add_field(key, value)
data.add_field(
key, value, filename=get_file_name(value) if has_files else None
)

response = await session.request(
"POST" if has_files else "GET",
Expand Down
8 changes: 8 additions & 0 deletions tgram/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from .handlers import Handlers

import os

API_URL = "https://api.telegram.org/"
ALL_UPDATES = [
getattr(Handlers, i)
for i in filter(lambda x: not x.startswith("_"), Handlers.__dict__)
]


def get_file_name(obj):
name = getattr(obj, "name", None)
if name and isinstance(name, str) and name[0] != "<" and name[-1] != ">":
return os.path.basename(name)

0 comments on commit 47c8646

Please sign in to comment.