A Telegram bot for tracking debts among friends and groups with natural language support using AI models like Google's Gemini, X.AI's Grok, or DeepSeek.
- 💰 Track debts between users
- 📊 View debt summaries and balances with visual representations
- 📝 Add notes to debt records
- 🧾 View transaction history
- 👥 Divide expenses among multiple users
- 📱 Set and display QR codes for payments
- 🔄 Clear debts with other users
- 👤 Set custom display names
- 🤖 Natural language processing using AI (Gemini, Grok, or DeepSeek)
- 🧠 Chat memory for better conversations per group
- 📦 Database backup and restoration
- 💾 Multi-group support with data isolation
- 🔄 Support for multiple AI models with easy switching
-
/adddebt <số_tiền> @username1 [@username2...] [ghi_chú]
- Add debt for one or more users- Example:
/adddebt 500 @toan Trà sữa
- Example:
/adddebt 10000 @toan @quy @tuan Tiền ăn
- Example:
-
/divide <số_tiền> @user1 @user2... [ghi_chú]
- Split expenses equally among specified users- Example:
/divide 900 @toan @quy @tuan Tiền ăn trưa
- Note: Only includes you in the calculation if you're tagged in the command
- Example:
-
/cleardebt @username1 [@username2...] [số_tiền]
- Clear debts (for one or more users)- Example:
/cleardebt @toan 500
- Example:
/cleardebt @toan @quy @tuan 200
- Example:
/cleardebt @toan @quy
(clears all debts)
- Example:
-
/summary [@username]
- View debt summary for yourself or another user- Example:
/summary
or/summary @toan
- Example:
-
/history [@username] [số_lượng]
- View transaction history- Example:
/history
or/history @toan 20
- Example:
-
/groupsum
- View group debt summary (group chats only)
-
/setqr <url_hình_ảnh>
- Save your QR code image URL- Example:
/setqr https://example.com/myqrcode.jpg
- Example:
-
/qr
- View your QR code- Example:
/qr
- Example:
-
/get @username qr
- View someone else's QR code- Example:
/get @toan qr
- Example:
-
/setname @username tên_hiển_thị
- Set display name- Example:
/setname @toan Anh Toàn
- Example:
-
/setmemorybank <số_tin_nhắn> [độ_dài_tóm_tắt]
- Configure the chat memory settings- First parameter (required): Number of messages to remember (default: 5, max: 20)
- Second parameter (optional): Summary character length (default: 150, range: 50-500)
- Example:
/setmemorybank 10
- Set to remember 10 messages with default summary length - Example:
/setmemorybank 10 200
- Remember 10 messages with 200-character summaries - Example:
/setmemorybank 0
- Disable memory bank completely - The bot uses AI to summarize conversations for better contextual responses
-
/help
- View help information
/status
- View system status and database information/shutdown
- Safely shut down the bot/backup
- Back up database and send the file via Telegram/switchmodel <model_name>
- Switch between AI models (gemini, grok, or deepseek)/showmemorybank
- Show current chat memory context for debugging purposes
TOKEN
- Your Telegram bot tokenDB_DIR
- Directory to store the database (defaults to ~/bot_data)GOOGLE_API_KEY
- Google API key for Gemini AI integrationDEEPSEEK_API_KEY
- DeepSeek API key for Deepseek AI integrationGROK_API_KEY
- X.AI API key for Grok AI integrationADMIN_IDS
- Comma-separated Fixed in code, list of Telegram user IDs with admin privileges
The bot supports multiple AI models for natural language processing:
-
Gemini (default)
- Requires
GOOGLE_API_KEY
environment variable - Uses Google's Gemini 2.0 Flash model
- Requires
-
Grok
- Requires
GROK_API_KEY
environment variable - Uses X.AI's Grok-3-mini-beta model
- Requires
-
DeepSeek
- Requires
DEEPSEEK_API_KEY
environment variable - Uses DeepSeek-V3 model (deepseek-chat)
- Requires
Admins can switch models during runtime using the /switchmodel
command.
The bot uses SQLite for data storage with the following tables:
debts
- Stores debt records with chat_id support for multi-group functionalityname_mappings
- Maps usernames to display names in specific chat contextsqr_codes
- Stores payment QR codes for users
The bot uses a memory bank feature to remember previous conversations in each group:
- Each group has its own memory bank (no cross-group memory leakage)
- The bot can remember up to 20 messages per group (configurable)
- AI-powered summaries are generated for each conversation exchange
- Summary length is configurable from 50 to 500 characters (default: 150)
- Instead of storing full conversations, the bot maintains concise summaries of key information
- This allows the bot to have more intelligent and context-aware conversations
- Users can adjust memory size and summary length with
/setmemorybank
command - Memory is stored in RAM (not persistent across bot restarts)
python-telegram-bot
sqlite3
google-generativeai
requests
openai # For DeepSeek support
- Clone the repository
- Install the requirements:
pip install -r requirements.txt
- Set the required environment variables
- Run the bot:
python bot.py
The bot supports natural language commands when mentioned. For example:
- "@DebtBot add debt 50k to @toan for coffee"
- "@DebtBot split 90k between me, @quy and @tuan"
- "@DebtBot how much do I owe?"
The bot will analyze the intent using the configured AI model and execute the appropriate command. The natural language processing includes:
- Pattern matching for common commands
- AI-powered intent recognition for complex requests
- Conversational responses for non-debt-related questions
- Vietnamese language support with cultural references
Admins can use the /backup
command to generate and receive a database backup file via Telegram. This file can later be placed in the bot's directory for restoration.
The bot separates data by chat_id, ensuring that debt information from one group doesn't appear in another. Admin commands are restricted to authorized user IDs only.