baha-99 commited on
Commit
59e23b7
·
1 Parent(s): ae25181

feat: now bot can read csv files along side with xls and xlsx

Browse files
Files changed (1) hide show
  1. bot_telegram.py +51 -7
bot_telegram.py CHANGED
@@ -99,7 +99,7 @@ class TelegramBot:
99
  "You can :\n"
100
  "1. Send me any question as text\n"
101
  "2. Send me an Excel file with questions (must have a 'question' column in 'rfp' sheet)\n\n"
102
- "Note: Excel files must contain no more than 1000 questions.\n\n"
103
  "type '/status' to check the status of the request"
104
  )
105
  else:
@@ -138,7 +138,7 @@ class TelegramBot:
138
  "You can:\n"
139
  "1. Send me any question as text\n"
140
  "2. Send me an Excel file with questions (must have a 'question' column in 'rfp' sheet)\n\n"
141
- "Note: Excel files must contain no more than 1000 questions."
142
  )
143
  else:
144
  await update.message.reply_text("❌ Wrong password. Try again.")
@@ -218,17 +218,46 @@ class TelegramBot:
218
 
219
  async def handle_excel(self, update: Update, context: CallbackContext):
220
  """Handle Excel files concurrently"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  try:
222
  document = update.message.document
223
  message_id = update.message.message_id
 
224
 
225
- # İlk olarak Excel'i oku ve soru sayısını hesapla
 
226
  file = await context.bot.get_file(document.file_id)
227
  file_bytes = await file.download_as_bytearray()
 
228
 
229
  # Excel'i oku ve soru sayısını hesapla
230
  try:
231
- df = pd.read_excel(io.BytesIO(file_bytes), sheet_name='rfp')
 
 
 
 
 
 
 
 
 
 
 
232
  # Add debug logging
233
  logging.info(f"Excel columns found: {df.columns.tolist()}")
234
 
@@ -260,15 +289,25 @@ class TelegramBot:
260
  #print("debug 1")
261
  #print("FILE_RFP_EXCEL_COUNT: ",FILE_RFP_EXCEL_COUNT)
262
  #print("type: ",type(FILE_RFP_EXCEL_COUNT))
263
- if df['question'].count() > FILE_RFP_EXCEL_COUNT:
 
 
 
 
 
 
264
  await update.message.reply_text(
265
  "❌ Error: Too many questions in Excel file!\n"
266
- f"Your file has {df['question'].count()} questions.\n"
267
  f"Maximum allowed is {FILE_RFP_EXCEL_COUNT} questions.\n"
268
  "Please reduce the number of questions and try again."
269
  )
 
270
  return
271
 
 
 
 
272
  num_questions = len(df['question'])
273
 
274
  # Tahmini süreyi hesapla
@@ -437,11 +476,16 @@ class TelegramBot:
437
 
438
  def setup_handlers(self):
439
  """Set up Telegram command and message handlers."""
 
440
  self.app.add_handler(CommandHandler("start", self.start_command))
441
  self.app.add_handler(CommandHandler("status", self.status_command))
442
  self.app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_message))
 
 
443
  self.app.add_handler(MessageHandler(
444
- filters.Document.FileExtension("xlsx") | filters.Document.FileExtension("xls"),
 
 
445
  self.handle_excel
446
  ))
447
 
 
99
  "You can :\n"
100
  "1. Send me any question as text\n"
101
  "2. Send me an Excel file with questions (must have a 'question' column in 'rfp' sheet)\n\n"
102
+ "Note: Excel files must contain no more than 200 questions.\n\n"
103
  "type '/status' to check the status of the request"
104
  )
105
  else:
 
138
  "You can:\n"
139
  "1. Send me any question as text\n"
140
  "2. Send me an Excel file with questions (must have a 'question' column in 'rfp' sheet)\n\n"
141
+ "Note: Excel files must contain no more than 200 questions."
142
  )
143
  else:
144
  await update.message.reply_text("❌ Wrong password. Try again.")
 
218
 
219
  async def handle_excel(self, update: Update, context: CallbackContext):
220
  """Handle Excel files concurrently"""
221
+ logging.info("=== Starting handle_excel function ===")
222
+
223
+ # Add authentication check
224
+ user_id = update.message.from_user.id
225
+ if user_id not in AUTHENTICATED_USERS:
226
+ logging.info(f"Unauthorized access attempt from user {user_id}")
227
+ await update.message.reply_text(
228
+ "❌ You are not authenticated.\n"
229
+ "Please type /start to authenticate and enter the password first."
230
+ )
231
+ return
232
+
233
+ logging.info(f"Authenticated user {user_id} uploaded file: {update.message.document.file_name}")
234
+ logging.info(f"Received file: {update.message.document.file_name}")
235
+
236
  try:
237
  document = update.message.document
238
  message_id = update.message.message_id
239
+ logging.info(f"Processing document with ID: {message_id}")
240
 
241
+ # First download the file
242
+ logging.info("Downloading file...")
243
  file = await context.bot.get_file(document.file_id)
244
  file_bytes = await file.download_as_bytearray()
245
+ logging.info("File downloaded successfully")
246
 
247
  # Excel'i oku ve soru sayısını hesapla
248
  try:
249
+ logging.info(f"Starting to read file: {document.file_name}")
250
+
251
+ # Check file extension
252
+ if document.file_name.lower().endswith('.csv'):
253
+ df = pd.read_csv(io.BytesIO(file_bytes))
254
+ logging.info("Reading as CSV file")
255
+ else:
256
+ df = pd.read_excel(io.BytesIO(file_bytes), sheet_name='rfp')
257
+ logging.info("Reading as Excel file")
258
+
259
+ logging.info(f"File read successfully. Columns found: {df.columns.tolist()}")
260
+
261
  # Add debug logging
262
  logging.info(f"Excel columns found: {df.columns.tolist()}")
263
 
 
289
  #print("debug 1")
290
  #print("FILE_RFP_EXCEL_COUNT: ",FILE_RFP_EXCEL_COUNT)
291
  #print("type: ",type(FILE_RFP_EXCEL_COUNT))
292
+ # Add debug logging before the check
293
+ question_count = df['question'].count()
294
+ logging.info(f"Number of questions found: {question_count}")
295
+ logging.info(f"Question limit (FILE_RFP_EXCEL_COUNT): {FILE_RFP_EXCEL_COUNT}")
296
+
297
+ if question_count > FILE_RFP_EXCEL_COUNT:
298
+ logging.info(f"Exceeded question limit: {question_count} > {FILE_RFP_EXCEL_COUNT}")
299
  await update.message.reply_text(
300
  "❌ Error: Too many questions in Excel file!\n"
301
+ f"Your file has {question_count} questions.\n"
302
  f"Maximum allowed is {FILE_RFP_EXCEL_COUNT} questions.\n"
303
  "Please reduce the number of questions and try again."
304
  )
305
+ logging.info("Sent error message to user about exceeding question limit")
306
  return
307
 
308
+ # If we get here, the question count is okay
309
+ logging.info("Question count is within limits, proceeding with processing")
310
+
311
  num_questions = len(df['question'])
312
 
313
  # Tahmini süreyi hesapla
 
476
 
477
  def setup_handlers(self):
478
  """Set up Telegram command and message handlers."""
479
+ logging.info("Setting up message handlers...")
480
  self.app.add_handler(CommandHandler("start", self.start_command))
481
  self.app.add_handler(CommandHandler("status", self.status_command))
482
  self.app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_message))
483
+
484
+ # Update Excel handler to include CSV files
485
  self.app.add_handler(MessageHandler(
486
+ filters.Document.FileExtension("xlsx") |
487
+ filters.Document.FileExtension("xls") |
488
+ filters.Document.FileExtension("csv"), # Add CSV support
489
  self.handle_excel
490
  ))
491