Skip to content

Commit d9d77f8

Browse files
committed
Merge branch 'develop'
2 parents 1bdcef1 + 308536b commit d9d77f8

File tree

8 files changed

+74
-20
lines changed

8 files changed

+74
-20
lines changed
File renamed without changes.

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
# Description
2+
This is the first version of my telegram bot which allows a simple expenses tracker.
23

3-
## Installtion
4-
* First create the `master.txt` and `token.txt` files, filled respectively with your master chat id and telegram bot token
4+
The bot can now take income and outcome and display the current month resume; here is a list of commands:
5+
* `/listoutcome` print the list of current month outcome
6+
* `/listincome` print the list of current month income
7+
* `/delete +/-<income/outcome amount> <comment>` delete the specified entry
8+
* `/balance` print total income, total outcome and the difference between those 2 (aka balance)
9+
* `+<income amount> <comment>` add to the income table the amount and the comment associated
10+
* `-<outcome amount> <comment>` add to the outcome table the amount and the comment associated
11+
12+
## Installation
13+
* First create the `master.txt` and `token.txt` files, filled respectively with your master chat id
14+
and telegram bot token
15+
* `Install docker`
16+
* Go into the clone folder and run `docker build moneytrakerBot/`
17+
* Copy the container id
18+
* Run `docker run <container id>`
572 Bytes
Binary file not shown.
222 Bytes
Binary file not shown.

bot.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
88

99
db = DBHelper()
10-
f = open("master.txt", "r")
11-
master = int(f.readline())
12-
if not master:
13-
logging.error("Error occurred, have you filled the master.txt file with your master id?")
14-
exit()
1510

1611

1712
handler = MessageHandler()
@@ -29,7 +24,8 @@
2924
# i found those code here https://apps.timwhitlock.info/emoji/tables/unicode :)
3025
emoji = {
3126
"moneybag": u'\U0001F4B0',
32-
"moneywings": u'\U0001F4B8'
27+
"moneywings": u'\U0001F4B8',
28+
"openhands": u'\U0001F450'
3329
}
3430

3531

@@ -70,16 +66,37 @@ def text_handler(text, chat_id):
7066
month = datetime.now().strftime("%m")
7167
rows = db.get_income(month)
7268
logging.info(rows)
73-
message = "Current month income list:\n\n"
74-
for r in rows:
75-
message = message + str(r).replace("(", "").replace(")", "").replace("'", "") + "\n"
69+
if rows:
70+
message = "Current month income list:\n\n"
71+
for r in rows:
72+
message = message + str(r).replace("(", "").replace(")", "").replace("'", "") + "\n"
73+
total_income = db.get_total_income(month)
74+
message = message+"\n\nTotal income: "+str(total_income)+" €"
75+
else:
76+
message = "No income to be displayed here " + emoji["openhands"]
7677
elif text == "/listoutcome":
7778
month = datetime.now().strftime("%m")
7879
rows = db.get_outcome(month)
7980
logging.info(rows)
80-
message = "Current month outcome list:\n\n"
81-
for r in rows:
82-
message = message+str(r).replace("(", "").replace(")", "").replace("'", "")+"\n"
81+
if rows:
82+
message = "Current month outcome list:\n\n"
83+
for r in rows:
84+
message = message+str(r).replace("(", "").replace(")", "").replace("'", "")+"\n"
85+
total_outcome = db.get_total_income(month)
86+
message = message+"\n\nTotal income: "+str(total_outcome)+" €"
87+
else:
88+
message = "No income to be displayed here " + emoji["openhands"]
89+
elif text == "/balance":
90+
month = datetime.now().strftime("%m")
91+
total_income = db.get_total_income(month)
92+
if total_income is None:
93+
total_income = 0.0
94+
total_outcome = db.get_total_outcome(month)
95+
if total_outcome is None:
96+
total_outcome = 0.0
97+
logging.info(total_outcome)
98+
balance = total_income + total_outcome
99+
message = "Current month balance: "+str(balance)+" €\n\n\nTotal income: "+str(total_income)+" €\n\nTotal outcome: "+str(total_outcome)+" €"
83100
handler.send_message(message, chat_id)
84101

85102

dbHelper.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,24 @@ def get_income(self, month):
4444
rows = cur.fetchall()
4545
return rows
4646

47+
def get_total_income(self, month):
48+
cur = self.conn.cursor()
49+
stmt = "SELECT SUM(value) FROM income WHERE strftime('%m', date) = '" + month + "'"
50+
cur.execute(stmt)
51+
total = cur.fetchone()
52+
return total[0]
53+
4754
def get_outcome(self, month):
4855
cur = self.conn.cursor()
4956
stmt = "SELECT * FROM outcome WHERE strftime('%m', date) = '"+month+"'"
5057
cur.execute(stmt)
5158
rows = cur.fetchall()
5259
return rows
60+
61+
def get_total_outcome(self, month):
62+
cur = self.conn.cursor()
63+
stmt = "SELECT SUM(value) FROM outcome WHERE strftime('%m', date) = '" + month + "'"
64+
cur.execute(stmt)
65+
total = cur.fetchone()
66+
return total[0]
67+

messageHandler.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,30 @@
1212
if not TOKEN:
1313
logging.error("Error occurred, have you filled the token.txt file with your bot token?")
1414
exit()
15+
1516
URL = "https://api.telegram.org/bot{}/".format(TOKEN)
1617

18+
f = open("master.txt", "r")
19+
master = int(f.readline())
20+
if not master:
21+
logging.error("Error occurred, have you filled the master.txt file with your master id?")
22+
exit()
23+
1724

1825
class MessageHandler:
1926

2027
def __init__(self):
21-
self.master = 58677785
28+
self.master = master
2229
self.allowed = [self.master]
2330

2431
#
2532
def get_url(self, url):
26-
response = requests.get(url)
27-
content = response.content.decode("utf8")
33+
try:
34+
response = requests.get(url)
35+
content = response.content.decode("utf8")
36+
except requests.exceptions.ConnectionError:
37+
logging.info("Max retries exceed")
38+
content = ""
2839
return content
2940

3041
#

requirements.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
sqlite3
2-
logging
3-
datetime
41
requests==2.22.0
52
urllib3==1.24.2

0 commit comments

Comments
 (0)