2023-04-23 16:54:58 +00:00
|
|
|
import aiohttp
|
2024-09-28 07:06:04 +00:00
|
|
|
import aiofiles
|
2023-04-16 14:58:53 +00:00
|
|
|
import json
|
2024-01-07 09:19:46 +00:00
|
|
|
from urllib.parse import urlencode
|
|
|
|
from bot.config import BOT_TOKEN
|
2023-09-11 20:04:53 +00:00
|
|
|
import logging
|
2023-09-06 10:20:50 +00:00
|
|
|
|
2023-09-11 20:04:53 +00:00
|
|
|
# Create a logger instance
|
2024-09-28 07:59:39 +00:00
|
|
|
logger = logging.getLogger("bot.api")
|
2024-09-27 08:28:41 +00:00
|
|
|
|
2024-02-12 12:50:35 +00:00
|
|
|
api_base = f"https://api.telegram.org/bot{BOT_TOKEN}/"
|
2024-09-28 08:09:23 +00:00
|
|
|
file_api_base = f"https://api.telegram.org/file/bot{BOT_TOKEN}/"
|
2023-04-16 14:58:53 +00:00
|
|
|
|
|
|
|
|
2024-02-12 12:50:35 +00:00
|
|
|
async def telegram_api(endpoint: str, json_data=None, **kwargs):
|
|
|
|
try:
|
|
|
|
url = api_base + f"{endpoint}?{urlencode(kwargs)}"
|
2024-09-27 06:23:55 +00:00
|
|
|
is_polling = endpoint == "getUpdates"
|
|
|
|
headers = {"Content-Type": "application/json"}
|
2024-09-28 07:59:39 +00:00
|
|
|
|
2024-02-12 12:50:35 +00:00
|
|
|
async with aiohttp.ClientSession() as session:
|
2024-09-26 11:27:44 +00:00
|
|
|
if not is_polling:
|
|
|
|
logger.info(f' >>> {url} {json_data if json_data else ""}')
|
2024-09-28 07:59:39 +00:00
|
|
|
async with session.get(url, data=json.dumps(json_data), headers=headers) as response:
|
2024-02-12 12:50:35 +00:00
|
|
|
data = await response.json()
|
2024-09-26 11:27:44 +00:00
|
|
|
if not is_polling:
|
2024-09-27 06:23:55 +00:00
|
|
|
logger.info(f" <<< {data}")
|
2024-02-12 12:50:35 +00:00
|
|
|
return data
|
2024-09-28 07:59:39 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error in telegram_api: {e}")
|
2024-02-12 12:50:35 +00:00
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2024-09-28 07:06:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2024-09-28 07:59:39 +00:00
|
|
|
|
|
|
|
if not file.get("ok"):
|
|
|
|
logger.error(f"Failed to get file info: {file.get('description')}")
|
|
|
|
return
|
|
|
|
|
2024-09-28 07:06:04 +00:00
|
|
|
file_path = file["result"]["file_path"]
|
2024-09-28 07:59:39 +00:00
|
|
|
sep = '' if file_path.startswith('/') else '/'
|
|
|
|
# Construct the correct download URL
|
2024-09-28 08:09:23 +00:00
|
|
|
download_url = f"{file_api_base}{sep}{file_path}"
|
2024-09-28 07:06:04 +00:00
|
|
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.get(download_url) as response:
|
|
|
|
if response.status == 200:
|
|
|
|
# Save the downloaded file to a temporary location
|
|
|
|
async with aiofiles.tempfile.NamedTemporaryFile(delete=True) as temp_file:
|
|
|
|
await temp_file.write(await response.read())
|
|
|
|
await temp_file.flush()
|
2024-09-28 07:59:39 +00:00
|
|
|
logger.info(f"Downloaded file: {temp_file.name}")
|
2024-09-28 07:06:04 +00:00
|
|
|
yield temp_file.name # Yield the path of the temporary file
|
|
|
|
else:
|
2024-09-28 07:59:39 +00:00
|
|
|
logger.error(f"Failed to download file: {response.status} - {await response.text()}")
|