Update main.py
Browse files
main.py
CHANGED
|
@@ -1,78 +1,71 @@
|
|
| 1 |
-
import logging
|
| 2 |
-
import asyncio
|
| 3 |
-
from telegram import Update
|
| 4 |
-
from telegram.ext import ApplicationBuilder, ContextTypes, MessageHandler, filters
|
| 5 |
-
from telegram.request import HTTPXRequest
|
| 6 |
-
|
| 7 |
-
# --- تنظیمات ---
|
| 8 |
-
TOKEN = '5700240160:AAFbRWWQMohMWSxzj7VKyC46IyJVtu89CQc'
|
| 9 |
-
CHAT_ID = '@medical_cen_archive'
|
| 10 |
-
|
| 11 |
-
# تنظیمات لاگ
|
| 12 |
-
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
|
| 13 |
-
|
| 14 |
-
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 15 |
-
if not update.message or not update.message.text:
|
| 16 |
-
return
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
line
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
explanation_text
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
application = ApplicationBuilder().token(TOKEN).request(t_request).build()
|
| 73 |
-
|
| 74 |
-
msg_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), handle_message)
|
| 75 |
-
application.add_handler(msg_handler)
|
| 76 |
-
|
| 77 |
-
# اجرای مداوم ربات
|
| 78 |
-
application.run_polling(drop_pending_updates=True)
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import asyncio
|
| 3 |
+
from telegram import Update
|
| 4 |
+
from telegram.ext import ApplicationBuilder, ContextTypes, MessageHandler, filters
|
| 5 |
+
from telegram.request import HTTPXRequest
|
| 6 |
+
|
| 7 |
+
# --- تنظیمات ---
|
| 8 |
+
TOKEN = '5700240160:AAFbRWWQMohMWSxzj7VKyC46IyJVtu89CQc'
|
| 9 |
+
CHAT_ID = '@medical_cen_archive'
|
| 10 |
+
|
| 11 |
+
# تنظیمات لاگ
|
| 12 |
+
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
|
| 13 |
+
|
| 14 |
+
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 15 |
+
if not update.message or not update.message.text:
|
| 16 |
+
return
|
| 17 |
+
|
| 18 |
+
lines = update.message.text.split('\n')
|
| 19 |
+
sent_count = 0
|
| 20 |
+
error_count = 0
|
| 21 |
+
|
| 22 |
+
for line in lines:
|
| 23 |
+
line = line.strip()
|
| 24 |
+
if not line or line.count('*') < 2:
|
| 25 |
+
continue
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
parts = line.split('*')
|
| 29 |
+
question_text = parts[0].strip()
|
| 30 |
+
options = [opt.strip() for opt in parts[1].split(',')]
|
| 31 |
+
correct_id = int(parts[2].strip())
|
| 32 |
+
explanation_text = parts[3].strip() if len(parts) >= 4 else None
|
| 33 |
+
|
| 34 |
+
if explanation_text and len(explanation_text) > 200:
|
| 35 |
+
explanation_text = explanation_text[:197] + "..."
|
| 36 |
+
|
| 37 |
+
await context.bot.send_poll(
|
| 38 |
+
chat_id=CHAT_ID,
|
| 39 |
+
question=question_text,
|
| 40 |
+
options=options,
|
| 41 |
+
type='quiz',
|
| 42 |
+
correct_option_id=correct_id,
|
| 43 |
+
explanation=explanation_text,
|
| 44 |
+
is_anonymous=True
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
sent_count += 1
|
| 48 |
+
await asyncio.sleep(2.0) # افزایش زمان انتظار برای امنیت بیشتر
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logging.error(f"Error processing line: {e}")
|
| 52 |
+
error_count += 1
|
| 53 |
+
|
| 54 |
+
if sent_count > 0:
|
| 55 |
+
await update.message.reply_text(f"✅ تعداد {sent_count} سوال ارسال شد.")
|
| 56 |
+
if error_count > 0:
|
| 57 |
+
await update.message.reply_text(f"⚠️ تعداد {error_count} خط با خطا مواجه شد.")
|
| 58 |
+
|
| 59 |
+
if __name__ == '__main__':
|
| 60 |
+
print("--- Bot is LIVE and Ready ---")
|
| 61 |
+
|
| 62 |
+
# استفاده از تنظیمات بهینه برای جلوگیری از ارورهای شبکه هاست
|
| 63 |
+
t_request = HTTPXRequest(connect_timeout=20, read_timeout=20)
|
| 64 |
+
|
| 65 |
+
application = ApplicationBuilder().token(TOKEN).request(t_request).build()
|
| 66 |
+
|
| 67 |
+
msg_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), handle_message)
|
| 68 |
+
application.add_handler(msg_handler)
|
| 69 |
+
|
| 70 |
+
# اجرای ربات
|
| 71 |
+
application.run_polling(drop_pending_updates=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|