taslim19
Use tempfile.gettempdir() for downloads in Telegram.py to avoid permission errors
dc00de0
import base64
import requests
import tempfile
from pyrogram import filters
from DragMusic import app
API_URL = "http://aizensolo.ru/getName"
API_TOKEN = "TEST-API-TOKEN"
@app.on_message(filters.command("whois") & filters.reply)
async def whois_character(client, message):
if not message.reply_to_message or not (message.reply_to_message.photo or message.reply_to_message.document):
return await message.reply("Reply to an image to identify the character.")
# Use a temporary file for download
with tempfile.NamedTemporaryFile(suffix=".jpg") as temp_file:
await message.reply_to_message.download(file_name=temp_file.name)
temp_file.seek(0)
encoded_string = base64.b64encode(temp_file.read())
data = {
'api_token': API_TOKEN,
'photo_b64': encoded_string.decode()
}
try:
response = requests.post(API_URL, json=data, timeout=15)
response.raise_for_status()
result = response.json()
if result.get("status") and all(k in result for k in ("name", "prefix", "bot_id", "bot_name")):
reply_text = (
f"Name: {result['name']}\n"
f"Prefix: {result['prefix']}\n"
f"Bot: [{result['bot_name']}](https://t.me/{result['bot_name']}) (`{result['bot_id']}`)"
)
elif not result.get("status"):
reply_text = f"No character found. (API status: {result.get('status')})"
else:
reply_text = f"API returned unexpected data: {result}"
except Exception as e:
reply_text = f"API error: {e}\nRaw response: {response.text if 'response' in locals() else 'No response'}"
await message.reply(reply_text)