Skip to content

vo1dee/EchoRead

Repository files navigation

EchoRead - Telegram Channel Summarizer Bot

A Telegram bot that monitors channels, fetches messages, and generates AI-powered summaries of the content.

Features

  • Monitor multiple Telegram channels
  • Fetch and store messages from channels
  • Generate daily summaries for individual channels
  • Generate custom time-range summaries across multiple channels
  • Interactive UI with buttons for easy navigation
  • Support for custom time ranges (hours, days, weeks)

Setup

  1. Clone the repository:
git clone https://github.com/yourusername/EchoRead.git
cd EchoRead
  1. Create and activate a virtual environment:
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Create a .env file in the project root with the following variables:
TELEGRAM_BOT_TOKEN=your_bot_token
TELEGRAM_API_ID=your_api_id
TELEGRAM_API_HASH=your_api_hash
OPENAI_MODEL=gpt-3.5-turbo  # or your preferred model
DATABASE_URL=sqlite:///./telegram_data.db

To obtain the required credentials:

  • TELEGRAM_BOT_TOKEN: Create a new bot using @BotFather on Telegram
  • TELEGRAM_API_ID and TELEGRAM_API_HASH: Get these from my.telegram.org

Usage

  1. Start the bot:
python main.py
  1. In Telegram, start a chat with your bot and use the following commands:

Basic Commands

  • /start - Start the bot and get a welcome message
  • /help - Display available commands
  • /addchannel @channelname - Add a channel to monitor
  • /removechannel @channelname - Stop monitoring a channel
  • /listchannels - Show all monitored channels
  • /fetchmessages @channelname [number] - Manually fetch recent messages (admin/debug)
  • /getsummary @channelname [YYYY-MM-DD] - Get a summary for a specific channel and date

New Summary Command

The /summary command provides an interactive way to generate summaries:

  1. Type /summary to start

  2. Select a time range:

    • Quick options: Last Hour, Last 6 Hours, Last 24 Hours, Last Week
    • Custom range: Enter a duration like:
      • 2h for last 2 hours
      • 3d for last 3 days
      • 1w for last week
  3. Select channels to include:

    • Use the interactive buttons to select/deselect channels
    • ✅ indicates selected channels
    • ❌ indicates unselected channels
    • "Select All" to include all channels
    • "Generate Summary" when ready
  4. The bot will generate a summary of messages from the selected channels within the specified time range.

Notes

  • The bot needs to be a member of the channels it monitors
  • For private channels, the bot needs to be added as an admin
  • Summaries are generated using OpenAI's API
  • Messages are stored in a local SQLite database by default
  • Long summaries are automatically split into multiple messages if they exceed Telegram's message length limit

Contributing

Feel free to submit issues and enhancement requests!

This multi-file structure provides a good separation of concerns for your Telegram Summarizer Bot.

Key parts of this structure:

  • config.py: Centralizes all your API keys and settings, loading them from a .env file for security.
  • database.py: Uses SQLAlchemy with an asynchronous engine (aiosqlite for SQLite in this example) to manage your data (monitored channels, fetched messages, generated summaries). It defines the database schema and provides asynchronous functions for CRUD operations.
  • telethon_service.py: Encapsulates all Telethon client logic. It handles connecting, authorizing, and fetching messages from public channels. It's designed to be called by your bot handlers when needed.
  • openai_service.py: Manages interactions with the OpenAI API. It takes text and parameters to generate summaries.
  • message_processor.py: Contains logic to retrieve messages from your database for a specific channel and date range, aggregate their text content, and prepare it for the OpenAI service. It also handles marking messages as processed.
  • telegram_bot_handlers.py: Defines all the command handlers for your python-telegram-bot. These handlers will interact with the database, Telethon service, message processor, and OpenAI service to fulfill user requests.
  • main.py: The main entry point. It initializes logging, configurations, database, all services (Telethon, OpenAI, MessageProcessor), and the python-telegram-bot application. It uses post_init to set up shared dependencies (like service instances and the DB session factory) in application.bot_data so handlers can access them. It also handles graceful shutdown.
  • requirements.txt: Lists all necessary Python packages.
  • .env: A file (you'll create this yourself and it should be in your .gitignore) to store your sensitive API keys and tokens.

To Get Started:

  1. Create the directory structure as shown.
  2. Copy each code block into its respective file.
  3. Create your .env file in the telegram_summarizer_bot/ directory and fill it with your actual API keys and tokens.
  4. Install dependencies:
    pip install -r requirements.txt
  5. Run the bot:
    python main.py

First Telethon Run: The first time you run main.py, if the telethon_service attempts to connect and doesn't find an existing session file (summarizer_bot_session.session), and if it's not authorized, it will prompt you in the console to enter your phone number (associated with your TELEGRAM_API_ID and TELEGRAM_API_HASH), the code Telegram sends you, and your 2FA password if enabled. This is a one-time setup for the Telethon client session.

This structure should give you a robust and maintainable foundation for your bot! You can expand on each module as your bot's features grow.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages