File size: 3,908 Bytes
a9459ec de6f088 a9459ec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 73 74 75 76 77 78 79 80 81 82 83 84 | import telebot
from telebot import types
import pytesseract
from PIL import Image
from docx import Document
from fpdf import FPDF
import arabic_reshaper
from bidi.algorithm import get_display
import fitz # PyMuPDF
import os
import re
# إعدادات البوت - التوكن الخاص بك
API_TOKEN ='8347829472:AAFFQH1JROEUUXHhRyMzoq_HRx6sBODF0lk'
bot = telebot.TeleBot(API_TOKEN)
# مخزن مؤقت لبيانات المستخدمين
user_data = {}
def get_arabic_pdf_text(text):
reshaped_text = arabic_reshaper.reshape(text)
return get_display(reshaped_text)
def create_main_markup():
markup = types.InlineKeyboardMarkup(row_width=2)
markup.add(types.InlineKeyboardButton("🧹 تنظيف الفراغات", callback_data="clean"),
types.InlineKeyboardButton("🔁 حذف التكرار", callback_data="dedup"))
markup.add(types.InlineKeyboardButton("🤖 تلخيص النص", callback_data="summarize"),
types.InlineKeyboardButton("📄 تصدير Word", callback_data="export_word"))
markup.add(types.InlineKeyboardButton("📕 تصدير PDF", callback_data="export_pdf"),
types.InlineKeyboardButton("🗑️ حذف البيانات", callback_data="delete"))
return markup
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, "مرحباً بك! أرسل لي نصاً، صورة، أو ملف PDF وسأقوم بمعالجته لك مجاناً 24/7.")
@bot.message_handler(content_types=['text', 'photo', 'document'])
def handle_input(message):
chat_id = message.chat.id
text_content = ""
try:
msg = bot.reply_to(message, "⏳ جاري معالجة طلبك...")
if message.content_type == 'text':
text_content = message.text
elif message.content_type == 'photo':
file_info = bot.get_file(message.photo[-1].file_id)
downloaded_file = bot.download_file(file_info.file_path)
with open("temp.jpg", "wb") as f: f.write(downloaded_file)
text_content = pytesseract.image_to_string(Image.open("temp.jpg"), lang='ara+eng')
os.remove("temp.jpg")
elif message.content_type == 'document' and message.document.file_name.lower().endswith('.pdf'):
file_info = bot.get_file(message.document.file_id)
downloaded_file = bot.download_file(file_info.file_path)
with open("temp.pdf", "wb") as f: f.write(downloaded_file)
with fitz.open("temp.pdf") as doc:
for page in doc: text_content += page.get_text()
os.remove("temp.pdf")
if text_content.strip():
user_data[chat_id] = text_content
bot.edit_message_text(f"✅ تم استخراج النص بنجاح ({len(text_content)} حرف).", chat_id, msg.message_id, reply_markup=create_main_markup())
else:
bot.edit_message_text("❌ لم أجد نصاً واضحاً.", chat_id, msg.message_id)
except Exception as e:
bot.send_message(chat_id, f"حدث خطأ: {str(e)}")
@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
chat_id = call.message.chat.id
if chat_id not in user_data: return
text = user_data[chat_id]
if call.data == "clean":
user_data[chat_id] = " ".join(text.split())
bot.edit_message_text("✅ تم تنظيف النص.", chat_id, call.message.message_id, reply_markup=create_main_markup())
elif call.data == "export_word":
doc = Document(); doc.add_paragraph(text); path = f"out_{chat_id}.docx"; doc.save(path)
with open(path, 'rb') as f: bot.send_document(chat_id, f); os.remove(path)
elif call.data == "delete":
del user_data[chat_id]
bot.edit_message_text("🗑️ تم حذف البيانات.", chat_id, call.message.message_id)
bot.polling(none_stop=True)
|