From 48e06bb6d9ed156797ef4bc0dab88820fef948f3 Mon Sep 17 00:00:00 2001 From: Stark Bots <88478059+StarkBotsIndustries@users.noreply.github.com> Date: Tue, 5 Oct 2021 07:27:35 +0530 Subject: [PATCH] v1.0.0 Generate Telethon String Session --- StringSessionBot/callbacks.py | 15 +++++++- StringSessionBot/generate.py | 71 +++++++++++++++++++++++++---------- requirements.txt | 1 + 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/StringSessionBot/callbacks.py b/StringSessionBot/callbacks.py index e8ff95d..51ef657 100644 --- a/StringSessionBot/callbacks.py +++ b/StringSessionBot/callbacks.py @@ -1,6 +1,6 @@ from Data import Data from pyrogram import Client -from pyrogram.types import CallbackQuery, InlineKeyboardMarkup +from pyrogram.types import CallbackQuery, InlineKeyboardMarkup, InlineKeyboardButton from StringSessionBot.generate import generate_session, ERROR_MESSAGE @@ -42,8 +42,19 @@ async def _callbacks(bot: Client, callback_query: CallbackQuery): reply_markup=InlineKeyboardMarkup(Data.home_buttons), ) elif query == "generate": + await callback_query.message.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") + ]]) + ) + elif query in ["pyrogram", "telethon"]: await callback_query.answer() try: - await generate_session(bot, callback_query.message) + if query == "pyrogram": + await generate_session(bot, callback_query.message) + else: + await generate_session(bot, callback_query.message, telethon=True) except Exception as e: await callback_query.message.reply(ERROR_MESSAGE.format(str(e))) diff --git a/StringSessionBot/generate.py b/StringSessionBot/generate.py index 7d06f28..0cb4d9c 100644 --- a/StringSessionBot/generate.py +++ b/StringSessionBot/generate.py @@ -1,7 +1,9 @@ from asyncio.exceptions import TimeoutError from Data import Data from pyrogram import Client, filters -from pyrogram.types import InlineKeyboardMarkup +from telethon import TelegramClient +from telethon.sessions import StringSession +from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton from pyrogram.errors import ( ApiIdInvalid, PhoneNumberInvalid, @@ -10,6 +12,14 @@ SessionPasswordNeeded, PasswordHashInvalid ) +from telethon.errors import ( + ApiIdInvalidError, + PhoneNumberInvalidError, + PhoneCodeInvalidError, + PhoneCodeExpiredError, + SessionPasswordNeededError, + PasswordHashInvalidError +) ERROR_MESSAGE = "Oops! An exception occurred! \n\n**Error** : {} " \ "\n\nPlease visit @StarkBotsChat if this message doesn't contain any " \ @@ -18,14 +28,18 @@ @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 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): +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): @@ -44,14 +58,20 @@ async def generate_session(bot, msg): return phone_number = phone_number_msg.text await msg.reply("Sending OTP...") - client = Client(":memory:", api_id, api_hash) + if telethon: + client = TelegramClient(StringSession(), api_id, api_hash) + else: + 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: + 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: + except (PhoneNumberInvalid, PhoneNumberInvalidError): await msg.reply('`PHONE_NUMBER` is invalid. Please start generating session again.', reply_markup=InlineKeyboardMarkup(Data.generate_button)) return try: @@ -63,30 +83,41 @@ async def generate_session(bot, msg): return phone_code = phone_code_msg.text.replace(" ", "") try: - await client.sign_in(phone_number, phone_code_hash, phone_code) - except PhoneCodeInvalid: + 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: + except (PhoneCodeExpired, PhoneCodeExpiredError): await msg.reply('OTP is expired. Please start generating session again.', reply_markup=InlineKeyboardMarkup(Data.generate_button)) return - except SessionPasswordNeeded: + 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: - await client.check_password(two_step_msg.text) + 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: + 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 - string_session = await client.export_session_string() - await client.send_message("me", f"**STRING SESSION** \n\n`{string_session}` \n\nGenerated by @StarkStringGenBot") + 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) + await client.send_message("me", text) await client.disconnect() - await phone_code_msg.reply("Successful. Please check your saved messages! \n\nBy @StarkBots") + 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): diff --git a/requirements.txt b/requirements.txt index 9643be4..57f72f9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ pyrogram~=1.2.9 +Telethon~=1.23.0 TgCrypto SQLAlchemy~=1.4.21 psycopg2