forked from ShivangKakkar/StringSessionBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
172 lines (162 loc) · 7.36 KB
/
generate.py
File metadata and controls
172 lines (162 loc) · 7.36 KB
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
from telethon import TelegramClient
from pyrogram.types import Message
from pyrogram import Client, filters
from asyncio.exceptions import TimeoutError
from telethon.sessions import StringSession
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from pyrogram.errors import (
ApiIdInvalid,
PhoneNumberInvalid,
PhoneCodeInvalid,
PhoneCodeExpired,
SessionPasswordNeeded,
PasswordHashInvalid
)
from telethon.errors import (
ApiIdInvalidError,
PhoneNumberInvalidError,
PhoneCodeInvalidError,
PhoneCodeExpiredError,
SessionPasswordNeededError,
PasswordHashInvalidError
)
from data import Data
ask_ques = "Please choose the python library you want to generate string session for"
buttons_ques = [
[
InlineKeyboardButton("Pyrogram", callback_data="pyrogram"),
InlineKeyboardButton("Telethon", callback_data="telethon"),
],
[
InlineKeyboardButton("Pyrogram Bot", callback_data="pyrogram_bot"),
InlineKeyboardButton("Telethon Bot", callback_data="telethon_bot"),
],
]
@Client.on_message(filters.private & ~filters.forwarded & filters.command('generate'))
async def main(_, msg):
await msg.reply(ask_ques, reply_markup=InlineKeyboardMarkup(buttons_ques))
async def generate_session(bot: Client, msg: Message, telethon=False, is_bot: bool = False):
if telethon:
ty = "Telethon"
else:
ty = "Pyrogram v2"
if is_bot:
ty += " Bot"
await msg.reply(f"Starting {ty} Session Generation...")
user_id = msg.chat.id
api_id_msg = await bot.ask(user_id, 'Please send your `API_ID`', filters=filters.text)
if await cancelled(api_id_msg):
return
try:
api_id = int(api_id_msg.text)
except ValueError:
await api_id_msg.reply('Not a valid API_ID (which must be an integer). Please start generating session again.', quote=True, reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
api_hash_msg = await bot.ask(user_id, 'Please send your `API_HASH`', filters=filters.text)
if await cancelled(api_hash_msg):
return
api_hash = api_hash_msg.text
if not is_bot:
t = "Now please send your `PHONE_NUMBER` along with the country code. \nExample : `+19876543210`'"
else:
t = "Now please send your `BOT_TOKEN` \nExample : `12345:abcdefghijklmnopqrstuvwxyz`'"
phone_number_msg = await bot.ask(user_id, t, filters=filters.text)
if await cancelled(phone_number_msg):
return
phone_number = phone_number_msg.text
if not is_bot:
await msg.reply("Sending OTP...")
else:
await msg.reply("Logging as Bot User...")
if telethon and is_bot:
client = TelegramClient(StringSession(), api_id, api_hash)
elif telethon:
client = TelegramClient(StringSession(), api_id, api_hash)
elif is_bot:
client = Client(name=f"bot_{user_id}", api_id=api_id, api_hash=api_hash, bot_token=phone_number, in_memory=True)
else:
client = Client(name=f"user_{user_id}", api_id=api_id, api_hash=api_hash, in_memory=True)
await client.connect()
try:
code = None
if not is_bot:
if telethon:
code = await client.send_code_request(phone_number)
else:
code = await client.send_code(phone_number)
except (ApiIdInvalid, ApiIdInvalidError):
await msg.reply('`API_ID` and `API_HASH` combination is invalid. Please start generating session again.', reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
except (PhoneNumberInvalid, PhoneNumberInvalidError):
await msg.reply('`PHONE_NUMBER` is invalid. Please start generating session again.', reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
try:
phone_code_msg = None
if not is_bot:
phone_code_msg = await bot.ask(user_id, "Please check for an OTP in official telegram account. If you got it, send OTP here after reading the below format. \nIf OTP is `12345`, **please send it as** `1 2 3 4 5`.", filters=filters.text, timeout=600)
if await cancelled(phone_code_msg):
return
except TimeoutError:
await msg.reply('Time limit reached of 10 minutes. Please start generating session again.', reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
if not is_bot:
phone_code = phone_code_msg.text.replace(" ", "")
try:
if telethon:
await client.sign_in(phone_number, phone_code, password=None)
else:
await client.sign_in(phone_number, code.phone_code_hash, phone_code)
except (PhoneCodeInvalid, PhoneCodeInvalidError):
await msg.reply('OTP is invalid. Please start generating session again.', reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
except (PhoneCodeExpired, PhoneCodeExpiredError):
await msg.reply('OTP is expired. Please start generating session again.', reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
except (SessionPasswordNeeded, SessionPasswordNeededError):
try:
two_step_msg = await bot.ask(user_id, 'Your account has enabled two-step verification. Please provide the password.', filters=filters.text, timeout=300)
except TimeoutError:
await msg.reply('Time limit reached of 5 minutes. Please start generating session again.', reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
try:
password = two_step_msg.text
if telethon:
await client.sign_in(password=password)
else:
await client.check_password(password=password)
if await cancelled(api_id_msg):
return
except (PasswordHashInvalid, PasswordHashInvalidError):
await two_step_msg.reply('Invalid Password Provided. Please start generating session again.', quote=True, reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
else:
if telethon:
await client.start(bot_token=phone_number)
else:
await client.sign_in_bot(phone_number)
if telethon:
string_session = client.session.save()
else:
string_session = await client.export_session_string()
text = f"**{ty.upper()} STRING SESSION** \n\n`{string_session}` \n\nGenerated by @StarkStringGenBot"
try:
if not is_bot:
await client.send_message("me", text)
else:
await bot.send_message(msg.chat.id, text)
except KeyError:
pass
await client.disconnect()
await bot.send_message(msg.chat.id, "Successfully generated {} string session. \n\nPlease check your saved messages! \n\nBy @StarkBots".format("telethon" if telethon else "pyrogram"))
async def cancelled(msg):
if "/cancel" in msg.text:
await msg.reply("Cancelled the Process!", quote=True, reply_markup=InlineKeyboardMarkup(Data.generate_button))
return True
elif "/restart" in msg.text:
await msg.reply("Restarted the Bot!", quote=True, reply_markup=InlineKeyboardMarkup(Data.generate_button))
return True
elif msg.text.startswith("/"): # Bot Commands
await msg.reply("Cancelled the generation process!", quote=True)
return True
else:
return False