Skip to content

A Telegram bot to help you keep track of daily average spending over time. Stores data in a Google Spreadsheet so you can view it anytime easily.

Notifications You must be signed in to change notification settings

alifeee/telegram-budgeter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Telegram Budgeter

This is a project to run on a server which sends me a Telegram message every day asking how much I spent the day before. The idea is to keep track of average daily spending.

Chat with "Bank Bot": "How much did you spend yesterday?" - "12.98" - "New average: 19.45"

Requirements

Requirement Version
Python 3.11.1
Google Sheets -

Commands

Set up environment

python3 -m venv env

Install dependencies

pip install -r requirements.txt

Test

pytest
ptw # Run with watch

Run

python3 ./bot.py

Google Credentials

Credentials are stored in google_credentials.json. Follow the gspread "Service Account" guide to set up a service account and download the credentials JSON file.

{
  "type": "service_account",
  "project_id": "telegram-budgeter",
  "private_key_id": "[PRIVATE KEY ID]",
  "private_key": "[PRIVATE KEY]",
  "client_email": "...@....gserviceaccount.com",
  "client_id": "[CLIENT ID]",
  ...
}

Telegram

Credentials

To obtain an access token for telegram, see help page, but in essence, talk to the BotFather.

The access token is used via an environment variable, or a .env file, which is not tracked by git.

Also in the environment should be an "admin ID", where errors are sent via the error handler.

touch .env
TELEGRAM_BOT_ACCESS_TOKEN=...
ADMIN_USER_ID=...

Change commands

To change the commands, talk to the BotFather and use the /setcommands command.

/setcommands
...
stats - Get spending statistics
spreadsheet - Get spreadsheet URL
spend - Set a day's spend
remind - Set reminders on/off
start - Set spreadsheet/restart
help - See help
privacy - See privacy information
cancel - cancel the current operation

Persistent data

To store each user's Google Sheet ID, a persistent pickle file is used. This is not tracked by git. This uses the Persistence API from python-telegram-bot.

persistent_data = PicklePersistence(filepath="bot_data.pickle")
application = Application.builder().token(API_KEY).persistence(persistent_data).build()

Deploy on remote server

Initial deployment

ssh $USER@$SERVER
cd ~/python
git clone https://github.com/alifeee/telegram-budgeter.git
cd telegram-budgeter
sudo apt-get update
sudo apt install python3.10-venv

cd ~/python/telegram-budgeter
python3 -m venv env
pip install -r requirements.txt
# set up service
cp telegram-budgeter.service /etc/systemd/system/telegram-budgeter.service
sudo systemctl enable telegram-budgeter.service
sudo systemctl start telegram-budgeter.service
sudo systemctl status telegram-budgeter.service

Move over secrets

scp google_credentials.json $USER@$SERVER:~/python/telegram-budgeter/
scp .env $USER@$SERVER:~/python/telegram-budgeter/

Update deployment

ssh $USER@$SERVER
cd ~/python/telegram-budgeter
git pull
# repeat "set up service" above

About

A Telegram bot to help you keep track of daily average spending over time. Stores data in a Google Spreadsheet so you can view it anytime easily.

Topics

Resources

Stars

Watchers

Forks

Languages