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.
Requirement | Version |
---|---|
Python | 3.11.1 |
Google Sheets | - |
python3 -m venv env
pip install -r requirements.txt
pytest
ptw # Run with watch
python3 ./bot.py
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]",
...
}
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=...
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
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()
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
scp google_credentials.json $USER@$SERVER:~/python/telegram-budgeter/
scp .env $USER@$SERVER:~/python/telegram-budgeter/
ssh $USER@$SERVER
cd ~/python/telegram-budgeter
git pull
# repeat "set up service" above