Skip to content

Commit 3ac555b

Browse files
authored
V2.3.4 dev change and faq (#43)
1 parent f6e5328 commit 3ac555b

File tree

15 files changed

+296
-139
lines changed

15 files changed

+296
-139
lines changed

.github/workflows/main.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Python application test
2+
3+
on:
4+
push:
5+
branches: [ '*' ]
6+
7+
jobs:
8+
build:
9+
10+
runs-on: ubuntu-latest
11+
environment: Test
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Set up Python 3.9.13
15+
uses: actions/setup-python@v2
16+
with:
17+
python-version: 3.9.13
18+
- name: Install dependencies
19+
run: |
20+
python -m pip install --upgrade pip
21+
pip install -r requirements.txt
22+
- name: Run tests
23+
env:
24+
FIREBASE_JSON: ${{ secrets.FIREBASE_JSON }}
25+
run: |
26+
python -m pytest

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,7 @@ cython_debug/
161161
.vercel
162162

163163
# My stuff
164-
test_func.py
164+
test_func.py
165+
users_backup.json
166+
one_off_func.py
167+
*.json

FAQ.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## FAQ on TeleFinance Tracker Bot
2+
3+
### 1. Sometimes the bot doesn't reply when I enter data. What should I do?
4+
The bot may occasionally encounter issues due to hosting limitations. If it doesn't respond, try waiting, or use `/cancel` and re-enter the data. Most of the time, `/cancel` would solve majority of your issues!
5+
6+
### 2. My entry ends up in the wrong category. How do I fix this?
7+
When keying new entry, do ensure that you have received the "Transaction logged." message before adding a new one. For your current incorrect entries, you can manually adjust them in the Google Sheet.
8+
9+
### 3. How do I add past transactions or entries for previous months?
10+
For entries from past months, you can manually add them directly to the Google Sheet. For transactions earlier in the current month, use the `/backlog` command.
11+
12+
### 4. How do I delete or edit a past entry?
13+
To delete or edit past entries, manually adjust them in the Google Sheet. Remember not to shift the remaining entries up, as this could disrupt new entries.
14+
15+
### 5. Can I add or edit quick settings for `/quickothers` or `/quicktransport`?
16+
Yes, you can customize these settings by modifying the tracker tab in the Google Sheet. There is a limit set in the bot to prevent excessively long lists.
17+
18+
### 6. How do I view all the commands available?
19+
You can view all available commands by opening the menu in the Telegram chat or by typing `/help`.
20+
21+
### 7. I edited the Google Sheet, but the bot doesn't seem to recognize the changes. What should I do?
22+
Do ensure that `TRACKER` under the Tracker tab is correctly updated. The first row refers to the row number the first entry is on, the Transport Row and Other Row should refers to the last entry of the respective category. This means there's possiblity that the Other or Transport Row is -1 of first row.
23+
24+
![tracker fixed](https://github.com/brucewzj99/tele-tracker-v2/doc-image/faq-tracker.png)
25+
26+
27+
### If you have additional questions or need further assistance, feel free to [open a new issue](https://github.com/brucewzj99/tele-tracker-v2/issues) on GitHub.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Bruce Wang
3+
Copyright (c) 2024 Bruce Wang
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ A python telegram bot to help track daily expenses onto google sheet, hosted on
66
## Release Notes
77
You can find the release notes over [here](https://github.com/brucewzj99/tele-tracker-v2/blob/master/release_notes.md).
88

9+
## FAQ
10+
You can find the FAQ over [here](https://github.com/brucewzj99/tele-tracker-v2/blob/main/FAQ.md).
11+
912
## Table of Contents
1013
- [Getting Started (Users)](#getting-started-users)
1114
- [Getting Started (Developers)](#getting-started-developers)
@@ -35,15 +38,14 @@ pip install -r requirements.txt
3538
* Set up Google Sheet API, download service account key
3639
* Retrieve Google Sheet API email
3740
* Set up Firebase Firestore Database, download service account key
38-
* Retrieve your firebase database url
3941
* Set up telegram bot via [BotFather](https://t.me/BotFather)
4042
* Insert all of them into .env as follows, you can use py dotenv or set it as env variable in your venv
4143
* You can use the same TOKEN for BOT_TOKEN & TEST_TOKEN but I recommend using two different bots for testing and production
44+
* I recommend having two different firebase databases for testing and production
4245

4346
``` .env
4447
BOT_TOKEN=your_bot_token
4548
TEST_TOKEN=your_test_token
46-
DATABASE_URL=firebase_url
4749
GOOGLE_API_EMAIL=google_api_email
4850
FIREBASE_JSON=service_account_key
4951
GOOGLE_JSON=service_account_key
@@ -53,9 +55,9 @@ MASTER_TELE_ID=your_telegram_id
5355
### Step 3
5456
* Proceed to project directory and run:
5557
``` python
56-
python3.9 test_polling.py
58+
python3.9 dev_polling.py
5759
OR
58-
python3.9 test_webhook.py
60+
python3.9 dev_webhook.py
5961
```
6062

6163
## Usage

bot/database_service/firestore_service.py

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,93 @@
11
from bot.database_service.auth import get_db_client
2-
from datetime import datetime
2+
from datetime import datetime, timedelta
3+
import pytz
4+
35

46
class FirestoreService:
57
"""
68
This class is responsible for managing the Firestore database.
79
"""
810

9-
def __init__(self):
11+
def __init__(self, collection_name="users"):
1012
self.db = get_db_client()
13+
self.collection_name = collection_name
1114

1215
# New user setup
1316
def new_user_setup(self, telegram_id, sheet_id, telegram_username):
14-
user_ref = self.db.collection("users").document(str(telegram_id))
15-
user_ref.set({
16-
"sheet_id": sheet_id,
17-
"datetime_created": datetime.now(),
18-
"username": telegram_username
19-
})
17+
user_ref = self.db.collection(self.collection_name).document(str(telegram_id))
18+
timestamp = datetime.now(pytz.timezone("Asia/Singapore"))
19+
user_ref.set(
20+
{
21+
"sheet_id": sheet_id,
22+
"datetime_created": timestamp,
23+
"username": telegram_username,
24+
"usage_count": 0,
25+
"last_accessed": timestamp,
26+
"hourly_accessed": timestamp,
27+
"overusage_count": 0,
28+
}
29+
)
2030

2131
# Check if user exists
2232
def check_if_user_exists(self, telegram_id):
23-
user_ref = self.db.collection("users").document(str(telegram_id))
33+
user_ref = self.db.collection(self.collection_name).document(str(telegram_id))
2434
user_doc = user_ref.get()
2535
return user_doc.exists
2636

2737
# Get user sheet id
28-
def get_user_sheet_id(self, telegram_id):
29-
user_ref = self.db.collection("users").document(str(telegram_id))
38+
def get_user_sheet_id(self, telegram_id, telegram_username):
39+
user_ref = self.db.collection(self.collection_name).document(str(telegram_id))
3040
user_doc = user_ref.get()
41+
3142
if user_doc.exists:
32-
return user_doc.get("sheet_id")
33-
else:
34-
return None
43+
try:
44+
# Update username if it is different
45+
if user_doc.get("username") != telegram_username:
46+
user_ref.update({"username": telegram_username})
47+
48+
# Get the current time
49+
now = datetime.now(pytz.timezone("Asia/Singapore"))
50+
# Retrieve the hourly accessed time
51+
hourly_accessed = user_doc.get("hourly_accessed")
52+
53+
if not hourly_accessed:
54+
hourly_accessed = now
55+
56+
usage_count = user_doc.get("usage_count")
57+
overusage_count = user_doc.get("overusage_count")
58+
if (now - hourly_accessed) < timedelta(hours=1):
59+
if usage_count < 30:
60+
usage_count += 1
61+
else:
62+
overusage_count += 1 # Increment overusage count if limit reached within the hour
63+
else:
64+
# Reset if a new hour has started
65+
usage_count = 1
66+
hourly_accessed = now
67+
68+
# Update the last accessed time, usage count and overusage count
69+
user_ref.update(
70+
{
71+
"last_accessed": now,
72+
"hourly_accessed": hourly_accessed,
73+
"usage_count": usage_count,
74+
"overusage_count": overusage_count,
75+
}
76+
)
77+
return user_doc.get("sheet_id")
78+
except Exception as e:
79+
raise e
80+
return None
3581

3682
# Get all user IDs
3783
def get_all_user_id(self):
38-
users_ref = self.db.collection("users")
84+
users_ref = self.db.collection(self.collection_name)
3985
user_ids = [int(user.id) for user in users_ref.stream()]
4086
return user_ids
4187

4288
# Get all sheet IDs
4389
def get_all_sheet_id(self):
44-
users_ref = self.db.collection("users")
90+
users_ref = self.db.collection(self.collection_name)
4591
sheet_ids = []
4692
for user in users_ref.stream():
4793
sheet_ids.append(user.get("sheet_id"))

bot/firestore_config.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

bot/firestore_service.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)