forked from SilentDemonSD/WZML-X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_pyro_sess.py
174 lines (153 loc) · 7.36 KB
/
gen_pyro_sess.py
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
#!/usr/bin/env python3
from time import time
from aiofiles.os import remove as aioremove
from asyncio import sleep, wrap_future, Lock
from functools import partial
from pyrogram import Client
from pyrogram.filters import command, user, text, private
from pyrogram.handlers import MessageHandler
from pyrogram.errors import SessionPasswordNeeded, FloodWait, PhoneNumberInvalid, ApiIdInvalid, PhoneCodeInvalid, PhoneCodeExpired, UsernameNotOccupied, ChatAdminRequired, PeerIdInvalid
from bot import bot, LOGGER
from bot.helper.ext_utils.bot_utils import new_thread, new_task
from bot.helper.telegram_helper.message_utils import sendMessage, editMessage, deleteMessage, sendFile
from bot.helper.telegram_helper.filters import CustomFilters
session_dict = {}
session_lock = Lock()
isStop = False
@new_task
async def genPyroString(client, message):
global isStop
session_dict.clear()
sess_msg = await sendMessage(message, """⌬ <u><i><b>Pyrogram String Session Generator</b></i></u>
<i>Send your <code>API_ID</code> or <code>APP_ID</code>.
Get from https://my.telegram.org</i>.
<b>Timeout:</b> 120s
<i>Send /stop to Stop Process</i>""")
session_dict['message'] = sess_msg
await wrap_future(invoke(client, message, 'API_ID'))
if isStop:
return
async with session_lock:
try:
api_id = int(session_dict['API_ID'])
except Exception:
return await editMessage(sess_msg, "<i><code>APP_ID</code> is Invalid.</i>\n\n ⌬ <b>Process Stopped.</b>")
await sleep(1.5)
await editMessage(sess_msg, """⌬ <u><i><b>Pyrogram String Session Generator</b></i></u>
<i>Send your <code>API_HASH</code>. Get from https://my.telegram.org</i>.
<b>Timeout:</b> 120s
<i>Send /stop to Stop Process</i>""")
await wrap_future(invoke(client, message, 'API_HASH'))
if isStop:
return
async with session_lock:
api_hash = session_dict['API_HASH']
if len(api_hash) <= 30:
return await editMessage(sess_msg, "<i><code>API_HASH</code> is Invalid.</i>\n\n ⌬ <b>Process Stopped.</b>")
while True:
await sleep(1.5)
await editMessage(sess_msg, """⌬ <u><i><b>Pyrogram String Session Generator</b></i></u>
<i>Send your Telegram Account's Phone number in International Format ( Including Country Code ). <b>Example :</b> +14154566376</i>.
<b>Timeout:</b> 120s
<i>Send /stop to Stop Process</i>""")
await wrap_future(invoke(client, message, 'PHONE_NO'))
if isStop:
return
await editMessage(sess_msg, f"⌬ <b>Verification Confirmation:</b>\n\n <i>Is {session_dict['PHONE_NO']} correct? (y/n/yes/no):</i> \n\n<b>Send y/yes (Yes) | n/no (No)</b>")
await wrap_future(invoke(client, message, 'CONFIRM_PHN'))
if isStop:
return
async with session_lock:
if session_dict['CONFIRM_PHN'].lower() in ['y', 'yes']:
break
try:
pyro_client = Client(f"WZML-X-{message.from_user.id}", api_id=api_id, api_hash=api_hash)
except Exception as e:
await editMessage(sess_msg, f"<b>Client Error:</b> {str(e)}")
return
try:
await pyro_client.connect()
except ConnectionError:
await pyro_client.disconnect()
await pyro_client.connect()
try:
user_code = await pyro_client.send_code(session_dict['PHONE_NO'])
await sleep(1.5)
except FloodWait as e:
return await editMessage(sess_msg, f"<b>Floodwait of {e.value} Seconds. Retry Again</b>\n\n ⌬ <b>Process Stopped.</b>")
except ApiIdInvalid:
return await editMessage(sess_msg, "<b>API_ID and API_HASH are Invalid. Retry Again</b>\n\n ⌬ <b>Process Stopped.</b>")
except PhoneNumberInvalid:
return await editMessage(sess_msg, "<b>Phone Number is Invalid. Retry Again</b>\n\n ⌬ <b>Process Stopped.</b>")
await sleep(1.5)
await editMessage(sess_msg, """⌬ <u><i><b>Pyrogram String Session Generator</b></i></u>
<i>OTP has been sent to your Phone Number, Enter OTP in <code>1 2 3 4 5</code> format. ( Space between each Digits )</i>
<b>If any error or bot not responded, Retry Again.</b>
<b>Timeout:</b> 120s
<i>Send /stop to Stop Process</i>""")
await wrap_future(invoke(client, message, 'OTP'))
if isStop:
return
async with session_lock:
otp = ' '.join(str(session_dict['OTP']))
try:
await pyro_client.sign_in(session_dict['PHONE_NO'], user_code.phone_code_hash, phone_code=otp)
except PhoneCodeInvalid:
return await editMessage(sess_msg, "<i>Input OTP is Invalid.</i>\n\n ⌬ <b>Process Stopped.</b>")
except PhoneCodeExpired:
return await editMessage(sess_msg, "<i> Input OTP has Expired.</i>\n\n ⌬ <b>Process Stopped.</b>")
except SessionPasswordNeeded:
await sleep(1.5)
await editMessage(sess_msg, f"""⌬ <u><i><b>Pyrogram String Session Generator</b></i></u>
<i>Account is being Protected via <b>Two-Step Verification.</b> Send your Password below.</i>
<b>Timeout:</b> 120s
<b>Password Hint</b> : {await pyro_client.get_password_hint()}
<i>Send /stop to Stop Process</i>""")
await wrap_future(invoke(client, message, 'TWO_STEP_PASS'))
if isStop:
return
async with session_lock:
password = session_dict['TWO_STEP_PASS'].strip()
try:
await pyro_client.check_password(password)
except Exception as e:
return await editMessage(sess_msg, f"<b>Password Check Error:</b> {str(e)}")
except Exception as e:
return await editMessage(sess_msg ,f"<b>Sign In Error:</b> {str(e)}")
try:
session_string = await pyro_client.export_session_string()
await pyro_client.send_message("self", f"⌬ <b><u>Pyrogram Session Generated :</u></b>\n\n<code>{session_string}</code>\n\n<b>Via <a href='https://github.com/weebzone/WZML-X'>WZML-X</a> [ @WZML_X ]</b>", disable_web_page_preview=True)
await pyro_client.disconnect()
await editMessage(sess_msg, "⌬ <u><i><b>Pyrogram String Session Generator</b></i></u> \n\n➲ <b>String Session is Successfully Generated ( Saved Messages ).</b>")
except Exception as e:
return await editMessage(sess_msg ,f"<b>Export Session Error:</b> {str(e)}")
try:
await aioremove(f'WZML-X-{message.from_user.id}.session')
await aioremove(f'WZML-X-{message.from_user.id}.session-journal')
except: pass
async def set_details(_, message, newkey):
global isStop
user_id = message.from_user.id
value = message.text
await deleteMessage(message)
async with session_lock:
session_dict[newkey] = value
session_dict[user_id] = False
if value.lower() == '/stop':
isStop = True
return await editMessage(session_dict['message'], '⌬ <b>Process Stopped</b>')
@new_thread
async def invoke(client, message, key):
global isStop
user_id = message.from_user.id
session_dict[user_id] = True
start_time = time()
handler = client.add_handler(MessageHandler(partial(set_details, newkey=key), filters=user(user_id) & text & private), group=-1)
while session_dict[user_id]:
await sleep(0.5)
if time() - start_time > 120:
session_dict[user_id] = False
await editMessage(message, "⌬ <b>Process Stopped</b>")
isStop = True
client.remove_handler(*handler)
bot.add_handler(MessageHandler(genPyroString, filters=command('exportsession') & private & CustomFilters.sudo))