Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alifeee/telegram-budgeter
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.
https://github.com/alifeee/telegram-budgeter
google-sheets gspread telegram-bot
Last synced: 8 days ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/alifeee/telegram-budgeter
- Owner: alifeee
- Created: 2023-04-28T18:40:12.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-19T00:04:10.000Z (9 months ago)
- Last Synced: 2024-02-20T00:57:07.130Z (9 months ago)
- Topics: google-sheets, gspread, telegram-bot
- Language: Python
- Homepage: https://t.me/daily_budgeter_bot
- Size: 480 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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"](images/conversation.png)
## Requirements
| Requirement | Version |
| ----------- | ------- |
| Python | 3.11.1 |
| Google Sheets | - |## Commands
### Set up environment
```bash
python3 -m venv env
```### Install dependencies
```bash
pip install -r requirements.txt
```### Test
```bash
pytest
ptw # Run with watch
```### Run
```bash
python3 ./bot.py
```## Google Credentials
Credentials are stored in `google_credentials.json`. Follow the [gspread "Service Account" guide][gspread-guide] to set up a service account and download the credentials JSON file.
```json
{
"type": "service_account",
"project_id": "telegram-budgeter",
"private_key_id": "[PRIVATE KEY ID]",
"private_key": "[PRIVATE KEY]",
"client_email": "[email protected]",
"client_id": "[CLIENT ID]",
...
}
```[gspread-guide]: https://docs.gspread.org/en/latest/oauth2.html#for-bots-using-service-account
## Telegram
### Credentials
To obtain an access token for telegram, see [help page](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Introduction-to-the-API), but in essence, talk to the [BotFather](https://t.me/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.
```bash
touch .env
``````.env
TELEGRAM_BOT_ACCESS_TOKEN=...
ADMIN_USER_ID=...
```### Change commands
To change the commands, talk to the [BotFather](https://t.me/botfather) and use the `/setcommands` command.
```text
/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](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Making-your-bot-persistent) from [python-telegram-bot][ptb].
[ptb]: https://github.com/python-telegram-bot/python-telegram-bot/
```python
persistent_data = PicklePersistence(filepath="bot_data.pickle")
application = Application.builder().token(API_KEY).persistence(persistent_data).build()
```## Deploy on remote server
### Initial deployment
```bash
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-venvcd ~/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
```bash
scp google_credentials.json $USER@$SERVER:~/python/telegram-budgeter/
scp .env $USER@$SERVER:~/python/telegram-budgeter/
```### Update deployment
```bash
ssh $USER@$SERVER
cd ~/python/telegram-budgeter
git pull
# repeat "set up service" above
```