Create approve_admins.py
Browse files
Detection/manager/approve_admins.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime as dt
|
2 |
+
import logging
|
3 |
+
from database import db
|
4 |
+
from config import PRIVATE_GROUP_ID, API_ID, API_HASH
|
5 |
+
from Detection.manager.builder_session import generate_random_string
|
6 |
+
|
7 |
+
from pyrogram import Client, filters
|
8 |
+
from pyrogram.errors import AuthKeyUnregistered
|
9 |
+
from pyrogram.types import (
|
10 |
+
CallbackQuery,
|
11 |
+
InlineKeyboardMarkup,
|
12 |
+
InlineKeyboardButton
|
13 |
+
)
|
14 |
+
|
15 |
+
LOGS = logging.getLogger(__name__)
|
16 |
+
|
17 |
+
def initial_client_user(session: str, plugins: str = "UserBot"):
|
18 |
+
client_name = generate_random_string(12)
|
19 |
+
return Client(
|
20 |
+
"{}".format(client_name),
|
21 |
+
api_id=API_ID,
|
22 |
+
api_hash=API_HASH,
|
23 |
+
session_string=session,
|
24 |
+
plugins={"root": f"Detection.{plugins}"}
|
25 |
+
)
|
26 |
+
|
27 |
+
@Client.on_callback_query(filters.regex(r"^statusub_(\d+)$"))
|
28 |
+
async def check_request(bot: Client, cb: CallbackQuery):
|
29 |
+
user_id = int(cb.matches[0].group(1))
|
30 |
+
request = await db.users_detection.find_one({"user_id": user_id})
|
31 |
+
if not request:
|
32 |
+
await cb.answer("No active requests found", show_alert=True)
|
33 |
+
return
|
34 |
+
|
35 |
+
status_icon = (
|
36 |
+
"π’"
|
37 |
+
if request["user_client"][0]["status"] == "approved"
|
38 |
+
else "π΄"
|
39 |
+
if request["user_client"][0]["status"] == "rejected"
|
40 |
+
else "β οΈ"
|
41 |
+
if request["user_client"][0]["status"] == "stopped"
|
42 |
+
else "π‘"
|
43 |
+
)
|
44 |
+
await cb.answer(
|
45 |
+
f"Request Status: {status_icon} {request['user_client'][0]['status'].capitalize()}\n"
|
46 |
+
f"Submitted: {request['user_client'][0].get('timestamp', 'Not available')}\n",
|
47 |
+
show_alert=True
|
48 |
+
)
|
49 |
+
|
50 |
+
@Client.on_callback_query(filters.regex(r"^(rejected_ub|pending_ub|approved_ub)_(\d+)$"))
|
51 |
+
async def admins_callback(client: Client, callback: CallbackQuery):
|
52 |
+
try:
|
53 |
+
action, user_id = callback.matches[0].groups()
|
54 |
+
action_type = action.split('_')[0]
|
55 |
+
admin_id = callback.from_user.id
|
56 |
+
admin_mention = callback.from_user.mention
|
57 |
+
if admin_id != 6477856957:
|
58 |
+
return await callback.answer("β Only Developer", show_alert=True)
|
59 |
+
request = await db.users_detection.find_one({"user_id": int(user_id)})
|
60 |
+
if not request:
|
61 |
+
await callback.answer("β User request not found!", show_alert=True)
|
62 |
+
await callback.message.edit_text(f"{callback.message.text}\n\nβ οΈ Failed: Request not found")
|
63 |
+
return
|
64 |
+
|
65 |
+
update_data = {
|
66 |
+
"user_client.$.status": action_type,
|
67 |
+
"last_updated": dt.now().isoformat(),
|
68 |
+
"user_client.$.admin_action": {
|
69 |
+
"by": admin_id,
|
70 |
+
"at": dt.now().isoformat(),
|
71 |
+
"username": callback.from_user.username
|
72 |
+
}
|
73 |
+
}
|
74 |
+
|
75 |
+
if action_type == "rejected":
|
76 |
+
update_data["user_client.$.admin_action"]["reason"] = "No reason provided"
|
77 |
+
|
78 |
+
update_result = await db.users_detection.update_one(
|
79 |
+
{
|
80 |
+
"_id": request["_id"],
|
81 |
+
"user_client.status": "pending",
|
82 |
+
"user_client.user_id": int(user_id)
|
83 |
+
},
|
84 |
+
{"$set": update_data}
|
85 |
+
)
|
86 |
+
|
87 |
+
if not update_result.modified_count:
|
88 |
+
await callback.answer("β Update failed!", show_alert=True)
|
89 |
+
return
|
90 |
+
|
91 |
+
if action_type == "approved":
|
92 |
+
await handle_approvalub(client, request, user_id, admin_id, admin_mention)
|
93 |
+
|
94 |
+
status_icon = {
|
95 |
+
"approved": "β
",
|
96 |
+
"rejected": "β",
|
97 |
+
"pending": "π"
|
98 |
+
}.get(action_type, "βΉοΈ")
|
99 |
+
|
100 |
+
await callback.message.edit_text(
|
101 |
+
f"{callback.message.text}\n\n"
|
102 |
+
f"{status_icon} Status: {action_type.capitalize()}ed by {admin_mention}\n"
|
103 |
+
f"β° {dt.now().strftime('%Y-%m-%d %H:%M:%S')}",
|
104 |
+
reply_markup=None
|
105 |
+
)
|
106 |
+
await callback.answer(f"Request {action_type}d successfully!")
|
107 |
+
|
108 |
+
except Exception as e:
|
109 |
+
LOGS.error(f"Admin action error: {str(e)}")
|
110 |
+
await handle_errorub(client, callback, e, action, admin_mention)
|
111 |
+
|
112 |
+
async def handle_approvalub(client, callback, request, user_id, admin_id, admin_mention):
|
113 |
+
try:
|
114 |
+
string_session = request["user_client"][0]["session_string"]
|
115 |
+
user_bots = initial_client_user(string_session)
|
116 |
+
try:
|
117 |
+
await user_bots.start()
|
118 |
+
bot_user = await user_bots.get_me()
|
119 |
+
except AuthKeyUnregistered as e:
|
120 |
+
await client.send_message(
|
121 |
+
PRIVATE_GROUP_ID,
|
122 |
+
f"Error reason: AuthKeyUnregistered `{user_id}`"
|
123 |
+
)
|
124 |
+
await client.send_message(user_id, "Error reason: AuthKeyUnregistered")
|
125 |
+
return
|
126 |
+
except Exception as e:
|
127 |
+
LOGS.error(f"Error handle_approvalub: {str(e)}")
|
128 |
+
await client.send_message(
|
129 |
+
user_id,
|
130 |
+
"β οΈ Userbot approval failed due to technical reasons.\n"
|
131 |
+
"Our team has been notified. Please try again later."
|
132 |
+
)
|
133 |
+
return
|
134 |
+
await db.users_detection.update_one(
|
135 |
+
{
|
136 |
+
"_id": request["_id"],
|
137 |
+
"user_client.status": "pending",
|
138 |
+
"user_client.user_id": int(user_id)
|
139 |
+
},
|
140 |
+
{
|
141 |
+
"$set": {
|
142 |
+
"user_client.$.user_id": bot_user.id,
|
143 |
+
"user_client.$.status": "approved",
|
144 |
+
"user_client.$.is_active": True,
|
145 |
+
"user_client.$.username": bot_user.username or "N/A",
|
146 |
+
"user_client.$.started_at": dt.now().isoformat(),
|
147 |
+
"user_client.$.admin_action": {
|
148 |
+
"by": admin_id,
|
149 |
+
"at": dt.now().isoformat()
|
150 |
+
}
|
151 |
+
}
|
152 |
+
}
|
153 |
+
)
|
154 |
+
await notify_userub(client, user_id, bot_user)
|
155 |
+
await client.send_message(
|
156 |
+
PRIVATE_GROUP_ID,
|
157 |
+
f"β
Approved Successfully\n\n"
|
158 |
+
f"π€ User: {request.get('username', 'N/A')} ({user_id})\n"
|
159 |
+
f"β Username: {bot_user.username}\n"
|
160 |
+
f"π Approved by: {admin_mention}\n"
|
161 |
+
f"β° Time: {dt.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
162 |
+
)
|
163 |
+
except Exception as e:
|
164 |
+
LOGS.error(f"Approval error: {str(e)}")
|
165 |
+
|
166 |
+
async def handle_errorub(client, callback, error, action, admin_mention):
|
167 |
+
await callback.answer("β οΈ Error", show_alert=True)
|
168 |
+
await callback.message.edit_text(
|
169 |
+
f"{callback.message.text}\n\nβ Error: {str(error)}"
|
170 |
+
)
|
171 |
+
await client.send_message(
|
172 |
+
PRIVATE_GROUP_ID,
|
173 |
+
f"π¨ Admin Action Error\n\n"
|
174 |
+
f"Action: {action}\n"
|
175 |
+
f"Admin: {admin_mention}\n"
|
176 |
+
f"Error: {str(error)}"
|
177 |
+
)
|
178 |
+
|
179 |
+
async def notify_userub(client, user_id, bot_user):
|
180 |
+
caption = (
|
181 |
+
"**Your Detection Has Been Approved!**\n\n"
|
182 |
+
f"Name: {bot_user.first_name}\n"
|
183 |
+
f"Username: @{bot_user.username or 'N/A'}\n"
|
184 |
+
f"User ID: `{bot_user.id}`\n\n"
|
185 |
+
)
|
186 |
+
await client.send_photo(
|
187 |
+
user_id,
|
188 |
+
photo="https://telegra.ph//file/586a3867c3e16ca6bb4fa.jpg",
|
189 |
+
caption=caption,
|
190 |
+
reply_markup=InlineKeyboardMarkup([
|
191 |
+
[InlineKeyboardButton("Channel", url="https://t.me/RendyProjects")]
|
192 |
+
])
|
193 |
+
)
|