A Python-based Telegram channel parser that saves posts to a PostgreSQL database. Built for Railway deployment.
- Automatic message grouping for multi-part posts
- Media handling (photos and documents)
- Continuous channel monitoring with configurable intervals
- Stateful parsing (remembers last parsed message for each channel)
- Graceful error handling and recovery
- Comprehensive logging
- Railway deployment ready with PostgreSQL support
-
Copy
.env.exampleto.envand fill in your credentials:cp .env.example .env
-
Get your Telegram API credentials:
- Go to https://my.telegram.org/apps
- Create a new application
- Copy API_ID and API_HASH to your .env file
-
Generate a session string:
python generate_session.py
Copy the generated session string to your .env file.
-
Install dependencies:
pip install -r requirements.txt
-
Run the service:
python -m src.service
-
Push your code to GitHub
-
On Railway:
- Create a new project
- Choose "Deploy from GitHub repo"
- Select your repository
-
Add PostgreSQL Database:
- Click "New"
- Select "Database"
- Choose "Add PostgreSQL"
-
Configure Environment Variables:
TELEGRAM_API_ID: Your Telegram API IDTELEGRAM_API_HASH: Your Telegram API HashSESSION_NAME: Name for your session (optional)SESSION_STRING: Your session string from generate_session.pyCHANNEL_NAMES: Comma-separated list of channel usernames to parse
Note: DATABASE_URL will be automatically set by Railway when you add PostgreSQL.
TELEGRAM_API_ID: Your Telegram API IDTELEGRAM_API_HASH: Your Telegram API HashSESSION_NAME: Name for your session (optional)SESSION_STRING: Session string for Telegram authenticationCHANNEL_NAMES: Comma-separated list of channel usernames or links to parseDATABASE_URL: PostgreSQL connection URL (automatically set by Railway)
The parser uses PostgreSQL for production and SQLite for development.
Tracks the parsing state for each channel:
channel_id: Unique identifier for the channel (BigInteger)channel_name: Username or title of the channellast_message_id: ID of the last parsed messagelast_parsed_date: Timestamp of the last successful parse
Groups related messages together:
channel_id: Channel identifier (BigInteger)channel_name: Channel namegroup_id: Group identifier from Telegram (BigInteger)first_message_id: ID of the first message in the groupcombined_text: Combined text of all messages in the groupposted_date: When the message was postedparsed_date: When the message was parsedmessage_link: Link to the first message in the group
Individual messages within a group:
message_id: Telegram message identifiertext: Message contentgroup_id: Reference to the message group (BigInteger)
Media files attached to messages:
group_id: Reference to the message group (BigInteger)media_type: Type of media (photo, document)file_id: Telegram file identifierfile_url: Binary data of the media filemime_type: MIME type of the filefile_size: Size of the file in bytes
- Railway provides built-in logging and monitoring
- Application logs can be viewed in the Railway dashboard
- The parser logs:
- Channel connections
- Message processing status
- Media downloads
- Errors and retries
The service includes robust error handling:
- Automatic retry on network errors
- 5-minute interval between parsing cycles
- 1-minute retry delay on errors
- Graceful handling of API rate limits
- Database transaction management for data integrity