├── requirements.txt ├── config.py ├── README.md └── main.py /requirements.txt: -------------------------------------------------------------------------------- 1 | pyrogram 2 | telethon 3 | asyncio 4 | requests 5 | tgcrypto 6 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | # Replace these with your actual API details 2 | API_ID = "28xxxx10" # Replace with your API ID Get From my.telegram.org 3 | API_HASH = "7fc5b356924ghfgggv481ab5eca3" # Replace with your API Hash Get From my.telegram.org 4 | BOT_TOKEN = "7824jjhhmd3gjq8qddzhyL0" # Replace with your Bot Token Get From @BotFather -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Telegram Session String Generator Bot 2 | 3 | ![Python](https://img.shields.io/badge/Python-3.8%2B-blue) 4 | ![Telethon](https://img.shields.io/badge/Telethon-1.21.1-blue) 5 | ![Pyrogram](https://img.shields.io/badge/Pyrogram-1.4.16-blue) 6 | ![License](https://img.shields.io/badge/License-MIT-green) 7 | ![GitHub stars](https://img.shields.io/github/stars/abirxdhack/StringSessionBot) 8 | ![GitHub forks](https://img.shields.io/github/forks/abirxdhack/StringSessionBot) 9 | 10 | Welcome to the Telegram Session String Generator Bot! This bot helps you generate session strings for both Pyrogram and Telethon clients securely and easily. 🚀 11 | 12 | ## Features ✨ 13 | 14 | - Safe and secure session string generation. 15 | - Supports both PyroGram and Telethon clients. 16 | - User-friendly interface. 17 | - Stores nothing, ensuring your privacy. 18 | 19 | ## Setup Instructions 🛠️ 20 | 21 | Follow these steps to set up and run the bot: 22 | 23 | 1. **Clone the repository:** 24 | 25 | ```bash 26 | git clone https://github.com/abirxdhack/StringSessionBot.git 27 | cd StringSessionBot 28 | ``` 29 | 30 | 2. **Install the required dependencies:** 31 | 32 | ```bash 33 | pip install -r requirements.txt 34 | ``` 35 | 36 | 3. **Create a `config.py` file:** 37 | 38 | ```python 39 | # config.py 40 | API_ID = 'your_api_id_here' 41 | API_HASH = 'your_api_hash_here' 42 | BOT_TOKEN = 'your_bot_token_here' 43 | ``` 44 | 45 | 4. **Run the bot:** 46 | 47 | ```bash 48 | python3 main.py 49 | ``` 50 | 51 | ## Configuration 52 | 53 | 1. Open the `config.py` file in your favorite text editor. 54 | 2. Replace the placeholders for `API_ID`, `API_HASH`, `BOT_TOKEN`, and with your actual values: 55 | - **`API_ID`**: Your API ID from [my.telegram.org](https://my.telegram.org). 56 | - **`API_HASH`**: Your API Hash from [my.telegram.org](https://my.telegram.org). 57 | - **`BOT_TOKEN`**: The token you obtained from [@BotFather](https://t.me/BotFather). 58 | 59 | ## Deploy the Bot 60 | 61 | ```sh 62 | git clone https://github.com/abirxdhack/StringSessionBot 63 | cd StringSessionBot 64 | python main.py 65 | ``` 66 | 67 | ## Usage 🚀 68 | 69 | - Start a private chat with the bot. 70 | - Use `/start` to get an overview and instructions. 71 | - Use `/pyro` to generate a Pyrogram session string. 72 | - Use `/tele` to generate a Telethon session string. 73 | - Follow the prompts to enter your API ID, API Hash, and phone number. 74 | - Enter the OTP and 2FA (if applicable) to get your session string. 75 | 76 | ## Example Commands 📋 77 | 78 | - `/start` - Get an overview and instructions. 79 | - `/pyro` - Generate a Pyrogram session string. 80 | - `/tele` - Generate a Telethon session string. 81 | 82 | ## Contributing 🤝 83 | 84 | Contributions are welcome! Please fork the repository and submit a pull request with your improvements. 85 | 86 | 87 | 88 | ## Contact 📬 89 | 90 | For any questions or suggestions, feel free to reach out: 91 | 92 | - Telegram: [My Dev👨‍💻](https://t.me/abirxdhack) 93 | 94 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import asyncio 3 | from asyncio.exceptions import TimeoutError 4 | from pyrogram import Client, filters 5 | from pyrogram.enums import ParseMode, ChatType 6 | from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton 7 | from telethon import TelegramClient 8 | from telethon.sessions import StringSession 9 | from pyrogram.errors import ( 10 | ApiIdInvalid, 11 | PhoneNumberInvalid, 12 | PhoneCodeInvalid, 13 | PhoneCodeExpired, 14 | SessionPasswordNeeded, 15 | PasswordHashInvalid 16 | ) 17 | from telethon.errors import ( 18 | ApiIdInvalidError, 19 | PhoneNumberInvalidError, 20 | PhoneCodeInvalidError, 21 | PhoneCodeExpiredError, 22 | SessionPasswordNeededError, 23 | PasswordHashInvalidError 24 | ) 25 | from config import ( 26 | API_ID, 27 | API_HASH, 28 | BOT_TOKEN 29 | ) 30 | 31 | # Constants for timeouts 32 | TIMEOUT_OTP = 600 # 10 minutes 33 | TIMEOUT_2FA = 300 # 5 minutes 34 | 35 | session_data = {} 36 | 37 | def setup_string_handler(app: Client): 38 | @app.on_message(filters.command(["pyro", "tele"], prefixes=["/", ".", ",", "!"]) & (filters.private | filters.group)) 39 | async def session_setup(client, message: Message): 40 | if message.chat.type in (ChatType.SUPERGROUP, ChatType.GROUP): 41 | await client.send_message( 42 | chat_id=message.chat.id, 43 | text="**❌ String Session Generator Only Works In Private Chats**", 44 | parse_mode=ParseMode.MARKDOWN 45 | ) 46 | return 47 | 48 | platform = "PyroGram" if message.command[0] == "pyro" else "Telethon" 49 | await handle_start(client, message, platform) 50 | 51 | @app.on_callback_query(filters.regex(r"^session_start_")) 52 | async def callback_query_start_handler(client, callback_query): 53 | await handle_callback_query(client, callback_query) 54 | 55 | @app.on_callback_query(filters.regex(r"^session_restart_")) 56 | async def callback_query_restart_handler(client, callback_query): 57 | await handle_callback_query(client, callback_query) 58 | 59 | @app.on_callback_query(filters.regex(r"^session_close$")) 60 | async def callback_query_close_handler(client, callback_query): 61 | await handle_callback_query(client, callback_query) 62 | 63 | @app.on_message(filters.text & filters.create(lambda _, __, message: message.chat.id in session_data)) 64 | async def text_handler(client, message: Message): 65 | await handle_text(client, message) 66 | 67 | async def handle_start(client, message, platform): 68 | session_type = "Telethon" if platform == "Telethon" else "Pyrogram" 69 | session_data[message.chat.id] = {"type": session_type} 70 | await client.send_message( 71 | chat_id=message.chat.id, 72 | text=( 73 | f"**💥Welcome to the {session_type} session setup!**\n" 74 | "**━━━━━━━━━━━━━━━━━**\n" 75 | "**This is a totally safe session string generator. We don't save any info that you will provide, so this is completely safe.**\n\n" 76 | "**Note: Don't send OTP directly. Otherwise, your account could be banned, or you may not be able to log in.**" 77 | ), 78 | reply_markup=InlineKeyboardMarkup([[ 79 | InlineKeyboardButton("Start", callback_data=f"session_start_{session_type.lower()}"), 80 | InlineKeyboardButton("Close", callback_data="session_close") 81 | ]]), 82 | parse_mode=ParseMode.MARKDOWN 83 | ) 84 | 85 | async def handle_callback_query(client, callback_query): 86 | data = callback_query.data 87 | chat_id = callback_query.message.chat.id 88 | 89 | if data == "session_close": 90 | platform = session_data.get(chat_id, {}).get("type", "").lower() 91 | if platform == "pyrogram": 92 | await callback_query.message.edit_text("**❌Cancelled. You can start by sending /pyro**", parse_mode=ParseMode.MARKDOWN) 93 | elif platform == "telethon": 94 | await callback_query.message.edit_text("**❌Cancelled. You can start by sending /tele**", parse_mode=ParseMode.MARKDOWN) 95 | if chat_id in session_data: 96 | del session_data[chat_id] 97 | return 98 | 99 | if data.startswith("session_start_"): 100 | session_type = data.split('_')[2] 101 | await callback_query.message.edit_text( 102 | "**Send Your API ID**", 103 | reply_markup=InlineKeyboardMarkup([[ 104 | InlineKeyboardButton("Restart", callback_data=f"session_restart_{session_type}"), 105 | InlineKeyboardButton("Close", callback_data="session_close") 106 | ]]), 107 | parse_mode=ParseMode.MARKDOWN 108 | ) 109 | session_data[chat_id]["stage"] = "api_id" 110 | 111 | if data.startswith("session_restart_"): 112 | session_type = data.split('_')[2] 113 | await handle_start(client, callback_query.message, platform=session_type.capitalize()) 114 | 115 | async def handle_text(client, message: Message): 116 | chat_id = message.chat.id 117 | if chat_id not in session_data: 118 | return 119 | 120 | session = session_data[chat_id] 121 | stage = session.get("stage") 122 | 123 | if stage == "api_id": 124 | try: 125 | api_id = int(message.text) 126 | session["api_id"] = api_id 127 | await client.send_message( 128 | chat_id=message.chat.id, 129 | text="**Send Your API Hash**", 130 | reply_markup=InlineKeyboardMarkup([[ 131 | InlineKeyboardButton("Restart", callback_data=f"session_restart_{session['type'].lower()}"), 132 | InlineKeyboardButton("Close", callback_data="session_close") 133 | ]]), 134 | parse_mode=ParseMode.MARKDOWN 135 | ) 136 | session["stage"] = "api_hash" 137 | except ValueError: 138 | await client.send_message( 139 | chat_id=message.chat.id, 140 | text="**❌Invalid API ID. Please enter a valid integer.**" 141 | ) 142 | 143 | elif stage == "api_hash": 144 | session["api_hash"] = message.text 145 | await client.send_message( 146 | chat_id=message.chat.id, 147 | text="** Send Your Phone Number\n[Example: +880xxxxxxxxxx] **", 148 | reply_markup=InlineKeyboardMarkup([[ 149 | InlineKeyboardButton("Restart", callback_data=f"session_restart_{session['type'].lower()}"), 150 | InlineKeyboardButton("Close", callback_data="session_close") 151 | ]]), 152 | parse_mode=ParseMode.MARKDOWN 153 | ) 154 | session["stage"] = "phone_number" 155 | 156 | elif stage == "phone_number": 157 | session["phone_number"] = message.text 158 | otp_message = await client.send_message( 159 | chat_id=message.chat.id, 160 | text="**💥Sending OTP Check PM.....**" 161 | ) 162 | await send_otp(client, message, otp_message) 163 | 164 | elif stage == "otp": 165 | otp = ''.join([char for char in message.text if char.isdigit()]) 166 | session["otp"] = otp 167 | otp_message = await client.send_message( 168 | chat_id=message.chat.id, 169 | text="**💥Validating Your Inputed OTP.....**" 170 | ) 171 | await validate_otp(client, message, otp_message) 172 | 173 | elif stage == "2fa": 174 | session["password"] = message.text 175 | await validate_2fa(client, message) 176 | 177 | async def send_otp(client, message, otp_message): 178 | session = session_data[message.chat.id] 179 | api_id = session["api_id"] 180 | api_hash = session["api_hash"] 181 | phone_number = session["phone_number"] 182 | telethon = session["type"] == "Telethon" 183 | 184 | if telethon: 185 | client_obj = TelegramClient(StringSession(), api_id, api_hash) 186 | else: 187 | client_obj = Client(":memory:", api_id, api_hash) 188 | 189 | await client_obj.connect() 190 | 191 | try: 192 | if telethon: 193 | code = await client_obj.send_code_request(phone_number) 194 | else: 195 | code = await client_obj.send_code(phone_number) 196 | session["client_obj"] = client_obj 197 | session["code"] = code 198 | session["stage"] = "otp" 199 | 200 | # Start a timeout task for OTP expiry 201 | asyncio.create_task(handle_otp_timeout(client, message)) 202 | 203 | await client.send_message( 204 | chat_id=message.chat.id, 205 | text="**✅Send The OTP as text. Please send a text message embedding the OTP like: 'AB5 CD0 EF3 GH7 IJ6'**", 206 | reply_markup=InlineKeyboardMarkup([[ 207 | InlineKeyboardButton("Restart", callback_data=f"session_restart_{session['type'].lower()}"), 208 | InlineKeyboardButton("Close", callback_data="session_close") 209 | ]]), 210 | parse_mode=ParseMode.MARKDOWN 211 | ) 212 | await otp_message.delete() 213 | except (ApiIdInvalid, ApiIdInvalidError): 214 | await client.send_message( 215 | chat_id=message.chat.id, 216 | text='**❌ `API_ID` and `API_HASH` combination is invalid**', 217 | reply_markup=InlineKeyboardMarkup([ 218 | [InlineKeyboardButton("Restart", callback_data=f"session_restart_{session['type'].lower()}"), InlineKeyboardButton("Close", callback_data="session_close")] 219 | ]) 220 | ) 221 | await otp_message.delete() 222 | return 223 | except (PhoneNumberInvalid, PhoneNumberInvalidError): 224 | await client.send_message( 225 | chat_id=message.chat.id, 226 | text='**❌`PHONE_NUMBER` is invalid.**', 227 | reply_markup=InlineKeyboardMarkup([ 228 | [InlineKeyboardButton("Restart", callback_data=f"session_restart_{session['type'].lower()}"), InlineKeyboardButton("Close", callback_data="session_close")] 229 | ]) 230 | ) 231 | await otp_message.delete() 232 | return 233 | 234 | async def handle_otp_timeout(client, message): 235 | await asyncio.sleep(TIMEOUT_OTP) 236 | if message.chat.id in session_data and session_data[message.chat.id].get("stage") == "otp": 237 | await client.send_message( 238 | chat_id=message.chat.id, 239 | text="**❌ Bro Your OTP Has Expired**", 240 | parse_mode=ParseMode.MARKDOWN 241 | ) 242 | del session_data[message.chat.id] 243 | 244 | async def validate_otp(client, message, otp_message): 245 | session = session_data[message.chat.id] 246 | client_obj = session["client_obj"] 247 | phone_number = session["phone_number"] 248 | otp = session["otp"] 249 | code = session["code"] 250 | telethon = session["type"] == "Telethon" 251 | 252 | try: 253 | if telethon: 254 | await client_obj.sign_in(phone_number, otp) 255 | else: 256 | await client_obj.sign_in(phone_number, code.phone_code_hash, otp) 257 | await generate_session(client, message) 258 | await otp_message.delete() 259 | except (PhoneCodeInvalid, PhoneCodeInvalidError): 260 | await client.send_message( 261 | chat_id=message.chat.id, 262 | text='**❌Bro Your OTP Is Wrong**', 263 | reply_markup=InlineKeyboardMarkup([ 264 | [InlineKeyboardButton("Restart", callback_data=f"session_restart_{session['type'].lower()}"), InlineKeyboardButton("Close", callback_data="session_close")] 265 | ]) 266 | ) 267 | await otp_message.delete() 268 | return 269 | except (PhoneCodeExpired, PhoneCodeExpiredError): 270 | await client.send_message( 271 | chat_id=message.chat.id, 272 | text='**❌Bro OTP Has expired**', 273 | reply_markup=InlineKeyboardMarkup([ 274 | [InlineKeyboardButton("Restart", callback_data=f"session_restart_{session['type'].lower()}"), InlineKeyboardButton("Close", callback_data="session_close")] 275 | ]) 276 | ) 277 | await otp_message.delete() 278 | return 279 | except (SessionPasswordNeeded, SessionPasswordNeededError): 280 | session["stage"] = "2fa" 281 | 282 | # Start a timeout task for 2FA expiry 283 | asyncio.create_task(handle_2fa_timeout(client, message)) 284 | 285 | await client.send_message( 286 | chat_id=message.chat.id, 287 | text="**❌ 2FA Is Required To Login. Please Enter 2FA**", 288 | reply_markup=InlineKeyboardMarkup([[ 289 | InlineKeyboardButton("Restart", callback_data=f"session_restart_{session['type'].lower()}"), 290 | InlineKeyboardButton("Close", callback_data="session_close") 291 | ]]), 292 | parse_mode=ParseMode.MARKDOWN 293 | ) 294 | await otp_message.delete() 295 | 296 | async def handle_2fa_timeout(client, message): 297 | await asyncio.sleep(TIMEOUT_2FA) 298 | if message.chat.id in session_data and session_data[message.chat.id].get("stage") == "2fa": 299 | await client.send_message( 300 | chat_id=message.chat.id, 301 | text="**❌ Bro Your 2FA Input Has Expired**", 302 | parse_mode=ParseMode.MARKDOWN 303 | ) 304 | del session_data[message.chat.id] 305 | 306 | async def validate_2fa(client, message): 307 | session = session_data[message.chat.id] 308 | client_obj = session["client_obj"] 309 | password = session["password"] 310 | telethon = session["type"] == "Telethon" 311 | 312 | try: 313 | if telethon: 314 | await client_obj.sign_in(password=password) 315 | else: 316 | await client_obj.check_password(password=password) 317 | await generate_session(client, message) 318 | except (PasswordHashInvalid, PasswordHashInvalidError): 319 | await client.send_message( 320 | chat_id=message.chat.id, 321 | text='**❌Invalid Password Provided**', 322 | reply_markup=InlineKeyboardMarkup([ 323 | [InlineKeyboardButton("Restart", callback_data=f"session_restart_{session['type'].lower()}"), InlineKeyboardButton("Close", callback_data="session_close")] 324 | ]) 325 | ) 326 | return 327 | 328 | async def generate_session(client, message): 329 | session = session_data[message.chat.id] 330 | client_obj = session["client_obj"] 331 | telethon = session["type"] == "Telethon" 332 | 333 | if telethon: 334 | string_session = client_obj.session.save() 335 | else: 336 | string_session = await client_obj.export_session_string() 337 | 338 | text = f"**{session['type'].upper()} SESSION FROM Smart Tool**:\n\n`{string_session}`\n\nGenerated by @ItsSmartToolBot" 339 | 340 | try: 341 | await client_obj.send_message("me", text) 342 | except KeyError: 343 | pass 344 | 345 | await client_obj.disconnect() 346 | await client.send_message( 347 | chat_id=message.chat.id, 348 | text="**This string has been saved ✅ in your Saved Messages**", 349 | parse_mode=ParseMode.MARKDOWN 350 | ) 351 | del session_data[message.chat.id] 352 | 353 | # Initialize the Pyrogram Client 354 | app = Client("sessionstring", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN) 355 | 356 | setup_string_handler(app) 357 | 358 | app.run() 359 | --------------------------------------------------------------------------------