File size: 8,193 Bytes
3a3b605 | 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | """نظام الهمسات السرية (همسه) — مطابق تماماً للتصميم المطلوب:
1) في الجروب: رد على الشخص واكتب (همسه) -> يظهر زر تحويل للخاص (• اكتب همستك السرية هنا •)
2) في الخاص: يكتب الهمسة -> يسأله البوت هل تريد إرسالها (✅ نعم / ❌ إلغاء)
3) في الجروب: تظهر رسالة الهمسة بزر (🔐 عرض الهمسة) ولا يراها إلا المستلم أو المرسل أو صاحب البوت فقط."""
import secrets
import time
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CallbackQueryHandler
from core.kb import btn
from core.router import exact, prefix
from core import ranks as R
from core.store import find_user_by_username
# الجلسات والهمسات في الذاكرة
_PENDING_LINKS = {} # token -> {chat_id, from_id, from_name, to_id, to_name, ts}
_ACTIVE_PM = {} # user_id -> {token, chat_id, from_id, from_name, to_id, to_name, text}
_WHISPERS = {} # wid -> {from_id, from_name, to_id, to_name, text, ts}
def _cleanup():
now = time.time()
for d in (_PENDING_LINKS, _WHISPERS):
stale = [k for k, v in d.items() if now - v.get("ts", 0) > 86400]
for k in stale:
d.pop(k, None)
async def _start_whisper(update, ctx, target_id, target_name):
_cleanup()
me = await ctx.bot.get_me()
token = secrets.token_urlsafe(8)
_PENDING_LINKS[token] = {
"chat_id": update.effective_chat.id,
"from_id": update.effective_user.id,
"from_name": update.effective_user.first_name,
"to_id": target_id,
"to_name": target_name,
"ts": time.time(),
}
url = f"https://t.me/{me.username}?start=wh_{token}"
kb = InlineKeyboardMarkup([[InlineKeyboardButton("• اكتب همستك السرية هنا •", url=url)]])
await update.message.reply_text(
f"✧︙تم تحديد الهمسه الى {target_name}\n\n✧︙اضغط علئ الزر لارسال الهمسه",
reply_markup=kb,
)
@exact("همسه", "همسة")
async def whisper_exact(update, ctx, arg=""):
if update.effective_chat.type == "private":
await update.message.reply_text("✧︙استخدم أمر (همسه) بالرد على شخص داخل الجروب.")
return
r = update.message.reply_to_message
if not r or not r.from_user:
await update.message.reply_text("✧︙رد على رسالة الشخص اللي عايز تبعتله همسة واكتب: همسه")
return
if r.from_user.id == update.effective_user.id:
await update.message.reply_text("✧︙مينفعش تبعت همسة لنفسك 😅")
return
if r.from_user.is_bot:
await update.message.reply_text("✧︙مينفعش تبعت همسة لبوت 🤖")
return
await _start_whisper(update, ctx, r.from_user.id, r.from_user.first_name)
@prefix("همسه", "همسة")
async def whisper_prefix(update, ctx, arg):
if update.effective_chat.type == "private":
return
info = find_user_by_username(update.effective_chat.id, arg)
if not info:
await update.message.reply_text("✧︙رد على رسالة الشخص أو اكتب معرف عضو متفاعل في الجروب.")
return
if info["id"] == update.effective_user.id:
await update.message.reply_text("✧︙مينفعش تبعت همسة لنفسك 😅")
return
await _start_whisper(update, ctx, info["id"], info["first"] or info["name"])
async def handle_start_deeplink(update, ctx, payload):
"""يتعامل مع /start wh_<token> في الخاص."""
if not payload.startswith("wh_"):
return False
token = payload[3:]
info = _PENDING_LINKS.get(token)
if not info:
await update.message.reply_text("❌ رابط الهمسة ده انتهى أو مش صالح، ارجع الجروب واعمل همسة جديدة.")
return True
if update.effective_user.id != info["from_id"]:
await update.message.reply_text("🔒 رابط الهمسة ده خاص باللي طلبها بس!")
return True
_ACTIVE_PM[update.effective_user.id] = {**info, "token": token, "text": None}
await update.message.reply_text(
f"💌 أهلاً يا {update.effective_user.first_name}!\n"
f"✧︙اكتب دلوقتي نص الهمسة السرية اللي عايز تبعتها لـ ({info['to_name']}):"
)
return True
async def handle_private_text(update, ctx):
"""يستقبل نص الهمسة في الخاص ويسأل للتأكيد قبل الإرسال."""
uid = update.effective_user.id
sess = _ACTIVE_PM.get(uid)
if not sess or sess.get("text") is not None:
return False
txt = (update.message.text or "").strip()
if not txt:
return False
if len(txt) > 190:
await update.message.reply_text("⚠️ الهمسة طويلة شوية (الحد الأقصى 190 حرف عشان تظهر كاملة في الإشعار السري). اختصرها وابعتها تاني:")
return True
sess["text"] = txt
kb = InlineKeyboardMarkup([[
btn("✅ نعم، إرسال الهمسة", f"wh:send:{uid}", style="success"),
btn("❌ إلغاء", f"wh:cancel:{uid}", style="danger"),
]])
await update.message.reply_text(
f"📩 همستك إلى ({sess['to_name']}):\n\n"
f"「 {txt} 」\n\n"
f"❓ هل تريد إرسالها الآن للجروب؟",
reply_markup=kb,
)
return True
async def cb(update, ctx):
q = update.callback_query
parts = q.data.split(":", 2)
action = parts[1]
if action == "cancel":
uid = int(parts[2])
if q.from_user.id != uid:
await q.answer()
return
_ACTIVE_PM.pop(uid, None)
await q.answer("تم الإلغاء")
await q.edit_message_text("❌ تم إلغاء إرسال الهمسة.")
return
if action == "send":
uid = int(parts[2])
if q.from_user.id != uid:
await q.answer()
return
sess = _ACTIVE_PM.pop(uid, None)
if not sess or not sess.get("text"):
await q.answer("انتهت جلسة الهمسة.", show_alert=True)
return
_PENDING_LINKS.pop(sess.get("token"), None)
wid = secrets.token_urlsafe(8)
_WHISPERS[wid] = {
"from_id": sess["from_id"],
"from_name": sess["from_name"],
"to_id": sess["to_id"],
"to_name": sess["to_name"],
"text": sess["text"],
"ts": time.time(),
}
kb = InlineKeyboardMarkup([[btn("🔐 عرض الهمسة السرية", f"wh:view:{wid}", style="primary")]])
try:
await ctx.bot.send_message(
sess["chat_id"],
f"💌 همسة سرية\n✧︙من: {sess['from_name']}\n✧︙إلى: {sess['to_name']}\n✧︙لا يمكن لأحد فتحها سواكما 🔒",
reply_markup=kb,
)
await q.answer("تم الإرسال ✅")
await q.edit_message_text("✅ تم إرسال همستك السرية بنجاح للمجموعه!")
except Exception:
await q.answer("تعذر الإرسال للمجموعه.", show_alert=True)
return
if action == "view":
wid = parts[2]
w = _WHISPERS.get(wid)
if not w:
await q.answer("⏳ هذه الهمسة قديمة ولم تعد متاحة.", show_alert=True)
return
uid = q.from_user.id
# مسموح فقط للمستلم أو المرسل أو صاحب البوت (OWNER_ID)
if uid in (w["to_id"], w["from_id"]) or R.is_owner(uid):
await q.answer(f"💌 همسة من {w['from_name']}:\n{w['text']}", show_alert=True)
else:
await q.answer("🔒 هذه الهمسة ليست لك!", show_alert=True)
HANDLERS = [CallbackQueryHandler(cb, pattern=r"^wh:")]
|