Backend API for personal finance management Telegram bot. Made with Java, Spring Boot 2, Spring Data and Telegram Bot Java Library.
First, go to BotFather, create your bot and get your bot token. Then register following commands for your bot:
/category
- Expenses category
/today
- Expenses statistics for today
/month
- Expenses statistics for the last month
/latest
- Recently added expense
Now you need to create database for the bot. Though you can use any RDBMS, PostgreSQL is recommended and supported by default. Assuming you've already installed Postgres, go to your terminal and launch psql (PostgreSQL shell). In psql enter the following:
CREATE DATABASE IF NOT EXISTS tg_finance_bot;
\c tg_finance_bot
CREATE TABLE IF NOT EXISTS users (
chat_id BIGINT PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255),
tg_username VARCHAR(255),
registered_at TIMESTAMP NOT NULL
);
CREATE TABLE IF NOT EXISTS categories (
category_id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS expenses (
expense_id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
amount NUMERIC(11, 2) NOT NULL DEFAULT 0,
category_id INT NOT NULL,
added_on TIMESTAMP NOT NULL,
CONSTRAINT fk_user FOREIGN KEY(user_id)
REFERENCES users(chat_id) ON DELETE CASCADE,
CONSTRAINT fk_category FOREIGN KEY(category_id)
REFERENCES categories(category_id) ON DELETE CASCADE
);
Full SQL code for creating database tables provided in sql/data.sql
file.
Go to application.properties
file in src/main/resources
folder and set up bot credentials and database settings.
bot.name
- bot name
bot.key
- bot token
spring.datasource.url
- database URL (jdbc:postgresql://localhost:5432/tg_finance_bot
by default)
spring.datasource.username
- database user
spring.datasource.password
- database password
Build project using Maven:
./mvnw clean package -DskipTests
Launch bot on your local machine:
java -jar target/telegram-finance-bot-0.1.0-alpha.jar
For launching this bot inside Docker container go to project root folder and enter in your terminal:
docker compose up
This command will also create database called 'tg_finance_bot' for you. To
set a different name for your DB, go to docker-compose.yml
and change POSTGRES_DB
property.
To enter psql (PostgreSQL shell):
docker exec -it pgdb psql -h localhost -p 5432 -d <YOUR_DB> -U <YOUR_USER>