normalized-log-fix5
This commit is contained in:
parent
71e8f74be6
commit
a066446c96
25
bot/api.py
25
bot/api.py
|
@ -6,7 +6,7 @@ from bot.config import BOT_TOKEN
|
|||
import logging
|
||||
|
||||
# Create a logger instance
|
||||
logger = logging.getLogger(" bot.api ")
|
||||
logger = logging.getLogger("bot.api")
|
||||
|
||||
api_base = f"https://api.telegram.org/bot{BOT_TOKEN}/"
|
||||
|
||||
|
@ -16,20 +16,18 @@ async def telegram_api(endpoint: str, json_data=None, **kwargs):
|
|||
url = api_base + f"{endpoint}?{urlencode(kwargs)}"
|
||||
is_polling = endpoint == "getUpdates"
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
url = api_base + f"{endpoint}?{urlencode(kwargs)}"
|
||||
if not is_polling:
|
||||
logger.info(f' >>> {url} {json_data if json_data else ""}')
|
||||
async with session.get(
|
||||
url, data=json.dumps(json_data), headers=headers
|
||||
) as response:
|
||||
async with session.get(url, data=json.dumps(json_data), headers=headers) as response:
|
||||
data = await response.json()
|
||||
if not is_polling:
|
||||
logger.info(f" <<< {data}")
|
||||
return data
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
logger.error(f"Error in telegram_api: {e}")
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
|
@ -37,8 +35,15 @@ async def download_file(file_id):
|
|||
"""Asynchronously download a file from Telegram and yield the temporary file path."""
|
||||
# Get the file path of the file using the telegram_api method
|
||||
file = await telegram_api("getFile", file_id=file_id)
|
||||
|
||||
if not file.get("ok"):
|
||||
logger.error(f"Failed to get file info: {file.get('description')}")
|
||||
return
|
||||
|
||||
file_path = file["result"]["file_path"]
|
||||
download_url = f"{api_base}/{file_path}"
|
||||
sep = '' if file_path.startswith('/') else '/'
|
||||
# Construct the correct download URL
|
||||
download_url = f"{api_base}{sep}{file_path}"
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(download_url) as response:
|
||||
|
@ -47,7 +52,7 @@ async def download_file(file_id):
|
|||
async with aiofiles.tempfile.NamedTemporaryFile(delete=True) as temp_file:
|
||||
await temp_file.write(await response.read())
|
||||
await temp_file.flush()
|
||||
logger.error(f"download file: {temp_file.name }")
|
||||
logger.info(f"Downloaded file: {temp_file.name}")
|
||||
yield temp_file.name # Yield the path of the temporary file
|
||||
else:
|
||||
logger.error(f"Failed to download file: {response}")
|
||||
logger.error(f"Failed to download file: {response.status} - {await response.text()}")
|
Loading…
Reference in New Issue
Block a user