deepak191z commited on
Commit
a7a05db
·
verified ·
1 Parent(s): a032ea3

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +10 -5
main.py CHANGED
@@ -102,7 +102,7 @@ async def convert_pdf_to_images(pdf_path):
102
  os.remove(pdf_path)
103
 
104
  async def send_image_to_telegram(image_path, caption):
105
- """ Sends an image to Telegram Bot with specified caption """
106
  try:
107
  with open(image_path, "rb") as img_file:
108
  message = await bot.send_photo(
@@ -110,14 +110,19 @@ async def send_image_to_telegram(image_path, caption):
110
  photo=img_file,
111
  caption=caption
112
  )
113
- # Return better URL format if possible (note: may not work for private chats)
114
- if message.chat.username:
115
- return f"https://t.me/{message.chat.username}/{message.message_id}"
116
- return f"https://t.me/c/{str(message.chat.id).replace('-100', '')}/{message.message_id}"
 
 
 
 
117
  except Exception as e:
118
  logger.error(f"Error sending image: {e}")
119
  return None
120
 
 
121
  if __name__ == "__main__":
122
  import uvicorn
123
  uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info", reload=True)
 
102
  os.remove(pdf_path)
103
 
104
  async def send_image_to_telegram(image_path, caption):
105
+ """ Sends an image to Telegram Bot and returns the direct image URL """
106
  try:
107
  with open(image_path, "rb") as img_file:
108
  message = await bot.send_photo(
 
110
  photo=img_file,
111
  caption=caption
112
  )
113
+ # Extract the direct URL of the uploaded image
114
+ image_url = message.photo[-1].file_id # Get the highest quality image ID
115
+
116
+ # Generate the direct file link
117
+ file_info = await bot.get_file(image_url)
118
+ direct_link = f"https://api.telegram.org/file/bot{BOT_TOKEN}/{file_info.file_path}"
119
+
120
+ return direct_link
121
  except Exception as e:
122
  logger.error(f"Error sending image: {e}")
123
  return None
124
 
125
+
126
  if __name__ == "__main__":
127
  import uvicorn
128
  uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info", reload=True)