I’m having trouble with my Telegram bot when trying to send a PDF file. Every time I attempt to send the document, I get this error message: aiogram.exceptions.TelegramBadRequest: Telegram server says - Bad Request: failed to get HTTP URL content
Here’s my code that handles photo processing and PDF generation:
import os
import uuid
import asyncio
from aiogram import Router, F, Bot
from aiogram.filters import CommandStart
from aiogram.types import Message
from gradio_client import Client, handle_file
msg_router = Router()
def process_math_question(file_path):
ai_client = Client("Qwen/Qwen2-Math-Demo")
response = ai_client.predict(
image=handle_file(file_path),
sketchpad={
"background": handle_file(
'https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png'),
"layers": [],
"composite": None
},
question="solve this, answer in LaTex format",
api_name="/math_chat_bot"
)
return response
@msg_router.message(CommandStart())
async def start_command(msg: Message):
await msg.answer("Hello there!")
@msg_router.message(F.photo)
async def handle_photo(msg: Message, telegram_bot: Bot):
temp_image = f"{uuid.uuid4()}.png"
file_data = await telegram_bot.get_file(msg.photo[-1].file_id)
await telegram_bot.download_file(file_data.file_path, destination=temp_image)
await msg.answer("Processing your request...")
math_result = await asyncio.to_thread(process_math_question, temp_image)
output_file = f"{uuid.uuid4()}.tex"
with open(output_file, "w") as tex_file:
tex_file.write(f"{math_result}")
os.system('bash convert_script.sh')
await msg.answer_document("output.pdf")
os.remove(temp_image)
os.remove(output_file)
os.remove("output.tex")
I tried using msg.answer_document("filename")
but it doesn’t work. The PDF file gets created successfully by the bash script, but sending it through the bot fails. What could be causing this issue?