Skip to content

Telegram Exporter is a Python application for exporting and storing messages from Telegram chats, groups, or channels into a SQLite database. It automatically generates CSV and JSON files for data analysis and integration with other tools. The application supports Docker deployment and includes CI/CD for automated builds and testing.

License

Notifications You must be signed in to change notification settings

VladimirVereshchagin/telegram_exporter

Repository files navigation

Telegram Exporter

Build Status Python Version Docker Image Size GitHub Release

Docker Hub repository for Telegram Exporter
GitHub Packages for Telegram Exporter

Project Description

Telegram Exporter is a Python application designed to export and store messages from Telegram chats, groups, or channels into a SQLite database. The application supports formatting messages into HTML and automatically generates CSV and JSON files for easy data analysis and integration with other tools.

The application is particularly useful for:

  • Archiving messages from personal or public Telegram groups.
  • Analyzing chat data with external tools like Excel, Python scripts, or web services.
  • Exporting messages for research, reporting, or integration into third-party systems.

Key Features

  • Comprehensive Data Export:
    • Exports messages along with metadata like date, URLs, and chat IDs.
    • Supports rich text formatting with styles like bold, italic, and underline.
  • Automatic File Generation:
    • Automatically generates html.csv and html.json after every export to ensure your data is always up-to-date.
  • SQLite Database Storage:
    • Efficiently stores messages with options for filtering and querying.
  • Dockerized Deployment:
    • Deployable using a pre-built Docker image for easy scalability and cross-platform support.
  • CI/CD Integration:
    • Automatically builds and pushes Docker images to Docker Hub upon successful tests.
  • Cross-version Compatibility:
    • Supports Python 3.10, 3.11, and 3.12.

Installation

Prerequisites

  • Python version 3.10 to 3.12 (Python 3.13 is not supported due to deprecated modules like imghdr).
  • Git
  • Docker (optional for containerized deployment)

Installation Steps

  1. Clone the repository:

    git clone https://github.com/VladimirVereshchagin/telegram_exporter.git
    cd telegram_exporter
  2. Create a virtual environment:

    python -m venv venv
  3. Activate the virtual environment:

    • On Windows:

      venv\Scripts\activate
    • On macOS/Linux:

      source venv/bin/activate
  4. Install dependencies:

    pip install -r requirements.txt
  5. Configure environment variables:

    • Rename .env.example to .env and fill in your credentials:

      API_ID=your_api_id
      API_HASH=your_api_hash
      PHONE=your_phone_number_with_country_code

Usage

You can export messages from any chat, group, or channel you have access to.

Export from "Saved Messages" (default)

Run the application without any arguments to export messages from your "Saved Messages":

python telegram_exporter/main.py

Export from a specific chat, group, or channel

Use the --chat argument with the username, ID, or link to the chat:

python telegram_exporter/main.py --chat username_or_link

Examples

  • By username:

    python telegram_exporter/main.py --chat example_user
  • By channel link:

    python telegram_exporter/main.py --chat https://t.me/example_channel
  • By chat ID:

    python telegram_exporter/main.py --chat -1001234567890

Note: Ensure you have access to the specified chat, group, or channel.


Quick Start via Docker

For quick deployment, use the pre-built Docker image available on Docker Hub.

Run the Container

docker run -d \
--name telegram_exporter \
--env-file .env \
-v $(pwd)/data:/app/data \
vladimirvereschagin/telegram_exporter:latest

Access the Files

After running the application, the following files will be updated in the data directory:

  • db.sqlite3 — SQLite database containing exported messages.
  • html.csv — CSV file with messages and URLs.
  • html.json — JSON file with messages and URLs.

CI/CD

GitHub Actions is configured to automatically:


URL Extraction

The application automatically extracts URLs from messages and saves them in the urls column in the database. This includes:

  • Direct links in the message text.
  • Links contained in message entities (e.g., formatted links where display text differs from the URL).

Running Tests Locally

Run the following command to execute all tests:

python -m unittest discover tests

For linting:

flake8 telegram_exporter tests

Feedback

If you have any questions or suggestions, please create an issue or pull request in the project repository.


Contributing

If you'd like to contribute, please follow the Contribution Guide.


License

This project is distributed under the MIT License. See the LICENSE file for details.

About

Telegram Exporter is a Python application for exporting and storing messages from Telegram chats, groups, or channels into a SQLite database. It automatically generates CSV and JSON files for data analysis and integration with other tools. The application supports Docker deployment and includes CI/CD for automated builds and testing.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages