-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibot.py
1391 lines (1202 loc) · 62.6 KB
/
fibot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sqlite3
from telegram.ext import Updater, CommandHandler, CallbackContext, CallbackQueryHandler, MessageHandler, Filters, ConversationHandler
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardRemove
import os
from datetime import datetime, timedelta
# Generic states
# STEP_1, STEP_2, STEP_3, STEP_4 = range(4)
STATE = {
"OPTION": None,
"MAIN_CATEGORY": None,
"CATEGORY": None,
"CATEGORY_ID": None,
"AMOUNT": None,
"TRANSACTION_ID": None,
"USER": None,
"BALANCE": None,
"PLANNED": None,
}
USER_ID_1 = 1234567890 # Add your telegram user ID #1 here
USER_ID_2 = 1234567891 # Add your telegram user ID #2 here
USER_NAME_1 = "ABC" # Your name #1 (any string)
USER_NAME_2 = "DEF" # Your name #2 (any string)
TOKEN = 'add your telegram bot token here'
def reset_state(option=True, main_category=True, category=True, amount=True, user=True, balance=True, limit=True, transaction=True, category_id=True):
if option:
STATE["OPTION"] = None
if main_category:
STATE["MAIN_CATEGORY"] = None
if category:
STATE["CATEGORY"] = None
if amount:
STATE["AMOUNT"] = None
if user:
STATE["USER"] = None
if balance:
STATE["BALANCE"] = None
if limit:
STATE["PLANNED"] = None
if transaction:
STATE["TRANSACTION_ID"] = None
if category_id:
STATE["CATEGORY_ID"] = None
# Function to get the current month's database name (based on year and month)
def get_db_name():
current_month = datetime.now().strftime("%Y_%m") # Format as 'YYYY_MM'
return f"finance_{current_month}.db"
# Function to initialize the database schema if it doesn't exist
def init_db():
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
# users table: id, name
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
)
""")
# categories table: id, name, limit, main_category
cursor.execute("""
CREATE TABLE IF NOT EXISTS categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
"limit" REAL NOT NULL,
main_category TEXT NOT NULL,
UNIQUE(name, main_category)
)
""")
# records table: id, user_id, category_id, amount, timestamp, note
cursor.execute("""
CREATE TABLE IF NOT EXISTS records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
category_id INTEGER NOT NULL,
amount REAL NOT NULL,
timestamp DATETIME DEFAULT (datetime('now','localtime')),
note TEXT,
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(category_id) REFERENCES categories(id)
)
""")
# # records table: id, user_id, category, amount, timestamp, note
# cursor.execute("""
# CREATE TABLE IF NOT EXISTS records (
# id INTEGER PRIMARY KEY AUTOINCREMENT,
# user_id INTEGER NOT NULL,
# category TEXT NOT NULL,
# amount REAL NOT NULL,
# timestamp DATETIME DEFAULT (datetime('now','localtime')),
# note TEXT
# )
# """)
# timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
# balance table: id, user_id, amount
cursor.execute("""
CREATE TABLE IF NOT EXISTS balance (
user_id INTEGER NOT NULL PRIMARY KEY,
amount REAL NOT NULL
)
""")
conn.commit()
conn.close()
# Function to check if the user is authorized (add user IDs in the list)
def is_authorized(update: Update):
authorized_users = [USER_ID_1, USER_ID_2] # Add authorized user IDs here
return update.effective_user.id in authorized_users
# Function to init user info
def init_user():
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
# Check if the user is already in the database
cursor.execute("SELECT id FROM users WHERE id = ?", (USER_ID_1,))
result = cursor.fetchone()
if not result:
cursor.execute("INSERT INTO users (id, name) VALUES (?, ?)", (USER_ID_1, 'USER_NAME_1'))
cursor.execute("SELECT id FROM users WHERE id = ?", (USER_ID_2,))
result = cursor.fetchone()
if not result:
cursor.execute("INSERT INTO users (id, name) VALUES (?, ?)", (USER_ID_2, 'USER_NAME_2'))
conn.commit()
conn.close()
# Function to get user name
def get_user_name(user_id):
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute("SELECT name FROM users WHERE id = ?", (user_id,))
result = cursor.fetchone()
conn.close()
return result[0] if result else "Unknown"
# Function to initialize balance from the previous month or set to 0 if not available
def initialize_balance():
current_db_name = get_db_name()
current_month = datetime.now().strftime("%Y_%m") # Format as 'YYYY_MM'
previous_month = current_month[:-2] + str(int(current_month[-2:]) - 1).zfill(2) # Get previous month
previous_db_name = f"finance_{previous_month}.db"
print(f"Current month: {current_month}, Previous month: {previous_month}")
# get the current balance
conn = sqlite3.connect(current_db_name)
cursor = conn.cursor()
cursor.execute("SELECT * FROM balance")
result = cursor.fetchall()
if result:
current_balance = {}
for user_id, balance in result:
current_balance[user_id] = balance
conn.close()
else:
# If the current month's database is empty, initialize the database based on the previous month's data
current_balance = {USER_ID_1: 0, USER_ID_2: 0}
# Check if a previous month's database exists
if os.path.exists(previous_db_name):
prev_conn = sqlite3.connect(previous_db_name)
prev_cursor = prev_conn.cursor()
# Query the previous month's database for the balance
# current_balance = {}
for user_id in [USER_ID_1, USER_ID_2]:
# current_balance[user_id] = 0
prev_cursor.execute("SELECT amount FROM balance WHERE user_id = ?", (user_id,))
result = prev_cursor.fetchone()
if result:
current_balance[user_id] += result[0]
print(f"Balance found for {get_user_name(user_id)}: {current_balance[user_id]}")
else:
print(f"No balance found for user {get_user_name(user_id)}")
print(f"Previous month initial balance: {current_balance}")
# Calculate total income and expenses for the previous month based on user ID
for user_id in [USER_ID_1, USER_ID_2]:
prev_cursor.execute("""
SELECT SUM(r.amount) FROM records r
JOIN categories c ON r.category = c.name
WHERE c.main_category = 'income' AND r.user_id = ?
""", (user_id,))
income = prev_cursor.fetchone()[0] or 0
print(f"Previous month income for {get_user_name(user_id)}: {income}")
prev_cursor.execute("""
SELECT SUM(r.amount) FROM records r
JOIN categories c ON r.category = c.name
WHERE c.main_category = 'expense' AND r.user_id = ?
""", (user_id,))
expenses = prev_cursor.fetchone()[0] or 0
print(f"Previous month expenses for {get_user_name(user_id)}: {expenses}")
# Remaining balance from the previous month
remaining_balance = income - expenses + current_balance[user_id]
print(f"Previous month remaining balance for {get_user_name(user_id)}: {remaining_balance}")
# Set the current balance to the remaining balance from the previous month
current_balance[user_id] = remaining_balance
prev_conn.close()
# Now set the balance in the new month’s database
conn = sqlite3.connect(current_db_name)
cursor = conn.cursor()
for user_id in [USER_ID_1, USER_ID_2]:
cursor.execute("REPLACE INTO balance (user_id, amount) VALUES (?, ?)", (user_id, current_balance[user_id]))
conn.commit()
conn.close()
# Function to initialize categories
def initialize_categories():
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
# Check if the categories are already in the database
# initialize income categories
subcategories = ['investment' , 'salary' , 'loan']
limits = [15000 , 5000 , 0 ]
main_category = 'income'
for subcategory_id, subcategory in enumerate(subcategories):
cursor.execute("SELECT name FROM categories WHERE name = ?", (subcategory,))
result = cursor.fetchone()
if not result:
print(f"Inserting {subcategory} with limit {limits[subcategory_id]}")
cursor.execute("INSERT INTO categories (name, 'limit', main_category) VALUES (?, ?, ?)", (subcategory, limits[subcategory_id], main_category))
subcategories = ['cafe' , 'food', 'subway' , 'bills' , 'rent']
limits = [100 , 400 , 62 + 62 , 300 , 650]
main_category = 'expense'
for subcategory_id, subcategory in enumerate(subcategories):
cursor.execute("SELECT name FROM categories WHERE name = ?", (subcategory,))
result = cursor.fetchone()
if not result:
print(f"Inserting {subcategory} with limit {limits[subcategory_id]}")
cursor.execute("INSERT INTO categories (name, 'limit', main_category) VALUES (?, ?, ?)", (subcategory, limits[subcategory_id], main_category))
conn.commit()
conn.close()
# Function to set the balance for the user
def set_balance(update: Update, user_id: int, balance: float):
if not is_authorized(update):
update.message.reply_text("Unauthorized access!")
return
try:
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute("SELECT amount FROM balance WHERE user_id = ?", (user_id,))
existing_balance = cursor.fetchone()
if existing_balance:
print(f"Existing balance: {existing_balance[0]}")
cursor.execute("REPLACE INTO balance (user_id, amount) VALUES (?, ?)", (user_id, balance))
conn.commit()
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text(f"Balance successfully updated to {balance} for {get_user_name(user_id)}", reply_markup=reply_markup)
conn.close()
except Exception as e:
print(f"Error: {e}")
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text("❕ Please provide a valid balance.", reply_markup=reply_markup)
# Add a transaction (income or expense)
def add_transaction(update: Update, category_id: str, amount: float, note, user_name=USER_NAME_1):
if not is_authorized(update):
update.message.reply_text("Unauthorized access!")
return
try:
if user_name == USER_NAME_1:
user_id = USER_ID_1
elif user_name == USER_NAME_2:
user_id = USER_ID_2
else:
update.message.reply_text("User not found!")
return
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
# # check if the category exists
# cursor.execute("SELECT id FROM categories WHERE id = ?", (category_id,))
# result = cursor.fetchone()
# if not result:
# update.message.reply_text("\U0001F605 This category doesn't exist. Use /addcat <in/out> <category> <limit> to add.")
# return
cursor.execute("INSERT INTO records (user_id, category_id, amount, note) VALUES (?, ?, ?, ?)", (user_id, category_id, amount, note))
conn.commit()
conn.close()
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.message.reply_text(f"✅ A transaction of {amount} has been added.", reply_markup=reply_markup)
except Exception as e:
update.callback_query.edit_message_text(f"✅ A transaction of {amount} has been added.", reply_markup=reply_markup)
except Exception as e:
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.message.reply_text("❕ Please provide a valid amount and category.", reply_markup=reply_markup)
except Exception as e:
update.callback_query.edit_message_text("❕ Please provide a valid amount and category.", reply_markup=reply_markup)
# Add a category (income or expense)
def add_category(update: Update, main_category: str, subcategory: str, limit: float):
if not is_authorized(update):
update.message.reply_text("Unauthorized access!")
return
try:
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
# If it's 'income', insert the category directly
if main_category == "income":
cursor.execute("INSERT INTO categories (name, 'limit', main_category) VALUES (?, ?, ?)",
(subcategory, limit, main_category))
# If it's 'expense', handle subcategory and insert it
elif main_category == "expense" and subcategory:
cursor.execute("INSERT INTO categories (name, 'limit', main_category) VALUES (?, ?, ?)",
(subcategory, limit, main_category))
conn.commit()
conn.close()
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
if subcategory:
update.message.reply_text(f"✅ '{subcategory}' with a planned limit of {limit} has been added to '{main_category}'.", reply_markup=reply_markup)
else:
update.message.reply_text(f"✅ '{subcategory}' with a planned limit of {limit} has been added to '{main_category}'.", reply_markup=reply_markup)
except Exception as e:
print(f"Error in add_category: {e}")
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text("❕ Please provide a valid category and limit. Please double-check the category name, it might already exist.", reply_markup=reply_markup)
def escape_markdown_v2(text: str) -> str:
"""Escape special characters for MarkdownV2 formatting."""
special_chars = r'_\*[\](`~>#+-.!|)' # List of characters that need escaping in MarkdownV2
for char in special_chars:
text = text.replace(char, '\\' + char) # Escape each special character
return text
# Summarize current/expected balance based on income and expenses
def summarize(update: Update, context: CallbackContext):
if not is_authorized(update):
update.message.reply_text("Unauthorized access!")
return
try:
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute('SELECT id, name, "limit", main_category FROM categories')
categories = cursor.fetchall()
if not categories:
update.message.reply_text("\U0001F605 database is empty.")
conn.close()
return
# Get balance for each user
current_balance = 0
for user_id in [USER_ID_1, USER_ID_2]:
cursor.execute("SELECT amount FROM balance WHERE user_id = ?", (user_id,))
user_balance = cursor.fetchone()
current_balance += user_balance[0] if user_balance else 0
# Calculate expected and real amounts
expected_income = 0
expected_expense = 0
real_income = 0
real_expense = 0
detailed_income = []
detailed_expense = []
# Calculate expected and real amounts for each category
for category_id, name, limit, main_category in categories:
cursor.execute("SELECT SUM(amount) FROM records WHERE category_id = ?", (category_id,))
real_amount = cursor.fetchone()[0] or 0
if main_category == "income":
expected_income += limit
real_income += real_amount
detailed_income.append([name, limit, real_amount])
elif main_category == "expense":
expected_expense += limit
real_expense += real_amount
detailed_expense.append([name, limit, real_amount])
remaining_balance = current_balance + real_income - real_expense
# Build the table header and rows
col_1_width = 7
col_2_width = 8
col_3_width = 8
col_4_width = 8
table_header_sum = "{:<{width1}}|{:<{width2}}|{:<{width3}}|{:<{width4}}\n".format("", " plan", " real", " remain", width1=col_1_width, width2=col_2_width, width3=col_3_width, width4=col_4_width) + "-" * (col_1_width + col_2_width + col_3_width + col_4_width) + "\n"
# table_header = "{:<{width1}}| {:<{width2}}| {:<{width3}}\n".format("", "plan", "real", width1=col_1_width, width2=col_2_width, width3=col_3_width) + "-" * (col_1_width + col_2_width + col_3_width) + "\n"
# Build summary table
# Escape the markdown special characters, including '.' with '\\.'
summary_table = "\n".join(
[f"{escape_markdown_v2(category).ljust(col_1_width)}|" + "{:.1f}".format(limit).rjust(col_2_width) + "|" + "{:.1f}".format(real).rjust(col_3_width) + "|" + "{:.1f}".format(limit - real).rjust(col_4_width) for category, limit, real in [["income", expected_income, real_income], ["expense", expected_expense, real_expense]]]
# [f"{escape_markdown_v2(category).ljust(col_1_width)}| " + "{:.1f}".format(limit).ljust(col_2_width) + "|" + "{:.1f}".format(real).ljust(col_3_width) for category, limit, real in [["income", expected_income, real_income], ["expense", expected_expense, real_expense]]]
)
col_1_width = 7
col_2_width = 8
col_3_width = 8
col_4_width = 8
table_header_income = "{:<{width1}}|{:<{width2}}|{:<{width3}}|{:<{width4}}\n".format("", " plan", " real", " remain", width1=col_1_width, width2=col_2_width, width3=col_3_width, width4=col_4_width) + "-" * (col_1_width + col_2_width + col_3_width + col_4_width) + "\n"
# table_header_income = "{:<{width1}}| {:<{width2}}| {:<{width3}}\n".format("", "plan", "real", width1=col_1_width, width2=col_2_width, width3=col_3_width) + "-" * (col_1_width + col_2_width + col_3_width) + "\n"
# Build detailed income table
detailed_income_table = "\n".join(
[f"{escape_markdown_v2(shorten_text(category, col_1_width, False)).ljust(col_1_width)}|" + "{:.1f}".format(limit).rjust(col_2_width) + "|" + "{:.1f}".format(real).rjust(col_3_width) + "|" + "{:.1f}".format(limit - real).rjust(col_4_width) for category, limit, real in detailed_income]
# [f"{escape_markdown_v2(category).ljust(col_1_width)}| " + "{:.1f}".format(limit).ljust(col_2_width) + "|" + "{:.1f}".format(real).ljust(col_3_width) for category, limit, real in detailed_income]
)
col_1_width = 7
col_2_width = 8
col_3_width = 8
col_4_width = 8
table_header_expense = "{:<{width1}}|{:<{width2}}|{:<{width3}}|{:<{width4}}\n".format("", " plan", " real", " remain", width1=col_1_width, width2=col_2_width, width3=col_3_width, width4=col_4_width) + "-" * (col_1_width + col_2_width + col_3_width + col_4_width) + "\n"
# table_header_expense = "{:<{width1}}| {:<{width2}}| {:<{width3}}\n".format("", "plan", "real", width1=col_1_width, width2=col_2_width, width3=col_3_width) + "-" * (col_1_width + col_2_width + col_3_width) + "\n"
# Build detailed expense table
detailed_expense_table = "\n".join(
[f"{escape_markdown_v2(shorten_text(category, col_1_width, False)).ljust(col_1_width)}|" + "{:.1f}".format(limit).rjust(col_2_width) + "|" + "{:.1f}".format(real).rjust(col_3_width) + "|" + "{:.1f}".format(limit - real).rjust(col_4_width) for category, limit, real in detailed_expense]
# [f"{escape_markdown_v2(category).ljust(col_1_width)}| " + "{:.1f}".format(limit).ljust(col_2_width) + "|" + "{:.1f}".format(real).ljust(col_3_width) for category, limit, real in detailed_expense]
# [f"{escape_markdown_v2(category).ljust(col_1_width)}| {str(limit).ljust(col_2_width)}| {str(real).ljust(col_3_width)}" for category, limit, real in detailed_expense]
)
# Create a summary text
summary_text = (
f"*💰 Income summary:*\n"
f"```\n{table_header_income}{detailed_income_table}\n```\n\n"
f"*💸 Expense summary:*\n"
f"```\n{table_header_expense}{detailed_expense_table}\n```"
f"\n\n*🏧 Overview:*\n"
f" - Last month balance: " + "{:.1f}".format(current_balance) + "\n"
f" - Current balance: " + "{:.1f}".format(remaining_balance) + "\n"
f"```\n{table_header_sum}{summary_table}\n```\n\n"
)
conn.close()
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(summary_text, parse_mode='Markdown', reply_markup=reply_markup)
except Exception as e:
print("Error in summarize")
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text("❕ Database might be empty.", reply_markup=reply_markup)
def shorten_text(text: str, max_length: int, with_dots=True) -> str:
if with_dots:
return (text[:max_length] + '...') if len(text) > max_length else text
else:
return text[:max_length] if len(text) > max_length else text
# Function to get detailed transaction information for income/expenses
def detail_transaction(update: Update, category: str):
if not is_authorized(update):
update.message.reply_text("Unauthorized access!")
return
# try:
main_category = category
if main_category == "in":
main_category = "income"
elif main_category == "out":
main_category = "expense"
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
# category in records is subcategory of category in categories
cursor.execute("SELECT category_id, categories.name, amount, timestamp, note FROM records JOIN categories ON records.category_id = categories.id WHERE main_category = ?", (main_category,))
transactions = cursor.fetchall()
if not transactions:
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(f"❕ No transactions found.", reply_markup=reply_markup)
conn.close()
return
# Build the table header and rows
col_1_width = 5
col_2_width = 8
col_3_width = 5
col_4_width = 7
table_header = "{:<{width1}}|{:<{width2}}|{:<{width3}}|{:<{width4}}\n".format("", " amount", "date", "note", width1=col_1_width, width2=col_2_width, width3=col_3_width, width4=col_4_width) + "-" * (col_1_width + col_2_width + col_3_width + col_4_width) + "\n"
# table_header = "{:<{width1}}| {:<{width2}}| {:<{width3}}\n".format("", "amount", "date", width1=col_1_width, width2=col_2_width, width3=col_3_width) + "-" * (col_1_width + col_2_width + col_3_width) + "\n"
# Build detailed transaction table
detailed_transaction_table = ""
for category_id, category, amount, timestamp, note in transactions:
category = escape_markdown_v2(category)
# keep only month and day
timestamp = timestamp[5:10]
note = escape_markdown_v2(note) if note else ""
print(category, amount, timestamp, note)
detailed_transaction = "\n".join(
[f"{shorten_text(category, col_1_width, False).ljust(col_1_width)}|" + "{:.1f}".format(amount).rjust(col_2_width) + "|" + timestamp.ljust(col_3_width) + "|" + shorten_text(note, col_4_width).ljust(col_4_width)]
# detailed_transaction = "\n".join(
# [f"{category.ljust(col_1_width)}| " + "{:.1f}".format(amount).ljust(col_2_width) + "|" + timestamp.ljust(col_3_width)]
# [f"{category.ljust(col_1_width)}| {str(amount).ljust(col_2_width)}| {str(timestamp).ljust(col_3_width)}"]
)
detailed_transaction_table = detailed_transaction_table + detailed_transaction + "\n"
# Create a detailed transaction text
detailed_transaction_text = (
f"*Detailed {main_category} transactions:*\n"
f"```\n{table_header}{detailed_transaction_table}\n```"
)
conn.close()
keyboard = [
[InlineKeyboardButton("⬅️ Back", callback_data='view_transactions')],
[InlineKeyboardButton("❌ Close", callback_data='home')],
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(detailed_transaction_text, parse_mode='Markdown', reply_markup=reply_markup)
# except Exception as e:
# keyboard = [
# [InlineKeyboardButton("❌ Close", callback_data='home')]
# ]
# reply_markup = InlineKeyboardMarkup(keyboard)
# update.callback_query.edit_message_text(f"❌ Database for {category} might be empty.", reply_markup=reply_markup)
def view_main_category(update: Update, context: CallbackContext, backto_loc='home', backto_text='⬅️ Back'):
try:
keyboard = [
[InlineKeyboardButton("Income", callback_data='mcat:income')],
[InlineKeyboardButton("Expense", callback_data='mcat:expense')],
[InlineKeyboardButton("❌ Cancel", callback_data='home')],
# [InlineKeyboardButton(backto_text, callback_data=backto_loc)],
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(text="Choose one of the follows:", reply_markup=reply_markup)
except Exception as e:
print("Error in view_main_category")
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text("❕ Database might be empty.", reply_markup=reply_markup)
def view_category(update: Update, context: CallbackContext, backto_loc='view_main_category', backto_text='⬅️ Back'):
try:
# Loop through the income categories and create buttons
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
main_category = STATE["MAIN_CATEGORY"]
if not main_category:
return
cursor.execute("SELECT id, name FROM categories WHERE main_category = ?", (main_category,))
categories = cursor.fetchall()
conn.close()
keyboard = []
for category in categories:
keyboard.append([InlineKeyboardButton(category[1], callback_data=f'scat:' + str(category[0]))])
# keyboard.append([InlineKeyboardButton(backto_text, callback_data=backto_loc)])
keyboard.append([InlineKeyboardButton("❌ Cancel", callback_data='home')])
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(text="Choose a category:", reply_markup=reply_markup)
except Exception as e:
print("Error in view_category")
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text("❕ Database might be empty.", reply_markup=reply_markup)
# Function to list all transactions using buttons
def view_transactions(update: Update, context: CallbackContext):
# try:
# Loop through the income categories and create buttons
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
# cursor.execute("SELECT * FROM records")
cursor.execute("SELECT records.id, user_id, category_id, amount, timestamp, note, categories.id, categories.name FROM records JOIN categories ON records.category_id = categories.id")
transactions = cursor.fetchall()
# check if the database is empty
if not transactions:
conn.close()
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text("❕ No transactions found.", reply_markup=reply_markup)
return
conn.close()
keyboard = []
for transaction in transactions:
keyboard.append([InlineKeyboardButton(str(transaction[0]) + ". " + transaction[7] + " | " + str(transaction[3]) + " | " + str(transaction[5]) + " @ " + transaction[4][5:10], callback_data='trans:' + str(transaction[0]))])
keyboard.append([InlineKeyboardButton("❌ Close", callback_data='home')])
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(text="Choose a transaction:", reply_markup=reply_markup)
# except Exception as e:
# print("Error in view_transactions")
# keyboard = [
# [InlineKeyboardButton("❌ Close", callback_data='home')]
# ]
# reply_markup = InlineKeyboardMarkup(keyboard)
# update.callback_query.edit_message_text("❌ Database might be empty.", reply_markup=reply_markup)
# Function to show the menu with buttons
def show_menu(update: Update, context: CallbackContext):
keyboard = [
[InlineKeyboardButton("🔍 Summary", callback_data='view_summary')],
[InlineKeyboardButton("🔍 Transaction History", callback_data='view_transactions')],
[InlineKeyboardButton("➕ Add Transaction", callback_data='add_transaction')],
[InlineKeyboardButton("➕ Add Category", callback_data='add_category')],
[InlineKeyboardButton("✍🏻 Modify Transaction", callback_data='modify_transaction')],
[InlineKeyboardButton("✍🏻 Modify Category", callback_data='modify_category')],
[InlineKeyboardButton("⚠️ Set Balance (not recommended)", callback_data='set_balance')],
]
reply_markup = InlineKeyboardMarkup(keyboard)
if update.message:
# hide the keyboard
# context.bot.send_message(chat_id=update.effective_chat.id, text="😃 How can I help you today?", reply_markup=ReplyKeyboardRemove())
update.message.reply_text(text="😃 How can I help you today?", reply_markup=reply_markup)
else:
# hide the keyboard
# context.bot.send_message(chat_id=update.effective_chat.id, text="😃 How can I help you today?", reply_markup=ReplyKeyboardRemove())
update.callback_query.edit_message_text(text="😃 How can I help you today?", reply_markup=reply_markup)
def view_user(update: Update, context: CallbackContext):
try:
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute("SELECT u.id, u.name, b.amount FROM users u, balance b WHERE u.id = b.user_id")
users = cursor.fetchall()
# add name names to list of buttons
keyboard = []
for user in users:
keyboard.append([InlineKeyboardButton(user[1] + " (" + str(user[2]) + ")", callback_data=user[0])])
keyboard.append([InlineKeyboardButton("❌ Close", callback_data='home')])
reply_markup = InlineKeyboardMarkup(keyboard)
conn.close()
update.callback_query.edit_message_text("Please select an account to set balance:", reply_markup=reply_markup)
except Exception as e:
print("Error in view_user")
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text("❕ Database might be empty.", reply_markup=reply_markup)
# Function to handle the button click actions
def process_button(update: Update, context: CallbackContext):
query = update.callback_query
query.answer() # Acknowledge the callback
print(f"Sate: {STATE}")
print(f"query.data: {query.data}")
# ----------- main operations -----------
# view balance summary
if query.data == 'view_summary':
reset_state()
STATE["OPTION"] = 'view_summary'
summarize(update, context) # Call the existing summarize function to display the summary
# view transaction history
if query.data == 'view_transactions':
reset_state()
STATE["OPTION"] = 'view_transactions'
view_main_category(update, context)
# add transaction
if query.data == 'add_transaction':
reset_state()
STATE["OPTION"] = 'add_transaction'
view_main_category(update, context)
# add category
if query.data == 'add_category':
reset_state()
STATE["OPTION"] = 'add_category'
view_main_category(update, context)
# modify transaction
if query.data == 'modify_transaction':
reset_state()
STATE["OPTION"] = 'modify_transaction'
view_transactions(update, context)
# modify category
if query.data == 'modify_category':
reset_state()
STATE["OPTION"] = 'modify_category'
view_main_category(update, context)
# set balance
if query.data == 'set_balance':
reset_state()
STATE["OPTION"] = 'set_balance'
view_user(update, context)
# home
if query.data == 'home':
reset_state()
show_menu(update, context)
# back to the previous menu
# if query.data == 'back':
# reset_state()
# show_menu(update, context)
# ----------- "detailed" operations -----------
if STATE["OPTION"] == 'view_transactions' and query.data.startswith('mcat:'):
reset_state()
main_category = query.data.split(':')[1]
detail_transaction(update, main_category)
# add transactions
if STATE["OPTION"] == 'add_transaction':
if query.data.startswith('mcat:'):
reset_state(option=False)
main_category = query.data.split(':')[1]
STATE["MAIN_CATEGORY"] = main_category
view_category(update, context)
elif query.data.startswith('scat:'):
reset_state(option=False, main_category=False)
category = query.data.split(':')[1]
STATE["CATEGORY"] = category
keyboard = [
[InlineKeyboardButton("❌ Cancel", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(text=f"#️⃣ Please provide the amount for the transaction:", reply_markup=reply_markup)
# transaction with empty note
elif query.data == 'note_nothing':
if STATE["MAIN_CATEGORY"] == 'income' or STATE["MAIN_CATEGORY"] == 'expense':
if STATE["CATEGORY"]:
if STATE["AMOUNT"]:
note = None
add_transaction(update, STATE["CATEGORY"], STATE["AMOUNT"], note)
reset_state()
# add category
if STATE["OPTION"] == 'add_category' and query.data.startswith('mcat:'):
reset_state(option=False)
main_category = query.data.split(':')[1]
STATE["MAIN_CATEGORY"] = main_category
keyboard = [
[InlineKeyboardButton("❌ Cancel", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(text=f"🏷️ Please provide a category name:", reply_markup=reply_markup)
# modify transaction
if STATE["OPTION"] == 'modify_transaction':
if query.data.startswith('trans:'):
STATE["TRANSACTION_ID"] = int(query.data.split(':')[1])
print(f"Transaction ID: {STATE['TRANSACTION_ID']}")
# ask to modify or delete
keyboard = [
[InlineKeyboardButton("✏️ Modify", callback_data='modify_trans')],
[InlineKeyboardButton("⛔ Delete", callback_data='delete_trans')],
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(text="❓ What do you want to do with this transaction?", reply_markup=reply_markup)
elif query.data == 'modify_trans':
STATE["OPTION"] = 'modify_transaction_amount'
print(f"Modifying transaction {STATE['TRANSACTION_ID']}")
# add a button with the current amount
conn = sqlite3.connect(get_db_name())
cursor = conn.cursor()
cursor.execute("SELECT amount FROM records WHERE id = ?", (STATE["TRANSACTION_ID"],))
amount = cursor.fetchone()[0]
conn.close()
keyboard = [
[InlineKeyboardButton(f"💰 {amount}", callback_data='modify_trans_amount')],
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(text="️#️⃣ Please provide the new amount for the transaction:", reply_markup=reply_markup)
elif query.data == 'delete_trans':
print(f"Deleting transaction {STATE['TRANSACTION_ID']}")
try:
# Ask for confirmation
keyboard = [
[InlineKeyboardButton("✅ Confirm", callback_data='confirm_delete')],
[InlineKeyboardButton("❌ Cancel", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(text="❓ Are you sure you want to delete this transaction?", reply_markup=reply_markup)
except Exception as e:
print("Error in delete transaction")
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text("❕ Database might be empty.", reply_markup=reply_markup)
elif query.data == 'confirm_delete':
print(f"Deleting transaction {STATE['TRANSACTION_ID']}")
try:
# Loop through the income categories and create buttons
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute("DELETE FROM records WHERE id = ?", (STATE["TRANSACTION_ID"],))
conn.commit()
conn.close()
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(text="Transaction deleted.", reply_markup=reply_markup)
except Exception as e:
print("Error in delete transaction")
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text("❕ Database might be empty.", reply_markup=reply_markup)
if STATE["OPTION"] == 'modify_transaction_amount' and query.data == 'modify_trans_amount':
# Get the current amount and current date
conn = sqlite3.connect(get_db_name())
cursor = conn.cursor()
cursor.execute("SELECT amount, timestamp FROM records WHERE id = ?", (STATE["TRANSACTION_ID"],))
res = cursor.fetchone()
conn.close()
amount = res[0]
timestamp = res[1]
conn.close()
STATE["AMOUNT"] = amount
print(f"Modifying transaction {STATE['AMOUNT']}")
keyboard = [
[InlineKeyboardButton("🗓️ " + timestamp[5:10], callback_data='modify_trans_date')],
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.callback_query.edit_message_text(text="🗓️ Please provide a new date for the transaction:", reply_markup=reply_markup)
except Exception as e:
update.message.reply_text(text="🗓️ Please provide a new date for the transaction:", reply_markup=reply_markup)
STATE["OPTION"] = 'modify_transaction_date'
if STATE["OPTION"] == 'modify_transaction_date' and query.data == 'modify_trans_date':
# Get the current date
conn = sqlite3.connect(get_db_name())
cursor = conn.cursor()
cursor.execute("SELECT timestamp FROM records WHERE id = ?", (STATE["TRANSACTION_ID"],))
timestamp = cursor.fetchone()[0]
conn.close()
# modify the transaction
date = timestamp
db_name = get_db_name()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute("UPDATE records SET amount = ?, timestamp = ? WHERE id = ?", (STATE["AMOUNT"], date, STATE["TRANSACTION_ID"]))
conn.commit()
conn.close()
keyboard = [
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.message.reply_text(f"✍🏻 Transaction has been updated.", reply_markup=reply_markup)
except Exception as e:
update.callback_query.edit_message_text(f"✍🏻 Transaction has been updated.", reply_markup=reply_markup)
reset_state()
# modify category
if STATE["OPTION"] == 'modify_category':
if query.data.startswith('mcat:'):
reset_state(option=False)
main_category = query.data.split(':')[1]
STATE["MAIN_CATEGORY"] = main_category
view_category(update, context)
elif query.data.startswith('scat:'):
reset_state(option=False, main_category=False)
category = query.data.split(':')[1]
STATE["CATEGORY_ID"] = category
keyboard = [
[InlineKeyboardButton("✏️ Modify", callback_data='modify_cat')],
[InlineKeyboardButton("⛔ Delete", callback_data='delete_cat')],
[InlineKeyboardButton("❌ Close", callback_data='home')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_text(text=f"❓ What do you want to do with this category?", reply_markup=reply_markup)
elif query.data == 'modify_cat':
print(f"Modifying category {STATE['CATEGORY']}")
# add a button with the current category name
conn = sqlite3.connect(get_db_name())
cursor = conn.cursor()
cursor.execute("SELECT name FROM categories WHERE id = ?", (STATE["CATEGORY_ID"],))
category = cursor.fetchone()[0]
conn.close()
keyboard = [
[InlineKeyboardButton(f"🏷️ {category}", callback_data='modify_cat_name')],