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
115 lines (106 loc) · 5.43 KB
/
Copy pathgenerate.py
File metadata and controls
115 lines (106 loc) · 5.43 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
from asyncio.exceptions import TimeoutError
from Data import Data
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup
from pyrogram.errors import (
ApiIdInvalid,
PhoneNumberInvalid,
PhoneCodeInvalid,
PhoneCodeExpired,
SessionPasswordNeeded,
PasswordHashInvalid
)
ERROR_MESSAGE = "Oops! An exception occurred! \n\n**Error** : {} " \
"\n\nPlease visit @StarkBotsChat if this message doesn't contain any " \
"sensitive information and you if want to report this as " \
"this error message is not being logged by us!"
@Client.on_message(filters.private & ~filters.forwarded & filters.command('generate'))
async def main(bot, msg):
try:
await generate_session(bot, msg)
except Exception as e:
await msg.reply(ERROR_MESSAGE.format(str(e)))
async def generate_session(bot, msg):
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...")
client = Client(":memory:", api_id, api_hash)
await client.connect()
try:
phone_code_hash = (await client.send_code(phone_number)).phone_code_hash
except ApiIdInvalid:
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:
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:
await client.sign_in(phone_number, phone_code_hash, phone_code)
except PhoneCodeInvalid:
await msg.reply('OTP is invalid. Please start generating session again.', reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
except PhoneCodeExpired:
await msg.reply('OTP is expired. Please start generating session again.', reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
except SessionPasswordNeeded:
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:
await client.check_password(two_step_msg.text)
if await cancelled(api_id_msg):
return
except PasswordHashInvalid:
await two_step_msg.reply('Invalid Password Provided. Please start generating session again.', quote=True, reply_markup=InlineKeyboardMarkup(Data.generate_button))
return
string_session = await client.export_session_string()
await client.send_message("me", f"**STRING SESSION** \n\n`{string_session}` \n\nGenerated by @StarkStringGenBot")
await client.disconnect()
await phone_code_msg.reply("Successful. Please check your saved messages! \n\nBy @StarkBots")
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
# @Client.on_message(filters.private & ~filters.forwarded & filters.command(['cancel', 'restart']))
# async def formalities(_, msg):
# if "/cancel" in msg.text:
# await msg.reply("Cancelled all the Processes!", 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
# else:
# return False