Spaces:
Runtime error

File size: 5,050 Bytes
354d4ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import telebot
import cloudscraper
import re
import base64
import requests
import json
from telebot import types

# --- Configuration ---
API_TOKEN = '8478249874:AAE5y8yx_zxiMd6SPFmZp662Jr0tVr_4L8c'
OCR_API_TOKEN = 'ed0002c02e888376f59dccb780e958e7'
ADMIN_ID = 993191413  # 🔔 ضع هنا الـ Chat ID الخاص بك لتصلك الإشعارات

bot = telebot.TeleBot(API_TOKEN)
scraper = cloudscraper.create_scraper()

# --- Helper Functions ---

def format_iid(iid):
    clean = re.sub(r'\D', '', iid)
    return "-".join(clean[i:i+7] for i in range(0, len(clean), 7))

def format_cid(cid):
    clean = re.sub(r'\D', '', cid)
    return "-".join(clean[i:i+6] for i in range(0, len(clean), 6))

def send_admin_log(user_info, user_input, bot_output):
    """إرسال تقرير مفصل للأدمن بكل عملية"""
    log_msg = (
        "👤 <b>New User Activity!</b>\n"
        f"<b>User:</b> {user_info.first_name} (@{user_info.username})\n"
        f"<b>ID:</b> <code>{user_info.id}</code>\n\n"
        f"📩 <b>User Sent:</b>\n<code>{user_input}</code>\n\n"
        f"🤖 <b>Bot Replied:</b>\n<code>{bot_output}</code>"
    )
    try:
        bot.send_message(ADMIN_ID, log_msg, parse_mode='HTML')
    except:
        pass

def get_cid_logic(iid):
    url = "https://getcid.online/api/cid"
    headers = {'Content-Type': 'application/json'}
    payload = {"iid": re.sub(r'\D', '', iid)}
    try:
        response = scraper.post(url, json=payload, headers=headers, timeout=30)
        if response.status_code == 200:
            res_json = response.json()
            if "cid" in res_json:
                return res_json['cid'], True
            return res_json.get("message", "Invalid IID"), False
        return "Server Error", False
    except:
        return "Timeout", False

def process_ocr(photo_bytes):
    url = "http://api.get-cid.com/api/ocr"
    base64_data = base64.b64encode(photo_bytes).decode('utf-8')
    payload = {"token": OCR_API_TOKEN, "mime_type": "image/jpeg", "image_base64": base64_data}
    try:
        response = requests.post(url, json=payload, timeout=30)
        if response.status_code == 200:
            res_data = response.json()
            raw_text = res_data.get("extracted_text", "")
            if raw_text:
                clean = re.sub(r'\D', '', raw_text)
                if len(clean) >= 48: return clean, True
        return None, False
    except:
        return None, False

# --- Telegram Handlers ---

@bot.message_handler(commands=['start'])
def start(message):
    bot.reply_to(message, "🛡️ <b>Microserial PRO</b>\nSend IID Photo or Text.", parse_mode='HTML')

@bot.message_handler(content_types=['photo'])
def handle_photo(message):
    status_msg = bot.reply_to(message, "📸 <b>Reading Image...</b>", parse_mode='HTML')
    file_info = bot.get_file(message.photo[-1].file_id)
    downloaded_file = bot.download_file(file_info.file_path)
    
    iid_text, ocr_success = process_ocr(downloaded_file)
    
    if ocr_success:
        cid_raw, cid_success = get_cid_logic(iid_text)
        if cid_success:
            show_final_result(message, status_msg.message_id, iid_text, cid_raw, "Image OCR")
        else:
            bot.edit_message_text(f"❌ Error: {cid_raw}", chat_id=message.chat.id, message_id=status_msg.message_id)
    else:
        bot.edit_message_text("❌ OCR Failed!", chat_id=message.chat.id, message_id=status_msg.message_id)

@bot.message_handler(func=lambda message: True)
def handle_text(message):
    clean_iid = re.sub(r'\D', '', message.text)
    if len(clean_iid) >= 48:
        status_msg = bot.reply_to(message, "⏳ <b>Processing...</b>", parse_mode='HTML')
        cid_raw, success = get_cid_logic(clean_iid)
        if success:
            show_final_result(message, status_msg.message_id, clean_iid, cid_raw, "Manual Text")
        else:
            bot.edit_message_text(f"❌ Error: {cid_raw}", chat_id=message.chat.id, message_id=status_msg.message_id)

def show_final_result(message, message_id, iid, cid, input_type):
    f_iid = format_iid(iid)
    f_cid = format_cid(cid)
    
    # 📝 نص الرسالة النهائي للعميل مع جملة التوثيق
    final_res = (
        "✅ <b>Process Completed!</b>\n\n"
        "<b>IID:</b>\n"
        f"<code>{f_iid}</code>\n\n"
        "<b>Confirmation ID (CID):</b>\n"
        f"<code>{f_cid}</code>\n\n"
        "<b>Get From Microsoft Database By Microserial</b>"
    )
    
    markup = types.InlineKeyboardMarkup()
    
    bot.edit_message_text(final_res, chat_id=message.chat.id, message_id=message_id, parse_mode='HTML', reply_markup=markup)
    
    # 🔔 إرسال الإشعار للأدمن (أنت)
    send_admin_log(message.from_user, f"Input: {input_type} | IID: {f_iid}", f"CID: {f_cid}")

if __name__ == "__main__":
    print("🚀 Microserial PRO (Admin Version) is Live!")
    bot.infinity_polling(skip_pending=True)