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
132 lines (126 loc) · 6.07 KB
/
generate.py
File metadata and controls
132 lines (126 loc) · 6.07 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
from asyncio.exceptions import TimeoutError
from Data import Data
from pyrogram import Client, filters
from telethon import TelegramClient
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
)
@Client.on_message(filters.private & ~filters.forwarded & filters.command('generate'))
async def main(_, msg):
await msg.reply(
"Please choose the python library you want to generate string session for",
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton("Pyrogram", callback_data="pyrogram"),
InlineKeyboardButton("Telethon", callback_data="telethon")
]])
)
async def generate_session(bot, msg, telethon=False):
await msg.reply("Starting {} Session Generation...".format("Telethon" if telethon else "Pyrogram"))
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_id_msg):
return
api_hash = api_hash_msg.text
phone_number_msg = await bot.ask(user_id, 'Now please send your `PHONE_NUMBER` along with the country code. \nExample : `+19876543210`', filters=filters.text)
if await cancelled(api_id_msg):
return
phone_number = phone_number_msg.text
await msg.reply("Sending OTP...")
if telethon:
client = TelegramClient(StringSession(), api_id, api_hash)
else:
client = Client(":memory:", api_id, api_hash)
await client.connect()
try:
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 = 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(api_id_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
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
if telethon:
string_session = client.session.save()
else:
string_session = await client.export_session_string()
text = "**{} STRING SESSION** \n\n`{}` \n\nGenerated by @StarkStringGenBot".format("TELETHON" if telethon else "PYROGRAM", string_session)
try:
await client.send_message("me", text)
except KeyError:
pass
await client.disconnect()
await phone_code_msg.reply("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