|
|
import logging
|
|
|
import asyncio
|
|
|
from telegram import Update
|
|
|
from telegram.ext import ApplicationBuilder, ContextTypes, MessageHandler, filters
|
|
|
from telegram.request import HTTPXRequest
|
|
|
|
|
|
|
|
|
TOKEN = '5700240160:AAFbRWWQMohMWSxzj7VKyC46IyJVtu89CQc'
|
|
|
CHAT_ID = '@medical_cen_archive'
|
|
|
|
|
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
|
|
|
|
|
|
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
|
if not update.message or not update.message.text:
|
|
|
return
|
|
|
|
|
|
|
|
|
lines = update.message.text.split('\n')
|
|
|
sent_count = 0
|
|
|
error_count = 0
|
|
|
|
|
|
for line in lines:
|
|
|
line = line.strip()
|
|
|
|
|
|
if not line or line.count('*') < 2:
|
|
|
continue
|
|
|
|
|
|
try:
|
|
|
parts = line.split('*')
|
|
|
question_text = parts[0].strip()
|
|
|
options = [opt.strip() for opt in parts[1].split(',')]
|
|
|
correct_id = int(parts[2].strip())
|
|
|
explanation_text = parts[3].strip() if len(parts) >= 4 else None
|
|
|
|
|
|
|
|
|
if explanation_text and len(explanation_text) > 200:
|
|
|
explanation_text = explanation_text[:197] + "..."
|
|
|
|
|
|
|
|
|
await context.bot.send_poll(
|
|
|
chat_id=CHAT_ID,
|
|
|
question=question_text,
|
|
|
options=options,
|
|
|
type='quiz',
|
|
|
correct_option_id=correct_id,
|
|
|
explanation=explanation_text,
|
|
|
is_anonymous=True
|
|
|
)
|
|
|
|
|
|
sent_count += 1
|
|
|
|
|
|
await asyncio.sleep(1.5)
|
|
|
|
|
|
except Exception as e:
|
|
|
logging.error(f"Error in line: {line[:30]}... -> {e}")
|
|
|
error_count += 1
|
|
|
|
|
|
|
|
|
if sent_count > 0:
|
|
|
await update.message.reply_text(f"✅ تعداد {sent_count} سوال با موفقیت ارسال شد.")
|
|
|
if error_count > 0:
|
|
|
await update.message.reply_text(f"⚠️ تعداد {error_count} خط با خطا مواجه شد (فرمت اشتباه).")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
print("--- Bot is LIVE and Ready ---")
|
|
|
print(f"Target Channel: {CHAT_ID}")
|
|
|
|
|
|
|
|
|
t_request = HTTPXRequest(connect_timeout=60, read_timeout=60)
|
|
|
|
|
|
application = ApplicationBuilder().token(TOKEN).request(t_request).build()
|
|
|
|
|
|
msg_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), handle_message)
|
|
|
application.add_handler(msg_handler)
|
|
|
|
|
|
|
|
|
application.run_polling(drop_pending_updates=True)
|
|
|
|