baha-99 commited on
Commit
e5e2af5
Β·
1 Parent(s): dcf0e08

fix: the file download error is fixed

Browse files
Files changed (1) hide show
  1. bot_telegram.py +49 -36
bot_telegram.py CHANGED
@@ -95,7 +95,8 @@ class TelegramBot:
95
  "You can:\n"
96
  "1. Send me any question as text\n"
97
  "2. Send me an Excel file with questions (must have a 'question' column in 'rfp' sheet)\n\n"
98
- "Note: Excel files must contain no more than 50 questions."
 
99
  )
100
  else:
101
  AWAITING_PASSWORD.add(user_id)
@@ -210,7 +211,7 @@ class TelegramBot:
210
  asyncio.create_task(self._process_excel_file(update, context, message_id))
211
 
212
  async def _process_excel_file(self, update, context, message_id):
213
- """Process Excel file with progress updates but no timeout"""
214
  try:
215
  document = update.message.document
216
 
@@ -218,53 +219,65 @@ class TelegramBot:
218
  processing_msg = await update.message.reply_text(
219
  f"πŸ“Š Starting Excel processing...\n"
220
  f"File: {document.file_name}\n"
221
- f"Request ID: #{message_id}\n"
222
- f"Processing time may vary depending on file size and complexity. type /status to check the status of the request."
223
  )
224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
  self.active_excel_files[message_id] = {
226
  'filename': document.file_name,
227
  'status': 'processing',
228
  'start_time': time.time()
229
  }
 
 
 
 
 
230
 
231
- # Process in thread pool without timeout
232
- try:
233
- # Start a background task to update progress message
234
- asyncio.create_task(self._update_progress(processing_msg, message_id, document.file_name))
235
-
236
- result = await asyncio.get_event_loop().run_in_executor(
237
- self.excel_executor,
238
- self._process_excel_sync,
239
- file_bytes,
240
- document.file_name
241
- )
242
-
243
- if result is None:
244
- await processing_msg.edit_text(
245
- f"❌ Failed to process file\n"
246
- f"File: {document.file_name}\n"
247
- f"Please check the file format and try again."
248
- )
249
- return
250
-
251
- # Send processed file
252
- await context.bot.send_document(
253
- chat_id=update.effective_chat.id,
254
- document=io.BytesIO(result),
255
- filename=f'processed_{document.file_name}',
256
- caption=f"βœ… Excel processing completed!\nRequest ID: #{message_id}"
257
- )
258
- await processing_msg.delete()
259
 
260
- except Exception as e:
261
- logging.error(f"Error processing file: {e}")
262
  await processing_msg.edit_text(
263
- f"❌ Error processing file\n"
264
  f"File: {document.file_name}\n"
265
- f"Error: {str(e)}"
266
  )
 
 
 
 
 
 
 
 
 
 
267
 
 
 
 
 
 
 
 
268
  finally:
269
  if message_id in self.active_excel_files:
270
  self.active_excel_files[message_id]['status'] = 'completed'
 
95
  "You can:\n"
96
  "1. Send me any question as text\n"
97
  "2. Send me an Excel file with questions (must have a 'question' column in 'rfp' sheet)\n\n"
98
+ "Note: Excel files must contain no more than 50 questions.\n\n"
99
+ "type '/status' to check the status of the request"
100
  )
101
  else:
102
  AWAITING_PASSWORD.add(user_id)
 
211
  asyncio.create_task(self._process_excel_file(update, context, message_id))
212
 
213
  async def _process_excel_file(self, update, context, message_id):
214
+ """Process Excel file with progress updates"""
215
  try:
216
  document = update.message.document
217
 
 
219
  processing_msg = await update.message.reply_text(
220
  f"πŸ“Š Starting Excel processing...\n"
221
  f"File: {document.file_name}\n"
222
+ f"Request ID: #{message_id}"
 
223
  )
224
 
225
+ # Step 1: Download file - ADD THIS PART
226
+ try:
227
+ file = await context.bot.get_file(document.file_id)
228
+ file_bytes = await file.download_as_bytearray() # Define file_bytes here
229
+ except Exception as e:
230
+ logging.error(f"Error downloading file: {e}")
231
+ await processing_msg.edit_text(
232
+ f"❌ Error downloading file: {document.file_name}\n"
233
+ f"Please try again or contact support."
234
+ )
235
+ return
236
+
237
+ # Step 2: Update status to processing
238
  self.active_excel_files[message_id] = {
239
  'filename': document.file_name,
240
  'status': 'processing',
241
  'start_time': time.time()
242
  }
243
+ await processing_msg.edit_text(
244
+ f"βš™οΈ Processing Excel file...\n"
245
+ f"File: {document.file_name}\n"
246
+ f"Request ID: #{message_id}"
247
+ )
248
 
249
+ # Step 3: Process in thread pool
250
+ result = await asyncio.get_event_loop().run_in_executor(
251
+ self.excel_executor,
252
+ self._process_excel_sync,
253
+ file_bytes, # Now file_bytes is defined
254
+ document.file_name
255
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
 
257
+ if result is None:
 
258
  await processing_msg.edit_text(
259
+ f"❌ Failed to process file\n"
260
  f"File: {document.file_name}\n"
261
+ f"Please check the file format and try again."
262
  )
263
+ return
264
+
265
+ # Send processed file
266
+ await context.bot.send_document(
267
+ chat_id=update.effective_chat.id,
268
+ document=io.BytesIO(result),
269
+ filename=f'processed_{document.file_name}',
270
+ caption=f"βœ… Excel processing completed!\nRequest ID: #{message_id}"
271
+ )
272
+ await processing_msg.delete()
273
 
274
+ except Exception as e:
275
+ logging.error(f"Error processing file: {e}")
276
+ await processing_msg.edit_text(
277
+ f"❌ Error processing file\n"
278
+ f"File: {document.file_name}\n"
279
+ f"Error: {str(e)}"
280
+ )
281
  finally:
282
  if message_id in self.active_excel_files:
283
  self.active_excel_files[message_id]['status'] = 'completed'