Upload main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|