A multi-user Telegram bot for expense tracking, now written in TypeScript with PostgreSQL database support.
- 📊 Track personal expenses per user
- 💰 Monthly expense reports
- 🔒 Multi-user support with isolated data
- 📱 Simple Telegram interface
- 🗄️ PostgreSQL database for reliability
- Node.js 18+
- PostgreSQL database
- Telegram Bot Token
-
Install dependencies:
npm install
-
Install development dependencies:
npm install --save-dev @types/express @types/node @types/pg ts-node-dev typescript
-
Set up your database:
Create a PostgreSQL database and run the SQL commands from
bd.sql:-- See bd.sql file for complete schema CREATE TABLE users ( user_id BIGINT PRIMARY KEY, username TEXT, phone TEXT NULL, registration_date TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE expenses ( id SERIAL PRIMARY KEY, user_id BIGINT NOT NULL REFERENCES users(user_id), description TEXT NOT NULL, amount NUMERIC(10, 2) NOT NULL, date TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP );
-
Environment variables:
Create a
.envfile with:TELEGRAM_BOT_TOKEN=your_bot_token_here POSTGRES_URL=postgresql://username:password@localhost:5432/database_name
-
Build the project:
npm run build
-
Start in development mode (with hot reload):
npm run dev
-
Build for production:
npm run build
-
Start production server:
npm start
/start- Welcome message and instructions/corte- Show current month's expense total
Simply send a message in the format: Description Amount
Examples:
Coffee 55Lunch 150.50Transportation 25
This bot is designed to work with Vercel's serverless functions. The main entry point is api/bot.ts which exports an Express app.
For Vercel deployment:
- Build the project:
npm run build - Deploy the
distfolder - Set environment variables in Vercel dashboard
├── api/
│ ├── bot.ts # Main bot logic (TypeScript)
│ └── bot.js # Original JavaScript version
├── types/
│ └── telegram.ts # TypeScript type definitions
├── dist/ # Compiled TypeScript output
├── bd.sql # Database schema
├── bot.py # Python version (legacy)
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── README.md # This file
The TypeScript version includes:
- Proper type definitions for Telegram API objects
- Database query result types
- Express request/response types
- Environment variable validation
- Input validation for expense entries
- Database connection error handling
- Graceful shutdown for database connections
- Proper TypeScript error types
- Fork the repository
- Create a feature branch
- Make changes with proper TypeScript types
- Test thoroughly
- Submit a pull request
ISC