Spaces:
Runtime error

microtrest / bot.py
Hammad2626's picture
Upload 2 files
354d4ec
Raw History Blame Contribute Delete
5.05 kB
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)