kim0arka commited on
Commit
bc9c28f
·
verified ·
1 Parent(s): ab14233

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +71 -78
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
- lines = update.message.text.split('\n')
20
- sent_count = 0
21
- error_count = 0
22
-
23
- for line in lines:
24
- line = line.strip()
25
- # بررسی اینکه خط خالی نباشد و حداقل ۳ بخش داشته باشد
26
- if not line or line.count('*') < 2:
27
- continue
28
-
29
- try:
30
- parts = line.split('*')
31
- question_text = parts[0].strip()
32
- options = [opt.strip() for opt in parts[1].split(',')]
33
- correct_id = int(parts[2].strip())
34
- explanation_text = parts[3].strip() if len(parts) >= 4 else None
35
-
36
- # رعایت محدودیت ۲۰۰ کاراکتری تلگرام برای توضیح
37
- if explanation_text and len(explanation_text) > 200:
38
- explanation_text = explanation_text[:197] + "..."
39
-
40
- # ارسال به کانال
41
- await context.bot.send_poll(
42
- chat_id=CHAT_ID,
43
- question=question_text,
44
- options=options,
45
- type='quiz',
46
- correct_option_id=correct_id,
47
- explanation=explanation_text,
48
- is_anonymous=True
49
- )
50
-
51
- sent_count += 1
52
- # یک وقفه کوتاه بین ارسال‌ها برای جلوگیری از ارور اسپم تلگرام
53
- await asyncio.sleep(1.5)
54
-
55
- except Exception as e:
56
- logging.error(f"Error in line: {line[:30]}... -> {e}")
57
- error_count += 1
58
-
59
- # فرستادن گزارش نهایی به خودت در ربات
60
- if sent_count > 0:
61
- await update.message.reply_text(f"✅ تعداد {sent_count} سوال با موفقیت ارسال شد.")
62
- if error_count > 0:
63
- await update.message.reply_text(f"⚠️ تعداد {error_count} خط با خطا مواجه شد (فرمت اشتباه).")
64
-
65
- if __name__ == '__main__':
66
- print("--- Bot is LIVE and Ready ---")
67
- print(f"Target Channel: {CHAT_ID}")
68
-
69
- # تنظیم تایم‌اوت بسیار بالا برای پایداری در برابر نوسان اینترنت
70
- t_request = HTTPXRequest(connect_timeout=60, read_timeout=60)
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)