Skip to content

Commit 90de36c

Browse files
committed
v2.3.1 - [BUGFIX] url sanitize and month
1 parent ec7b1e3 commit 90de36c

File tree

4 files changed

+79
-46
lines changed

4 files changed

+79
-46
lines changed

bot/google_sheet_service.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def get_sub_dropdown_value(spreadsheet_id, main_value, entry_type):
9090

9191

9292
def update_prev_day(spreadsheet_id, month, first_row, last_row=0):
93+
month = month.title()
94+
9395
if last_row == 0:
9496
last_row = get_last_entered_row(spreadsheet_id, month)
9597
body = {"values": [[f"=SUM(C{first_row}:H{last_row})"]]}
@@ -103,6 +105,8 @@ def update_prev_day(spreadsheet_id, month, first_row, last_row=0):
103105

104106

105107
def get_last_entered_row(spreadsheet_id, month):
108+
month = month.title()
109+
106110
result = (
107111
sheets_api.spreadsheets()
108112
.values()
@@ -114,6 +118,8 @@ def get_last_entered_row(spreadsheet_id, month):
114118

115119

116120
def create_date(spreadsheet_id, day, month, first_row):
121+
month = month.title()
122+
117123
body = {"values": [[day]]}
118124
range_name = f"{month}!A{first_row}"
119125
sheets_api.spreadsheets().values().update(
@@ -129,7 +135,9 @@ def create_entry(spreadsheet_id, month, row_tracker, row_data):
129135
price = row_data[1].strip()
130136
remarks = row_data[2].strip()
131137
category = row_data[3].strip()
132-
payment = row_data[4].strip()
138+
payment = row_data[4].strip()
139+
month = month.title()
140+
133141

134142
data = [price, remarks, category, payment]
135143
sheet_column_start = "H"
@@ -172,6 +180,7 @@ def create_backlog_entry(spreadsheet_id, backlog_day, backlog_month, row_data):
172180
remarks = row_data[2].strip()
173181
category = row_data[3].strip()
174182
payment = row_data[4].strip()
183+
backlog_month = backlog_month.title()
175184

176185
day_first_entry_index = get_day_first_entry_index(
177186
spreadsheet_id, backlog_month, backlog_day
@@ -396,6 +405,7 @@ def get_quick_add_list(spreadsheet_id, entry_type):
396405

397406

398407
def get_day_transaction(spreadsheet_id, month, date):
408+
month = month.title()
399409
result = (
400410
sheets_api.spreadsheets()
401411
.values()
@@ -437,6 +447,8 @@ def get_day_transaction(spreadsheet_id, month, date):
437447

438448

439449
def get_first_row_to_move(spreadsheet_id, month, date):
450+
month = month.title()
451+
440452
result = (
441453
sheets_api.spreadsheets()
442454
.values()
@@ -457,6 +469,8 @@ def get_first_row_to_move(spreadsheet_id, month, date):
457469

458470

459471
def get_day_first_entry_index(spreadsheet_id, month, date):
472+
month = month.title()
473+
460474
result = (
461475
sheets_api.spreadsheets()
462476
.values()
@@ -487,6 +501,8 @@ def get_work_place(spreadsheet_id):
487501

488502

489503
def update_income(spreadsheet_id, month, row_data):
504+
month = month.title()
505+
490506
data_mo = row_data[:3]
491507
data_r = [row_data[-1]]
492508
body_mo = {"values": [data_mo]}
@@ -523,6 +539,8 @@ def update_income(spreadsheet_id, month, row_data):
523539

524540

525541
def get_overall(spreadsheet_id, month):
542+
month = month.title()
543+
526544
result = (
527545
sheets_api.spreadsheets()
528546
.values()

bot/telegram_bot.py

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
timezone = pytz.timezone("Asia/Singapore")
2323
MASTER_TELE_ID = os.environ.get("MASTER_TELE_ID")
2424

25-
2625
def get_category_text(sheet_id, entry_type):
2726
msg = ""
2827
markup_list = []
@@ -57,8 +56,8 @@ def start(update, context):
5756
update.message.reply_text(SETUP_TEXT, parse_mode=ParseMode.HTML)
5857
return CS.SET_UP
5958
except Exception as e:
60-
print(e)
61-
update.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
59+
60+
update.message.reply_text(ERROR_TEXT + "\nError:\n" + utils.sanitize_error_message(str(e)))
6261
return ConversationHandler.END
6362

6463

@@ -179,7 +178,7 @@ def config_handler(update, context) -> int:
179178
)
180179
return CS.CONFIG_SETUP
181180
except Exception as e:
182-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
181+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
183182
return ConversationHandler.END
184183

185184

@@ -198,8 +197,8 @@ def config_setup(update, context) -> int:
198197
)
199198
return CS.CONFIG_CATEGORY
200199
except Exception as e:
201-
print(e)
202-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
200+
201+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
203202
return ConversationHandler.END
204203
update.callback_query.edit_message_text(END_TEXT, reply_markup=None)
205204
return ConversationHandler.END
@@ -233,8 +232,8 @@ def config_category(update, context) -> int:
233232
)
234233
return CS.CONFIG_SUBCATEGORY
235234
except Exception as e:
236-
print(e)
237-
update.callback_query.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
235+
236+
update.callback_query.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
238237
return ConversationHandler.END
239238

240239

@@ -256,8 +255,8 @@ def config_subcategory(update, context) -> int:
256255
)
257256
return CS.CONFIG_PAYMENT
258257
except Exception as e:
259-
print(e)
260-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
258+
259+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
261260
return ConversationHandler.END
262261

263262

@@ -276,8 +275,8 @@ def config_payment(update, context) -> int:
276275
)
277276
return CS.CONFIG_SUBPAYMENT
278277
except Exception as e:
279-
print(e)
280-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
278+
279+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
281280
return ConversationHandler.END
282281

283282

@@ -302,8 +301,8 @@ def config_subpayment(update, context) -> int:
302301
)
303302
return ConversationHandler.END
304303
except Exception as e:
305-
print(e)
306-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
304+
305+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
307306
return ConversationHandler.END
308307

309308

@@ -362,8 +361,8 @@ def remarks(update: Update, context) -> int:
362361
)
363362
return CS.CATEGORY
364363
except Exception as e:
365-
print(e)
366-
update.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
364+
365+
update.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
367366
return ConversationHandler.END
368367

369368

@@ -407,8 +406,8 @@ def category(update, context) -> int:
407406
)
408407
return CS.PAYMENT
409408
except Exception as e:
410-
print(e)
411-
update.callback_query.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
409+
410+
update.callback_query.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
412411
return ConversationHandler.END
413412

414413

@@ -434,8 +433,8 @@ def subcategory(update, context) -> int:
434433
)
435434
return CS.PAYMENT
436435
except Exception as e:
437-
print(e)
438-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
436+
437+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
439438
return ConversationHandler.END
440439

441440

@@ -466,8 +465,8 @@ def payment(update, context) -> int:
466465
update.callback_query.message.reply_text("Transaction logged.")
467466
return ConversationHandler.END
468467
except Exception as e:
469-
print(e)
470-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
468+
469+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
471470
return ConversationHandler.END
472471

473472

@@ -494,8 +493,8 @@ def subpayment(update, context) -> int:
494493
return ConversationHandler.END
495494

496495
except Exception as e:
497-
print(e)
498-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
496+
497+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
499498
return ConversationHandler.END
500499

501500

@@ -607,8 +606,8 @@ def add_transport(update, context):
607606
context.user_data["sheet_id"], EntryType.TRANSPORT
608607
)
609608
except Exception as e:
610-
print(e)
611-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
609+
610+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
612611
return ConversationHandler.END
613612
if not setting_list or not setting_list[0]:
614613
update.message.reply_text(QUICK_SETUP_TRANSPORT)
@@ -644,8 +643,8 @@ def add_others(update, context):
644643
context.user_data["sheet_id"], EntryType.OTHERS
645644
)
646645
except Exception as e:
647-
print(e)
648-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
646+
647+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
649648
return ConversationHandler.END
650649
if not setting_list or not setting_list[0]:
651650
update.message.reply_text(QUICK_SETUP_OTHER)
@@ -693,10 +692,10 @@ def quick_add(update, context) -> int:
693692
update.message.reply_text("Transaction logged.")
694693
return ConversationHandler.END
695694
except Exception as e:
696-
update.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
695+
update.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
697696
return ConversationHandler.END
698697
except Exception as e:
699-
print(e)
698+
700699
update.message.reply_text("Please follow the format and try again.")
701700
return CS.QUICK_ADD
702701

@@ -713,8 +712,8 @@ def get_day_transaction(update, context):
713712
update.message.reply_text(GET_TRANSACTION_TEXT)
714713
return CS.HANDLE_GET_TRANSACTION
715714
except Exception as e:
716-
print(e)
717-
update.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
715+
716+
update.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
718717
return ConversationHandler.END
719718

720719

@@ -726,8 +725,8 @@ def get_overall(update, context):
726725
update.message.reply_text(GET_OVERALL_TEXT)
727726
return CS.HANDLE_GET_OVERALL
728727
except Exception as e:
729-
print(e)
730-
update.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
728+
729+
update.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
731730
return ConversationHandler.END
732731

733732

@@ -769,8 +768,8 @@ def handle_get_transaction(update, context):
769768
update.message.reply_text(GET_TRANSACTION_TEXT)
770769
return CS.HANDLE_GET_TRANSACTION
771770
except Exception as e:
772-
print(e)
773-
update.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
771+
772+
update.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
774773
return ConversationHandler.END
775774

776775

@@ -806,8 +805,8 @@ def handle_get_overall(update, context):
806805
update.message.reply_text(GET_OVERALL_TEXT)
807806
return CS.HANDLE_GET_OVERALL
808807
except Exception as e:
809-
print(e)
810-
update.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
808+
809+
update.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
811810
return ConversationHandler.END
812811

813812

@@ -818,8 +817,8 @@ def add_income(update, context):
818817
context.user_data["sheet_id"] = db.get_user_sheet_id(telegram_id)
819818
update.message.reply_text(ADD_INCOME_TEXT)
820819
except Exception as e:
821-
print(e)
822-
update.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
820+
821+
update.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
823822
return ConversationHandler.END
824823
return CS.INCOME
825824

@@ -847,8 +846,8 @@ def income(update, context) -> int:
847846
)
848847
return CS.WORK_PLACE
849848
except Exception as e:
850-
print(e)
851-
update.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
849+
850+
update.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
852851
return ConversationHandler.END
853852

854853

@@ -881,8 +880,8 @@ def cpf(update, context) -> int:
881880
update.callback_query.message.reply_text(INCOME_LIMIT_TEXT)
882881
return ConversationHandler.END
883882
except Exception as e:
884-
print(e)
885-
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" + str(e))
883+
884+
update.callback_query.message.reply_text(ERROR_TEXT + "\nError:\n" +utils.sanitize_error_message(str(e)))
886885
return ConversationHandler.END
887886

888887

@@ -896,7 +895,7 @@ def backlog(update, context) -> int:
896895
def add_backlog_entry(update, context) -> int:
897896
reply = update.message.text
898897
if utils.check_date_format(reply):
899-
if reply == dt.datetime.now(timezone).strftime("%d %b").lstrip("0"):
898+
if reply == dt.datetime.now(timezone).strftime("%d %b").lstrip("0").lower():
900899
context.user_data["backlog"] = False
901900
else:
902901
day, month = reply.split(" ")

bot/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ def check_date_format(date_string):
2525
def check_month_format(month_string):
2626
pattern = r"^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)$"
2727
return bool(re.fullmatch(pattern, month_string, re.IGNORECASE))
28+
29+
30+
def sanitize_error_message(error_message):
31+
# Regular expression to match URLs
32+
url_pattern = r'https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,}'
33+
34+
# Replace URLs with "<url here>"
35+
sanitized_message = re.sub(url_pattern, '<url here>', error_message)
36+
37+
return sanitized_message

release_notes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# Release Notes
2+
## Version 2.3.1 - Date 18 Mar 2024
3+
4+
### Changes 🛠️
5+
- Censor URL for error message
6+
- Month capitalize bug fix
7+
28
## Version 2.3.0 - Date 16 Mar 2024
39
### For Developer 🧑‍💻
410
- Create new database module which is currently in use for future maintenance and development purposes

0 commit comments

Comments
 (0)