Generating a Telegram Session String
To connect a Telegram profile in Community Swarm, you need a session string â a portable, encoded representation of an authenticated Telegram session. This string allows Community Swarm to operate as your Telegram account without requiring you to re-enter verification codes on every startup.
Session strings are generated using either Telethon or Pyrogram, two popular Python libraries for the Telegram API. Both produce a valid session string that Community Swarm accepts.
Prerequisites
Before generating a session string, make sure you have:
- Telegram API credentials (
api_idandapi_hash) â If you donât have these yet, follow the Telegram API Setup guide first. - Python 3.8 or later installed on your computer.
- pip (Pythonâs package manager) â included with most Python installations.
You can check your Python version by running python3 --version (or python --version on Windows) in your terminal. If Python is not installed, download it from python.org .
Generate Your Session String
Choose either Telethon or Pyrogram â both produce a valid session string. Use whichever you prefer.
Telethon
Install Telethon
Open your terminal and run:
pip install telethonCreate the script
Create a new file called generate_session.py and paste the following code:
from telethon.sync import TelegramClient
from telethon.sessions import StringSession
api_id = int(input("Enter your API ID: "))
api_hash = input("Enter your API Hash: ")
with TelegramClient(StringSession(), api_id, api_hash) as client:
print("\n" + "=" * 60)
print("YOUR SESSION STRING (copy everything below):")
print("=" * 60)
print(client.session.save())
print("=" * 60)Run the script
python generate_session.pyThe script will prompt you for:
- API ID â Your numeric
api_idfrom my.telegram.org - API Hash â Your 32-character
api_hash - Phone number â The phone number associated with your Telegram account (include country code, e.g.,
+15551234567) - Verification code â Telegram will send a code to your Telegram app. Enter it when prompted.
- 2FA password â If your account has two-factor authentication enabled, enter your password when prompted.
Copy the session string
After successful authentication, the script will output your session string â a long string of characters starting with 1. Copy the entire string exactly as shown between the separator lines.
The session string is typically a single long line with no spaces or line breaks. Make sure you copy the complete string without accidentally truncating it.
Adding to Community Swarm
Once you have your session string, add it to your profile:
Go to the Profiles page
Log in to your Community Swarm dashboard and navigate to Profiles in the sidebar.
Open the credentials dialog
Find your Telegram profile and click the three-dot menu (âŽ) â Credentials.
Paste the session string
Paste your session string into the Session String field. Make sure the entire string is pasted without any extra spaces or line breaks.
Save
Click Save Credentials. Community Swarm will encrypt and store the session string immediately. Your profile is now ready to connect.
Your session string is encrypted immediately. Community Swarm encrypts all credentials using AES-256-GCM before storing them in the database. The session string is never stored in plaintext and is only decrypted when the userbot service needs to establish a connection.
Security Best Practices
- A session string provides full access to your Telegram account. Anyone with it can read and send messages, join groups, and modify account settings. Treat it like a password.
- Generate session strings only on trusted devices. Do not run the script on shared or public computers.
- Delete the script after use. Once you have your session string saved in Community Swarm, remove the
generate_session.pyfile from your computer. - Do not share session strings in public channels, repositories, or documents.
- Use dedicated accounts for Community Swarm rather than your personal Telegram account.
- Monitor active sessions. In Telegram, go to Settings > Devices to see all connected sessions. You can terminate suspicious sessions from there.
Troubleshooting
âFLOOD_WAIT_Xâ error
Telegram has rate-limited your requests. The X indicates how many seconds you must wait before trying again. Wait the specified time and re-run the script.
âInvalid api_idâ or âInvalid api_hashâ error
Verify that your API credentials are correct by logging into my.telegram.org . The api_hash must be exactly 32 hexadecimal characters with no extra spaces.
âPhoneCodeInvalidâ error
The verification code you entered was incorrect or expired. Re-run the script and enter the new code promptly â codes expire after approximately 60 seconds.
âSessionPasswordNeededâ error
Your account has two-factor authentication enabled. Enter your 2FA password when the script prompts you. If youâve forgotten it, youâll need to reset it through Telegramâs settings.
âAUTH_KEY_UNREGISTEREDâ or session expired
The session string has become invalid. This can happen if you terminated all active sessions from Telegram settings, or if Telegram invalidated the session. Generate a new session string by re-running the script.
âPhoneNumberBannedâ error
The Telegram account associated with this phone number has been banned. You will need to use a different account or contact Telegram support to appeal.