This Telegram bot is designed to store messages from chats and provide AI-powered summaries on demand. It uses the Anthropic API for generating summaries and can handle various time-based summary requests.
- Store messages from Telegram chats in a SQLite database
- Generate AI-powered summaries of chat discussions
- Flexible summary requests (by minutes, hours, or days)
- Clean up old messages from the database
- Ask general questions to the AI
- Split bills from receipt photos
- Basic calculator functionality
- Admin broadcast messages
- Question answering based on chat history
- Chat-specific context for better AI responses
- Python 3.7+
- A Telegram Bot Token
- An Anthropic API Key
- Docker and Docker Compose (for Docker deployment)
- ngrok
-
Clone this repository:
git clone https://github.com/sdaveas/telegram-summary-bot cd telegram-summary-bot
-
Install the required dependencies: using pipenv
pipenv install
plain python
pip install -r requirements.txt
-
Create an
.env
file in the project root and add your API keys:TELEGRAM_BOT_TOKEN=your_telegram_bot_token_here ANTHROPIC_API_KEY=your_anthropic_api_key_here DATABASE_PATH=./database/messages.db ADMIN_USER_ID=your_telegram_user_id_here WEBHOOK_HOST=<global host listening to WEBHOOK_PORT> # set by 'ngrok http WEBHOOK_PORT', or whatever cloud service WEBHOOK_PORT=<your port> # 8443 by default
To start the bot, run:
python src/app.py
- Ensure you have Docker and Docker Compose installed on your system.
- Create an
.env
file as described in the Installation section. - Run the following command to start the bot:
docker-compose up -d ngrok http 8443
- To stop the bot:
docker-compose down
/help
: Display usage information/summary <M>m<H>h<D>d
: Generate a summary of recent messages- Examples:
/summary 10m
(last 10 minutes)/summary 2h30m
(last 2 hours and 30 minutes)/summary 1d6h30m
(last 1 day, 6 hours, and 30 minutes)
- Examples:
/question <time_range> <question>
: Ask a question about recent chat history- Example:
/question 1h What was the main topic discussed?
- Example:
/ask <question>
: Ask a general question to the AI/cal <expression>
: Perform basic mathematical calculations- Example:
/cal 2+2
- Example:
/clean
: Delete messages older than 1 day from the database/credits
: Show repo url/broadcast <message>
: (Admin only) Send a message to all chats/set_context <context>
: Set the context for the current chat- Example:
/set_context This is a work chat about project X
- Example:
/get_context
: View the current context for the chat/clear_context
: Remove the context for the current chatsplit_bill
orsb
(as photo caption): Split a bill from a receipt photo- Example: Send a photo with caption "split_bill Alice: 1 pizza, Bob: 2 beers"
The bot can maintain context for each chat, which is used to provide better responses to AI queries. The context is automatically included in all AI-powered responses (summaries, questions, and bill splitting). You can:
- Set the context using
/set_context <context>
- View the current context using
/get_context
- Remove the context using
/clear_context
The context helps the AI understand the chat's purpose and provide more relevant responses. For example, setting a context like "This is a work chat about project X" will help the AI provide more focused summaries and answers.
The bot uses a SQLite database to store messages and context. The database is automatically created when you first run the bot in the path specified by the environment variable DATABASE_PATH
(e.g. ./database/messages.db
)
The bot includes a test suite that can be run using:
python src/brain_test.py
- Keep your
.env
file secret and never commit it to version control. - Ensure you have the necessary rights and comply with privacy regulations when storing and processing chat messages.
- The
/broadcast
command is restricted to the user specified inADMIN_USER_ID
.
Contributions are welcome! Please feel free to submit a Pull Request.
All crates licensed under either of:
- Apache License, Version 2.0
- MIT license
at your option.
This bot stores message data. Ensure you have the right to store and process this data, and that you comply with all relevant data protection regulations.