Telegram bot for tracking expenses and syncing them to Google Sheets.
- π° Track expenses via Telegram messages
- π Automatic sync to Google Sheets
- π± Multi-currency support (USD, EUR, RUB, RSD, GBP, CHF, JPY, CNY, INR)
- π·οΈ Automatic category detection with confirmation
- π Statistics and expense analysis
- π Secure OAuth2 authentication with Google
- π€ AI-powered expense analysis and advice (Hugging Face)
- πΌ Budget management and tracking
- π₯ Group chat support with shared expenses
- ποΈ Daily AI financial advice (scheduled)
The bot understands various expense formats:
190 Π΅Π²ΡΠΎ Alex hobby
190Π΅ Alex hobby
190Π΄ Alex hobby
190$ Alex hobby
190 $ Alex hobby
$190 Alex hobby
$ 190 Π΅Π²ΡΠΎ Alex hobby
190 euro Alex hobby
190 Eur Alex hobby
190 EUR Alex hobby
1 900 RSD Lena hobby theater
Format: [amount] [currency?] [category] [comment]
bun install- Open @BotFather in Telegram
- Send
/newbotand follow instructions - Copy the bot token
- Go to Google Cloud Console
- Create a new project or select existing
- Enable Google Sheets API and Google Drive API:
- APIs & Services β Library
- Search for "Google Sheets API" and enable
- Search for "Google Drive API" and enable
- Create OAuth 2.0 credentials:
- APIs & Services β Credentials
- Click "Create Credentials" β "OAuth client ID"
- Application type: "Web application"
- Authorized redirect URIs:
http://localhost:3000/callback - Copy Client ID and Client Secret
- Go to Hugging Face
- Create account or sign in
- Go to Settings β Access Tokens
- Create new token with "Read" access
- Copy the token (starts with
hf_...)
Create .env file:
cp .env.example .envEdit .env with your credentials:
# Telegram Bot (from @BotFather)
BOT_TOKEN=your_telegram_bot_token
# Google OAuth (from Google Cloud Console)
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
GOOGLE_REDIRECT_URI=http://localhost:3000/callback
# OAuth Server
OAUTH_SERVER_PORT=3000
# Database
DATABASE_PATH=./data/expenses.db
# Encryption (generate with: openssl rand -hex 32)
ENCRYPTION_KEY=your_32_byte_hex_encryption_key
# Environment
NODE_ENV=development
# Hugging Face (optional, for AI features)
HF_TOKEN=your_hugging_face_api_tokenGenerate encryption key:
openssl rand -hex 32Development mode (with auto-reload):
bun run devProduction mode:
bun run startYou should see:
π Starting ExpenseSyncBot...
π¦ Initializing database...
β Database ready
π Starting OAuth server...
β OAuth server running on http://localhost:3000
π€ Starting Telegram bot...
β Bot started successfully
β
ExpenseSyncBot is running!
Press Ctrl+C to stop
- Start a chat with your bot in Telegram
- Send
/start - Send
/connectto begin Google authorization - Click the authorization link
- Grant permissions in Google
- Select default currency
- Select additional currencies
- Bot will create a Google Sheet and you're ready!
Simply send a message in any supported format:
100 Π΅Π΄Π° ΠΎΠ±Π΅Π΄ Π² ΡΠ΅ΡΡΠΎΡΠ°Π½Π΅
50$ ΡΡΠ°Π½ΡΠΏΠΎΡΡ ΡΠ°ΠΊΡΠΈ
1900 RSD ΡΠ°Π·Π²Π»Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠΈΠ½ΠΎ
The bot will:
- Parse the expense
- Show you the parsed data
- Ask for confirmation if it's a new category
- Save to Google Sheets
- Confirm the save
Basic Commands:
/start- Welcome message and setup status/connect- Connect Google account and setup/reconnect- Reconnect Google account
Expense Management:
/stats- View expense statistics/sum(or/total) - Sum expenses by filters (category, currency, date range)/sync- Manual sync to Google Sheets/categories- List all categories
Budget & Planning:
/budget- Create and manage budgets
Spreadsheet:
/spreadsheet- Get link to your Google Sheet
AI Features (Groups):
@botname <question>- Ask AI about your expenses (mention bot in groups)/advice- Get daily AI financial advice/prompt- Manage custom AI system prompt for your group
Settings:
/settings- View current settings
The bot includes AI-powered expense analysis using Hugging Face models:
- Ask Questions: Mention the bot in group chats with your question (e.g.,
@botname how much did I spend on food this month?) - Daily Advice: Set up scheduled financial advice using
/advicecommand - Custom Prompts: Configure AI behavior per-group using
/promptcommand - Context-Aware: AI has access to all expenses, budgets, and categories
AI features require HF_TOKEN in .env file.
The bot works in both personal chats and group chats:
- Personal: Each user has their own spreadsheet, categories, and expenses
- Groups: Shared spreadsheet and budgets, multiple users can contribute expenses
- AI features are available in groups (mention bot to ask questions)
src/
βββ bot/
β βββ commands/ # Bot commands (/start, /connect, etc.)
β βββ handlers/ # Message and callback handlers
β βββ keyboards.ts # Inline keyboard builders
βββ database/
β βββ repositories/ # Data access layer
β βββ schema.ts # Database migrations
β βββ types.ts # TypeScript types
βββ services/
β βββ google/
β β βββ oauth.ts # OAuth2 flow
β β βββ sheets.ts # Google Sheets API
β βββ currency/
β βββ parser.ts # Expense message parser
β βββ converter.ts # Currency conversion
βββ web/
β βββ oauth-callback.ts # OAuth callback server
βββ config/
βββ constants.ts # App constants
βββ env.ts # Environment config
- Runtime: Bun
- Telegram: GramIO
- Database: SQLite (bun:sqlite)
- Google API: googleapis
- AI: Hugging Face Inference API
- Currency: currency.js
- Date Utils: date-fns
# Development mode with auto-reload
bun run dev
# Production mode
bun run start
# Type checking
bun run type-check
# or directly:
bunx tsc --noEmitSQLite database is stored in ./data/expenses.db (gitignored).
Migrations run automatically on startup. See src/database/schema.ts for migration logic.
Important: Never modify deployed migrations - always add new ones.
Edit src/config/constants.ts:
- Add aliases to
CURRENCY_ALIASES - Add code to
SUPPORTED_CURRENCIES - Add symbol to
CURRENCY_SYMBOLS - Add exchange rate in src/services/currency/converter.ts
- Create handler in
src/bot/commands/ - Register in src/bot/index.ts
- Add to README commands list
The bot is deployed on Digital Ocean using PM2 process manager. See DEPLOY.md for complete deployment guide.
Key features:
- PM2 for process management and auto-restart
- Caddy reverse proxy for HTTPS OAuth callbacks
- GitHub Actions for auto-deployment
- SQLite database persisted in
./data/
Make sure the redirect URI in .env matches exactly what you configured in Google Cloud Console.
- Check bot token is correct
- Make sure bot is running (
bun run devorbun run start) - Check logs for errors
- Check OAuth token is valid (
/reconnect) - Verify spreadsheet wasn't deleted
- Check Google API quotas
- Verify
HF_TOKENis set in.env - Check Hugging Face API status
- Make sure you're using the bot in a group (for AI questions)
- Check logs for API errors
MIT
Built with Bun, GramIO, Claude code, and β€οΈ