Hashim-dotcom commited on
Commit
3ae27c8
·
verified ·
1 Parent(s): b438356

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +13 -6
bot.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import sys
3
  import telegram
@@ -60,7 +61,9 @@ async def handle_audio(update, context):
60
  file_id = update.message.voice.file_id
61
  elif update.message.audio:
62
  file_id = update.message.audio.file_id
63
- else: return
 
 
64
  audio_file = await context.bot.get_file(file_id)
65
  input_path = f"{file_id}_input"
66
  await audio_file.download_to_drive(input_path)
@@ -70,12 +73,15 @@ async def handle_audio(update, context):
70
  sf.write(output_path, output_audio[1], output_audio[0], format='WAV')
71
 
72
  await update.message.reply_audio(audio=open(output_path, 'rb'), title="صوت محول")
 
73
  except Exception as e:
74
  logger.error(f"حدث خطأ أثناء المعالجة: {e}")
75
  await update.message.reply_text(f"عذراً، حدث خطأ: {str(e)}")
76
  finally:
77
- if 'input_path' in locals() and os.path.exists(input_path): os.remove(input_path)
78
- if 'output_path' in locals() and os.path.exists(output_path): os.remove(output_path)
 
 
79
 
80
  async def main():
81
  TOKEN = os.getenv("TELEGRAM_TOKEN")
@@ -83,7 +89,6 @@ async def main():
83
  logger.critical("خطأ: لم يتم العثور على TELEGRAM_TOKEN.")
84
  return
85
 
86
- # === هذا هو الجزء الذي تم إصلاحه ليتوافق مع Dockerfile ===
87
  request = HTTPXRequest(http_version="1.1")
88
  application = (
89
  Application.builder()
@@ -92,15 +97,17 @@ async def main():
92
  .rate_limiter(AIORateLimiter())
93
  .build()
94
  )
95
- # =======================================================
96
 
97
  application.add_handler(CommandHandler("start", start))
98
  application.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, handle_audio))
 
99
  logger.info("البوت قيد التشغيل...")
100
  await application.initialize()
101
  await application.updater.start_polling()
102
  await application.start()
103
- while True: await asyncio.sleep(3600)
 
 
104
 
105
  if __name__ == "__main__":
106
  asyncio.run(main())
 
1
+ # Final version - 5 August
2
  import os
3
  import sys
4
  import telegram
 
61
  file_id = update.message.voice.file_id
62
  elif update.message.audio:
63
  file_id = update.message.audio.file_id
64
+ else:
65
+ return
66
+
67
  audio_file = await context.bot.get_file(file_id)
68
  input_path = f"{file_id}_input"
69
  await audio_file.download_to_drive(input_path)
 
73
  sf.write(output_path, output_audio[1], output_audio[0], format='WAV')
74
 
75
  await update.message.reply_audio(audio=open(output_path, 'rb'), title="صوت محول")
76
+
77
  except Exception as e:
78
  logger.error(f"حدث خطأ أثناء المعالجة: {e}")
79
  await update.message.reply_text(f"عذراً، حدث خطأ: {str(e)}")
80
  finally:
81
+ if 'input_path' in locals() and os.path.exists(input_path):
82
+ os.remove(input_path)
83
+ if 'output_path' in locals() and os.path.exists(output_path):
84
+ os.remove(output_path)
85
 
86
  async def main():
87
  TOKEN = os.getenv("TELEGRAM_TOKEN")
 
89
  logger.critical("خطأ: لم يتم العثور على TELEGRAM_TOKEN.")
90
  return
91
 
 
92
  request = HTTPXRequest(http_version="1.1")
93
  application = (
94
  Application.builder()
 
97
  .rate_limiter(AIORateLimiter())
98
  .build()
99
  )
 
100
 
101
  application.add_handler(CommandHandler("start", start))
102
  application.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, handle_audio))
103
+
104
  logger.info("البوت قيد التشغيل...")
105
  await application.initialize()
106
  await application.updater.start_polling()
107
  await application.start()
108
+
109
+ while True:
110
+ await asyncio.sleep(3600)
111
 
112
  if __name__ == "__main__":
113
  asyncio.run(main())