Spaces:
Paused

nvd commited on
Commit
31924f5
·
verified ·
1 Parent(s): 85ba7d7

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +17 -11
bot.py CHANGED
@@ -121,19 +121,25 @@ async def send_message(chat_id, text, reply_markup=None):
121
  params["reply_markup"] = json.dumps(reply_markup)
122
  return await bale_request("sendMessage", params)
123
 
 
 
124
  async def send_document_file(chat_id, file_path, caption=""):
125
- """Upload a local file to Bale using sendDocument with multipart/form-data."""
126
  url = BALE_API + "sendDocument"
127
- async with aiohttp.ClientSession() as session:
128
- with open(file_path, "rb") as f:
129
- form = aiohttp.FormData()
130
- form.add_field("chat_id", str(chat_id))
131
- form.add_field("caption", caption)
132
- form.add_field("document", f, filename=os.path.basename(file_path))
133
- async with session.post(url, data=form) as resp:
134
- result = await resp.json()
135
- logger.info(f"📨 sendDocument (file) response: {result}")
136
- return result
 
 
 
 
137
 
138
  async def answer_callback(callback_id, text=""):
139
  params = {
 
121
  params["reply_markup"] = json.dumps(reply_markup)
122
  return await bale_request("sendMessage", params)
123
 
124
+ import httpx # add near the top imports
125
+
126
  async def send_document_file(chat_id, file_path, caption=""):
127
+ """Upload a local file to Bale using sendDocument (httpx, multipart)."""
128
  url = BALE_API + "sendDocument"
129
+ data = {"chat_id": str(chat_id)}
130
+ if caption:
131
+ data["caption"] = caption
132
+
133
+ with open(file_path, "rb") as f:
134
+ files = {"document": f} # identical to the working TG bot
135
+ async with httpx.AsyncClient() as client:
136
+ resp = await client.post(url, data=data, files=files, timeout=60)
137
+ if resp.status_code != 200:
138
+ logger.error(f"❌ Bale upload failed: {resp.status_code} {resp.text}")
139
+ return {"ok": False, "description": resp.text}
140
+ result = resp.json()
141
+ logger.info(f"📨 sendDocument (file) response: {result}")
142
+ return result
143
 
144
  async def answer_callback(callback_id, text=""):
145
  params = {