15
15
from bot .text_str import *
16
16
from bot .common import EntryType
17
17
from bot .common import ConversationState as CS
18
- import bot .google_sheet as gs
19
- import bot .firestore as db
18
+ import bot .google_sheet_service as gs
19
+ import bot .firestore_service as db
20
+ import bot .utils as utils
20
21
21
22
timezone = pytz .timezone ("Asia/Singapore" )
22
23
23
-
24
- def create_inline_markup (list ):
25
- keyboard_markup_list = []
26
- for reply in list :
27
- if reply :
28
- keyboard_markup_list .append (
29
- [InlineKeyboardButton (reply , callback_data = reply )]
30
- )
31
- return InlineKeyboardMarkup (keyboard_markup_list )
32
-
33
-
34
- def is_valid_price (price ):
35
- pattern = r"^-?\d{0,10}(\.\d{0,2})?$"
36
- return bool (re .match (pattern , price ))
37
-
38
-
39
- def check_date_format (date_string ):
40
- pattern = r"\b\d{1,2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b"
41
- return bool (re .fullmatch (pattern , date_string , re .IGNORECASE ))
42
-
43
-
44
- def check_month_format (month_string ):
45
- pattern = r"^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)$"
46
- return bool (re .fullmatch (pattern , month_string , re .IGNORECASE ))
47
-
48
-
49
24
def get_category_text (sheet_id , entry_type ):
50
25
msg = ""
51
26
markup_list = []
@@ -73,7 +48,7 @@ def start(update, context):
73
48
link = f"https://docs.google.com/spreadsheets/d/{ context .user_data ['sheet_id' ]} /edit"
74
49
update .message .reply_text (
75
50
f"Seems like you have already linked a Google sheet with us, do you want to link a different Google sheet with us?\n \n { link } " ,
76
- reply_markup = create_inline_markup (["Yes" , "No" ]),
51
+ reply_markup = utils . create_inline_markup (["Yes" , "No" ]),
77
52
)
78
53
return CS .RESET_UP
79
54
else :
@@ -158,7 +133,7 @@ def config(update, context):
158
133
"Cancel" ,
159
134
]
160
135
update .message .reply_text (
161
- "How can i help you today?" , reply_markup = create_inline_markup (list )
136
+ "How can i help you today?" , reply_markup = utils . create_inline_markup (list )
162
137
)
163
138
return CS .CONFIG_HANDLER
164
139
@@ -197,7 +172,7 @@ def config_handler(update, context) -> int:
197
172
keyboard_list .append ("Add new" )
198
173
keyboard_list .append ("Cancel" )
199
174
update .callback_query .message .reply_text (
200
- msg , reply_markup = create_inline_markup (keyboard_list )
175
+ msg , reply_markup = utils . create_inline_markup (keyboard_list )
201
176
)
202
177
return CS .CONFIG_SETUP
203
178
except Exception as e :
@@ -216,7 +191,7 @@ def config_setup(update, context) -> int:
216
191
)
217
192
update .callback_query .message .edit_text (
218
193
f"Choose your default { config .value } type." ,
219
- reply_markup = create_inline_markup (markup_list ),
194
+ reply_markup = utils . create_inline_markup (markup_list ),
220
195
)
221
196
return CS .CONFIG_CATEGORY
222
197
except Exception as e :
@@ -240,7 +215,7 @@ def config_category(update, context) -> int:
240
215
)
241
216
payment_list = gs .get_main_dropdown_value (sheet_id , "Payment" )
242
217
update .callback_query .message .reply_text (
243
- DEFAULT_PAYMENT_TEXT , reply_markup = create_inline_markup (payment_list )
218
+ DEFAULT_PAYMENT_TEXT , reply_markup = utils . create_inline_markup (payment_list )
244
219
)
245
220
return CS .CONFIG_PAYMENT
246
221
elif config == EntryType .OTHERS :
@@ -249,7 +224,7 @@ def config_category(update, context) -> int:
249
224
sub_markup_list .pop (0 )
250
225
update .callback_query .message .edit_text (
251
226
DEFAULT_SUBCATEGORY_TEXT ,
252
- reply_markup = create_inline_markup (sub_markup_list ),
227
+ reply_markup = utils . create_inline_markup (sub_markup_list ),
253
228
)
254
229
return CS .CONFIG_SUBCATEGORY
255
230
except Exception as e :
@@ -271,7 +246,7 @@ def config_subcategory(update, context) -> int:
271
246
reply_markup = None ,
272
247
)
273
248
update .callback_query .message .reply_text (
274
- DEFAULT_PAYMENT_TEXT , reply_markup = create_inline_markup (payment_list )
249
+ DEFAULT_PAYMENT_TEXT , reply_markup = utils . create_inline_markup (payment_list )
275
250
)
276
251
return CS .CONFIG_PAYMENT
277
252
except Exception as e :
@@ -289,7 +264,7 @@ def config_payment(update, context) -> int:
289
264
if len (sub_markup_list ) > 1 :
290
265
sub_markup_list .pop (0 )
291
266
update .callback_query .message .edit_text (
292
- DEFAULT_PAYMENT_TEXT , reply_markup = create_inline_markup (sub_markup_list )
267
+ DEFAULT_PAYMENT_TEXT , reply_markup = utils . create_inline_markup (sub_markup_list )
293
268
)
294
269
return CS .CONFIG_SUBPAYMENT
295
270
except Exception as e :
@@ -328,7 +303,7 @@ def add_entry(update, context):
328
303
context .user_data ["sheet_id" ] = db .get_user_sheet_id (telegram_id )
329
304
update .message .reply_text (
330
305
ENTRY_TYPE_TEXT ,
331
- reply_markup = create_inline_markup (
306
+ reply_markup = utils . create_inline_markup (
332
307
[entry_type .value for entry_type in EntryType ]
333
308
),
334
309
)
@@ -346,7 +321,7 @@ def entry(update, context) -> int:
346
321
347
322
def price (update , context ) -> int :
348
323
reply = update .message .text
349
- if is_valid_price (reply ):
324
+ if utils . is_valid_price (reply ):
350
325
context .user_data ["price" ] = reply
351
326
entry_type = context .user_data ["entry_type" ]
352
327
if entry_type == EntryType .TRANSPORT :
@@ -372,7 +347,7 @@ def remarks(update: Update, context) -> int:
372
347
return CS .REMARKS
373
348
msg , markup_list = get_category_text (sheet_id , entry_type )
374
349
try :
375
- update .message .reply_text (msg , reply_markup = create_inline_markup (markup_list ))
350
+ update .message .reply_text (msg , reply_markup = utils . create_inline_markup (markup_list ))
376
351
return CS .CATEGORY
377
352
except Exception as e :
378
353
update .message .reply_text (ERROR_TEXT )
@@ -392,7 +367,7 @@ def category(update, context) -> int:
392
367
context .user_data ["category" ] = f"{ reply } "
393
368
payment_list = get_payment_text (sheet_id )
394
369
update .callback_query .message .reply_text (
395
- DEFAULT_PAYMENT_TEXT , reply_markup = create_inline_markup (payment_list )
370
+ DEFAULT_PAYMENT_TEXT , reply_markup = utils . create_inline_markup (payment_list )
396
371
)
397
372
return CS .PAYMENT
398
373
elif entry_type == EntryType .OTHERS :
@@ -403,7 +378,7 @@ def category(update, context) -> int:
403
378
sub_markup_list .append (BACK_TEXT )
404
379
update .callback_query .message .edit_text (
405
380
DEFAULT_SUBCATEGORY_TEXT ,
406
- reply_markup = create_inline_markup (sub_markup_list ),
381
+ reply_markup = utils . create_inline_markup (sub_markup_list ),
407
382
)
408
383
return CS .SUBCATEGORY
409
384
# This won't be called as there will always be a subcategory, but just in case
@@ -414,7 +389,7 @@ def category(update, context) -> int:
414
389
payment_list = get_payment_text (sheet_id )
415
390
update .callback_query .message .reply_text (
416
391
DEFAULT_PAYMENT_TEXT ,
417
- reply_markup = create_inline_markup (payment_list ),
392
+ reply_markup = utils . create_inline_markup (payment_list ),
418
393
)
419
394
return CS .PAYMENT
420
395
except Exception as e :
@@ -430,7 +405,7 @@ def subcategory(update, context) -> int:
430
405
if reply == BACK_TEXT :
431
406
msg , markup_list = get_category_text (sheet_id , entry_type )
432
407
update .callback_query .edit_message_text (
433
- msg , reply_markup = create_inline_markup (markup_list )
408
+ msg , reply_markup = utils . create_inline_markup (markup_list )
434
409
)
435
410
return CS .CATEGORY
436
411
try :
@@ -440,7 +415,7 @@ def subcategory(update, context) -> int:
440
415
)
441
416
payment_list = get_payment_text (sheet_id )
442
417
update .callback_query .message .reply_text (
443
- DEFAULT_PAYMENT_TEXT , reply_markup = create_inline_markup (payment_list )
418
+ DEFAULT_PAYMENT_TEXT , reply_markup = utils . create_inline_markup (payment_list )
444
419
)
445
420
return CS .PAYMENT
446
421
except Exception as e :
@@ -459,7 +434,7 @@ def payment(update, context) -> int:
459
434
sub_markup_list .pop (0 )
460
435
sub_markup_list .append (BACK_TEXT )
461
436
update .callback_query .message .edit_text (
462
- DEFAULT_PAYMENT_TEXT , reply_markup = create_inline_markup (sub_markup_list )
437
+ DEFAULT_PAYMENT_TEXT , reply_markup = utils . create_inline_markup (sub_markup_list )
463
438
)
464
439
return CS .SUBPAYMENT
465
440
# This won't be called as there will always be a subpayment, but just in case
@@ -482,7 +457,7 @@ def subpayment(update, context) -> int:
482
457
if reply == BACK_TEXT :
483
458
payment_list = get_payment_text (sheet_id )
484
459
update .callback_query .edit_message_text (
485
- DEFAULT_PAYMENT_TEXT , reply_markup = create_inline_markup (payment_list )
460
+ DEFAULT_PAYMENT_TEXT , reply_markup = utils . create_inline_markup (payment_list )
486
461
)
487
462
return CS .PAYMENT
488
463
try :
@@ -601,7 +576,7 @@ def add_transport(update, context):
601
576
else :
602
577
update .message .reply_text (
603
578
"Quick Add Transport, please choose your category." ,
604
- reply_markup = create_inline_markup (setting_list ),
579
+ reply_markup = utils . create_inline_markup (setting_list ),
605
580
)
606
581
return CS .QUICK_ADD_TRANSPORT
607
582
@@ -627,7 +602,7 @@ def add_others(update, context):
627
602
)
628
603
update .message .reply_text (
629
604
"Quick Add Others, please choose your category." ,
630
- reply_markup = create_inline_markup (setting_list ),
605
+ reply_markup = utils . create_inline_markup (setting_list ),
631
606
)
632
607
return CS .QUICK_ADD_CATEGORY
633
608
@@ -706,7 +681,7 @@ def handle_get_transaction(update, context):
706
681
try :
707
682
if reply .lower () == "tdy" :
708
683
reply = dt .datetime .now (timezone ).strftime ("%d %b" ).lstrip ("0" )
709
- if check_date_format (reply ):
684
+ if utils . check_date_format (reply ):
710
685
day , month = reply .split (" " )
711
686
total_spend , transport_values , other_values = gs .get_day_transaction (
712
687
sheet_id , month , day
@@ -735,7 +710,7 @@ def handle_get_overall(update, context):
735
710
sheet_id = context .user_data ["sheet_id" ]
736
711
month = update .message .text
737
712
try :
738
- if check_month_format (month ):
713
+ if utils . check_month_format (month ):
739
714
values = gs .get_overall (sheet_id , month )
740
715
741
716
final_income = values [0 ]
@@ -786,7 +761,7 @@ def income(update, context) -> int:
786
761
if "," in reply :
787
762
income , remarks = reply .split ("," )
788
763
else :
789
- if is_valid_price (income ):
764
+ if utils . is_valid_price (income ):
790
765
income = reply
791
766
else :
792
767
update .message .reply_text (ADD_INCOME_RETRY_TEXT )
@@ -797,7 +772,7 @@ def income(update, context) -> int:
797
772
sheet_id = context .user_data ["sheet_id" ]
798
773
work_list = gs .get_work_place (sheet_id )
799
774
update .message .reply_text (
800
- CHOOSE_INCOME_SOURCE_TEXT , reply_markup = create_inline_markup (work_list )
775
+ CHOOSE_INCOME_SOURCE_TEXT , reply_markup = utils . create_inline_markup (work_list )
801
776
)
802
777
return CS .WORK_PLACE
803
778
except Exception as e :
@@ -811,7 +786,7 @@ def work_place(update, context) -> int:
811
786
update .callback_query .answer ()
812
787
update .callback_query .message .edit_text (f"Place: { place } " , reply_markup = None )
813
788
update .callback_query .message .reply_text (
814
- CPF_TEXT , reply_markup = create_inline_markup (["Yes" , "No" ])
789
+ CPF_TEXT , reply_markup = utils . create_inline_markup (["Yes" , "No" ])
815
790
)
816
791
return CS .CPF
817
792
0 commit comments