A Python script to fetch messages from Telegram channels. Filters the messages based on keywords, cleans the text, and saves the results to a JSON file. Useful for archiving posts, data analysis, or content monitoring.
- Connects to Telegram using Telethon.
- Fetches the latest messages from a specific channel.
- Filters out messages containing banned keywords.
- Removes unwanted tags from messages.
- Avoids saving duplicate messages.
- Saves messages to a JSON file with metadata (
id,date,text,link).
- Python 3.8+
- Telethon
- python-dotenv
Install dependencies via pip:
pip install telethon python-dotenv- Clone the repository:
git clone https://github.com/your-username/telegram-channel-scraper.git
cd telegram-channel-scraper- Create a
.envfile in the project root with the following variables:
API_ID=your_telegram_api_id
API_HASH=your_telegram_api_hash
PHONE_NUMBER=your_phone_number_with_country_code
CHANNEL_USERNAME=@your_channel_username
FETCH_LIMIT=20 # optional, defaults to 20
BANNED_KEYWORDS=spam,ad,etc # optional, comma-separated
TAGS_TO_REMOVE=#tag1,#tag2 # optional, comma-separatedNote: You can obtain
API_IDandAPI_HASHby creating an application on my.telegram.org.
Run the script:
python telegram_scraper.py- The script will log in to Telegram using your phone number.
- It fetches messages from the specified channel, filters them, and saves them in
telegram_posts.json. - Skips duplicates if messages are already saved.
The script saves messages in JSON:
[
{
"id": 123,
"date": "2026-01-27 12:00:00",
"text": "Cleaned message content",
"link": "https://t.me/channel_username/123"
}
]- Make sure your account has access to the channel you want to scrape.
BANNED_KEYWORDSandTAGS_TO_REMOVEare optional.- Always respect Telegram’s terms of service when scraping content.
This project is licensed under the MIT License.