A Telegram bot for group chats that provides summarization, fact-checking, and question-answering capabilities using Google's Gemini AI with Google Search grounding.
- TLDR Summary: Summarize the last N messages in a group chat with
/tldr [number] - Fact Checking: Fact-check messages by replying with
/factcheck - Question Answering: Ask questions with
/q <your question>and receive factual, search-grounded answers - Image Generation: Generate images with
/img <description>. Uses Gemini by default, or Vertex AI if configured (can return multiple images). Generated images are automatically uploaded to CWD.PW for external hosting with AI generation metadata (model and prompt information). - Video Generation: Generates a video based on a text prompt and/or a replied-to image with
/vid <prompt>. - Image Understanding: Analyze and understand images when replying to a photo with
/factcheckor/q - Google Search Grounding: All responses are grounded in current information from Google Search
- Telegraph Integration: Automatically creates Telegraph pages for lengthy responses
- Database Logging: Messages are stored in a database for summarization and analysis
- Multi-language Support: Automatically detects and responds in the same language as the query
- Support Integration: Customizable support message with Ko-fi link for tip collection
- Python 3.13 or higher
- A Telegram Bot Token from BotFather
- Google Gemini API key from Google AI Studio
- Optional: Telegraph API access token (for optimal Telegraph integration)
- Optional: CWD.PW API key (for image hosting when using image generation features)
-
Clone the repository:
git clone https://github.com/yourusername/TelegramGroupHelperBot.git cd TelegramGroupHelperBot -
Create a virtual environment and activate it:
python -m venv venv source ./venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the dependencies:
pip install -r requirements.txt
-
Copy the example environment file and edit it with your credentials:
cp .env.example .env # Edit .env with your Telegram Bot Token, Gemini API key, Telegraph settings, and CWD.PW API key -
Run the database migrations:
alembic upgrade head
-
Run the bot:
python -m bot.main
Configure the bot by editing the .env file:
BOT_TOKEN: Your Telegram Bot token from BotFatherGEMINI_API_KEY: Google Gemini API key
TELEGRAM_MAX_LENGTH: Maximum character length before using Telegraph (default: 4000)TELEGRAPH_ACCESS_TOKEN: Your Telegraph API access tokenTELEGRAPH_AUTHOR_NAME: Name to display as author on Telegraph pagesTELEGRAPH_AUTHOR_URL: URL for the author name
CWD_PW_API_KEY: Your CWD.PW API key for image hosting (optional, required for external image hosting)
SUPPORT_MESSAGE: Custom message displayed with the/supportcommand (default: "☕ If you find this bot helpful, consider supporting its development!")KOFI_LINK: Your Ko-fi profile URL for accepting tips (default: "https://ko-fi.com/yourusername")
WHITELIST_FILE_PATH: Path to the whitelist file containing allowed user IDs (default: "allowed_chat.txt")
DATABASE_URL: Database connection URL (default: SQLite)USE_WEBHOOK: Whether to use webhook mode (default: false)WEBHOOK_URL: URL for the webhook in productionRATE_LIMIT_SECONDS: Rate limiting between user requests (default: 15)GEMINI_MODEL: Gemini model to use for general text tasks (default: gemini-pro)GEMINI_PRO_MODEL: Gemini Pro model for more complex tasks, including media analysis (default: gemini-2.5-pro-exp-03-25)GEMINI_IMAGE_MODEL: Gemini model for image generation (default: gemini-2.0-flash-exp-image-generation)GEMINI_VIDEO_MODEL: The Gemini model to use for video generation. Defaults to "veo-3.0-generate-preview".
VERTEX_PROJECT_ID: Your Google Cloud Project ID (required if using Vertex AI for any feature).VERTEX_LOCATION: The Google Cloud region for your Vertex AI resources (e.g.,us-central1, required if using Vertex AI).USE_VERTEX_IMAGE: Set to "true" to use Vertex AI for image generation via the/imgcommand (default: "false").VERTEX_IMAGE_MODEL: The specific Vertex AI image generation model to use (e.g.,imagegeneration@006).USE_VERTEX_VIDEO: Set to "true" to use Vertex AI for video generation via the/vidcommand (default: "false", currently uses Gemini VEO).VERTEX_VIDEO_MODEL: The specific Vertex AI video generation model to use ifUSE_VERTEX_VIDEOis true.
The bot can be deployed in various ways:
- Local Development: Run the bot locally with polling mode
- Production Webhook: Deploy to a server and use webhook mode
- Docker: Use the provided Dockerfile for containerized deployment
- Fly.io: Deploy using the included
fly.tomlconfiguration
-
Build the Docker image:
docker build -t telegram-group-helper-bot . -
Run the container:
docker run --env-file .env telegram-group-helper-bot
-
Install the Fly.io CLI
-
Log in to Fly.io:
fly auth login
-
Launch the app:
fly launch --generate-name
-
Set secrets:
fly secrets set BOT_TOKEN=your_bot_token GEMINI_API_KEY=your_gemini_key CWD_PW_API_KEY=your_cwd_pw_key -
Deploy:
fly deploy
For responses that exceed the Telegram message size limit, the bot automatically:
- Creates a Telegraph page with proper formatting
- Sends a link to the page instead of the full content
- Preserves markdown formatting in the Telegraph page
This feature triggers automatically when:
- The message exceeds the character limit set in
TELEGRAM_MAX_LENGTH - The message contains too many lines (22+)
This bot uses Google's Gemini AI with Google Search grounding to ensure responses are based on current, accurate information. When you ask a question or fact-check a statement, the bot:
- Searches the web for relevant information
- Grounds its response in the search results
- Provides a factual, up-to-date answer
- Includes citations where appropriate
The bot leverages Gemini's advanced image understanding capabilities to:
- Analyze photos shared in the group
- Answer questions about image content
- Fact-check claims in images or captions
- Detect objects and scenes in photos
- Process multiple image formats (JPEG, PNG, WEBP, HEIC, HEIF)
Simply reply to an image with /q What's in this picture? or /factcheck to analyze visual content.
The bot includes a comprehensive whitelist feature to restrict access to authorized users and groups:
- Access Control: Only users or groups listed in the whitelist file can use bot commands
- Flexible Access: Users can access the bot if either their user ID OR their group chat ID is whitelisted
- Backward Compatibility: If no whitelist file exists, all users and groups are allowed (default behavior)
- Easy Management: Simple text file with one ID per line (user IDs or group chat IDs)
- Security: Failed whitelist checks are logged for monitoring
- Create a file named
allowed_chat.txt(or setWHITELIST_FILE_PATHin your.env) - Add one Telegram user ID or group chat ID per line
- Restart the bot
- Ask users to send a message to @userinfobot
- Check bot logs when users send commands (temporarily disable whitelist first)
- Use Telegram's API to get user information
- Add the bot to the group
- Send a command in the group
- Check the bot logs - you'll see the chat_id (negative number)
- Or use @userinfobot in the group to get the group ID
- Or use the included
get_chat_id.pyscript to easily get IDs
# Telegram User and Chat Whitelist
# Add one ID per line (user IDs or group chat IDs)
# User IDs (positive numbers):
123456789
987654321
# Group Chat IDs (negative numbers):
-1001234567890
-1009876543210
To allow all users (disable whitelist), either:
- Delete the
allowed_chat.txtfile - Set
WHITELIST_FILE_PATHto a non-existent file path
Use the included get_chat_id.py script to easily get user and chat IDs:
- Edit the script and set your bot token
- Run:
python get_chat_id.py - Send a message to your bot
- The script will display all the ID information you need for the whitelist
The bot includes automatic image hosting through CWD.PW for generated images:
- Automatic Upload: All generated images are automatically uploaded to CWD.PW for external hosting
- Multiple Formats: Supports PNG, JPEG, and WebP image formats
- Error Handling: Upload failures don't affect image generation functionality
- Logging: Upload status and URLs are logged for debugging
To enable image hosting, set your CWD.PW API key in the .env file:
CWD_PW_API_KEY=your_cwd_pw_api_key_here
- When
CWD_PW_API_KEYis configured: Generated images are uploaded to CWD.PW and URLs are logged - When
CWD_PW_API_KEYis not configured: Images are generated normally but not uploaded - Upload failures are logged but don't prevent image generation or delivery to users
The bot uses SQLAlchemy for database operations. By default, it uses SQLite, but you can configure PostgreSQL for production.
See README-MIGRATIONS.md for instructions on managing database migrations.
Run tests with pytest:
pytestTo run specific test categories:
# Run unit tests only
pytest tests/unit/
# Run integration tests only
pytest tests/integration/- Kevin Z - Original project creator and main developer
- Frank L - CWD.PW image hosting integration and AI metadata enhancement
See AUTHORS for detailed contribution information.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.