This script allows you to securely authenticate a Telegram user account using the GramJS MTProto client and generate a StringSession
. This session string can be stored in a database for use in automated Telegram operations like group management, messaging, etc.
- Node.js (v14 or higher recommended)
- Telegram App credentials (from my.telegram.org)
git clone https://github.com/Emmynem/telegram-session-generator
cd telegram-session-generator
npm install
Create a .env
file in the root of the project with the following:
API_ID=your_telegram_api_id
API_HASH=your_telegram_api_hash
Get your API_ID
and API_HASH
from https://my.telegram.org.
Run the index script to generate your Telegram StringSession
:
node index.js
You will be prompted to enter:
- π± Your phone number (with country code)
- π Your 2FA password (if enabled)
- π The Telegram code you receive
After successful login, a StringSession
will be printed to your terminal:
π Save this stringSession securely in your database:
1A0B...<your-session-string>
Save this session string securely in your database. It can be used to:
- Avoid logging in again
- Authenticate Telegram actions in other scripts (sending messages, inviting users, etc.)
Example usage in another script:
const stringSession = new StringSession("your_saved_session_string");
- Treat the session string like a password.
- Do not share or commit it to version control.
- Store it securely in an encrypted database field or secret manager.
MIT