Skip to content

Benjamngarcia/telegram_expenses_bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Budget Bot - TypeScript Version

A multi-user Telegram bot for expense tracking, now written in TypeScript with PostgreSQL database support.

Features

  • 📊 Track personal expenses per user
  • 💰 Monthly expense reports
  • 🔒 Multi-user support with isolated data
  • 📱 Simple Telegram interface
  • 🗄️ PostgreSQL database for reliability

Prerequisites

  • Node.js 18+
  • PostgreSQL database
  • Telegram Bot Token

Setup

  1. Install dependencies:

    npm install
  2. Install development dependencies:

    npm install --save-dev @types/express @types/node @types/pg ts-node-dev typescript
  3. 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
    );
  4. Environment variables:

    Create a .env file with:

    TELEGRAM_BOT_TOKEN=your_bot_token_here
    POSTGRES_URL=postgresql://username:password@localhost:5432/database_name
  5. Build the project:

    npm run build

Development

  • Start in development mode (with hot reload):

    npm run dev
  • Build for production:

    npm run build
  • Start production server:

    npm start

Usage

Bot Commands

  • /start - Welcome message and instructions
  • /corte - Show current month's expense total

Adding Expenses

Simply send a message in the format: Description Amount

Examples:

  • Coffee 55
  • Lunch 150.50
  • Transportation 25

Deployment

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:

  1. Build the project: npm run build
  2. Deploy the dist folder
  3. Set environment variables in Vercel dashboard

Project Structure

├── 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

Type Safety

The TypeScript version includes:

  • Proper type definitions for Telegram API objects
  • Database query result types
  • Express request/response types
  • Environment variable validation

Error Handling

  • Input validation for expense entries
  • Database connection error handling
  • Graceful shutdown for database connections
  • Proper TypeScript error types

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make changes with proper TypeScript types
  4. Test thoroughly
  5. Submit a pull request

License

ISC

Made by @benjamngarcia

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors