Skip to content

Commit d5bb05f

Browse files
authored
mini refactor (#27)
1 parent 7a4b6bc commit d5bb05f

File tree

6 files changed

+57
-61
lines changed

6 files changed

+57
-61
lines changed

bot/bot_controller.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.

bot/telegram_bot.py

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,12 @@
1515
from bot.text_str import *
1616
from bot.common import EntryType
1717
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
2021

2122
timezone = pytz.timezone("Asia/Singapore")
2223

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-
4924
def get_category_text(sheet_id, entry_type):
5025
msg = ""
5126
markup_list = []
@@ -73,7 +48,7 @@ def start(update, context):
7348
link = f"https://docs.google.com/spreadsheets/d/{context.user_data['sheet_id']}/edit"
7449
update.message.reply_text(
7550
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"]),
7752
)
7853
return CS.RESET_UP
7954
else:
@@ -158,7 +133,7 @@ def config(update, context):
158133
"Cancel",
159134
]
160135
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)
162137
)
163138
return CS.CONFIG_HANDLER
164139

@@ -197,7 +172,7 @@ def config_handler(update, context) -> int:
197172
keyboard_list.append("Add new")
198173
keyboard_list.append("Cancel")
199174
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)
201176
)
202177
return CS.CONFIG_SETUP
203178
except Exception as e:
@@ -216,7 +191,7 @@ def config_setup(update, context) -> int:
216191
)
217192
update.callback_query.message.edit_text(
218193
f"Choose your default {config.value} type.",
219-
reply_markup=create_inline_markup(markup_list),
194+
reply_markup=utils.create_inline_markup(markup_list),
220195
)
221196
return CS.CONFIG_CATEGORY
222197
except Exception as e:
@@ -240,7 +215,7 @@ def config_category(update, context) -> int:
240215
)
241216
payment_list = gs.get_main_dropdown_value(sheet_id, "Payment")
242217
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)
244219
)
245220
return CS.CONFIG_PAYMENT
246221
elif config == EntryType.OTHERS:
@@ -249,7 +224,7 @@ def config_category(update, context) -> int:
249224
sub_markup_list.pop(0)
250225
update.callback_query.message.edit_text(
251226
DEFAULT_SUBCATEGORY_TEXT,
252-
reply_markup=create_inline_markup(sub_markup_list),
227+
reply_markup=utils.create_inline_markup(sub_markup_list),
253228
)
254229
return CS.CONFIG_SUBCATEGORY
255230
except Exception as e:
@@ -271,7 +246,7 @@ def config_subcategory(update, context) -> int:
271246
reply_markup=None,
272247
)
273248
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)
275250
)
276251
return CS.CONFIG_PAYMENT
277252
except Exception as e:
@@ -289,7 +264,7 @@ def config_payment(update, context) -> int:
289264
if len(sub_markup_list) > 1:
290265
sub_markup_list.pop(0)
291266
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)
293268
)
294269
return CS.CONFIG_SUBPAYMENT
295270
except Exception as e:
@@ -328,7 +303,7 @@ def add_entry(update, context):
328303
context.user_data["sheet_id"] = db.get_user_sheet_id(telegram_id)
329304
update.message.reply_text(
330305
ENTRY_TYPE_TEXT,
331-
reply_markup=create_inline_markup(
306+
reply_markup=utils.create_inline_markup(
332307
[entry_type.value for entry_type in EntryType]
333308
),
334309
)
@@ -346,7 +321,7 @@ def entry(update, context) -> int:
346321

347322
def price(update, context) -> int:
348323
reply = update.message.text
349-
if is_valid_price(reply):
324+
if utils.is_valid_price(reply):
350325
context.user_data["price"] = reply
351326
entry_type = context.user_data["entry_type"]
352327
if entry_type == EntryType.TRANSPORT:
@@ -372,7 +347,7 @@ def remarks(update: Update, context) -> int:
372347
return CS.REMARKS
373348
msg, markup_list = get_category_text(sheet_id, entry_type)
374349
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))
376351
return CS.CATEGORY
377352
except Exception as e:
378353
update.message.reply_text(ERROR_TEXT)
@@ -392,7 +367,7 @@ def category(update, context) -> int:
392367
context.user_data["category"] = f"{reply}"
393368
payment_list = get_payment_text(sheet_id)
394369
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)
396371
)
397372
return CS.PAYMENT
398373
elif entry_type == EntryType.OTHERS:
@@ -403,7 +378,7 @@ def category(update, context) -> int:
403378
sub_markup_list.append(BACK_TEXT)
404379
update.callback_query.message.edit_text(
405380
DEFAULT_SUBCATEGORY_TEXT,
406-
reply_markup=create_inline_markup(sub_markup_list),
381+
reply_markup=utils.create_inline_markup(sub_markup_list),
407382
)
408383
return CS.SUBCATEGORY
409384
# 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:
414389
payment_list = get_payment_text(sheet_id)
415390
update.callback_query.message.reply_text(
416391
DEFAULT_PAYMENT_TEXT,
417-
reply_markup=create_inline_markup(payment_list),
392+
reply_markup=utils.create_inline_markup(payment_list),
418393
)
419394
return CS.PAYMENT
420395
except Exception as e:
@@ -430,7 +405,7 @@ def subcategory(update, context) -> int:
430405
if reply == BACK_TEXT:
431406
msg, markup_list = get_category_text(sheet_id, entry_type)
432407
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)
434409
)
435410
return CS.CATEGORY
436411
try:
@@ -440,7 +415,7 @@ def subcategory(update, context) -> int:
440415
)
441416
payment_list = get_payment_text(sheet_id)
442417
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)
444419
)
445420
return CS.PAYMENT
446421
except Exception as e:
@@ -459,7 +434,7 @@ def payment(update, context) -> int:
459434
sub_markup_list.pop(0)
460435
sub_markup_list.append(BACK_TEXT)
461436
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)
463438
)
464439
return CS.SUBPAYMENT
465440
# 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:
482457
if reply == BACK_TEXT:
483458
payment_list = get_payment_text(sheet_id)
484459
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)
486461
)
487462
return CS.PAYMENT
488463
try:
@@ -601,7 +576,7 @@ def add_transport(update, context):
601576
else:
602577
update.message.reply_text(
603578
"Quick Add Transport, please choose your category.",
604-
reply_markup=create_inline_markup(setting_list),
579+
reply_markup=utils.create_inline_markup(setting_list),
605580
)
606581
return CS.QUICK_ADD_TRANSPORT
607582

@@ -627,7 +602,7 @@ def add_others(update, context):
627602
)
628603
update.message.reply_text(
629604
"Quick Add Others, please choose your category.",
630-
reply_markup=create_inline_markup(setting_list),
605+
reply_markup=utils.create_inline_markup(setting_list),
631606
)
632607
return CS.QUICK_ADD_CATEGORY
633608

@@ -706,7 +681,7 @@ def handle_get_transaction(update, context):
706681
try:
707682
if reply.lower() == "tdy":
708683
reply = dt.datetime.now(timezone).strftime("%d %b").lstrip("0")
709-
if check_date_format(reply):
684+
if utils.check_date_format(reply):
710685
day, month = reply.split(" ")
711686
total_spend, transport_values, other_values = gs.get_day_transaction(
712687
sheet_id, month, day
@@ -735,7 +710,7 @@ def handle_get_overall(update, context):
735710
sheet_id = context.user_data["sheet_id"]
736711
month = update.message.text
737712
try:
738-
if check_month_format(month):
713+
if utils.check_month_format(month):
739714
values = gs.get_overall(sheet_id, month)
740715

741716
final_income = values[0]
@@ -786,7 +761,7 @@ def income(update, context) -> int:
786761
if "," in reply:
787762
income, remarks = reply.split(",")
788763
else:
789-
if is_valid_price(income):
764+
if utils.is_valid_price(income):
790765
income = reply
791766
else:
792767
update.message.reply_text(ADD_INCOME_RETRY_TEXT)
@@ -797,7 +772,7 @@ def income(update, context) -> int:
797772
sheet_id = context.user_data["sheet_id"]
798773
work_list = gs.get_work_place(sheet_id)
799774
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)
801776
)
802777
return CS.WORK_PLACE
803778
except Exception as e:
@@ -811,7 +786,7 @@ def work_place(update, context) -> int:
811786
update.callback_query.answer()
812787
update.callback_query.message.edit_text(f"Place: {place}", reply_markup=None)
813788
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"])
815790
)
816791
return CS.CPF
817792

bot/updates.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
from telegram import Bot
22
import os
3-
import bot.firebase as fb
3+
import bot.firestore_service as fs
44

55
TOKEN = os.environ.get("BOT_TOKEN")
66
bot = Bot(token=TOKEN)
77

88

99
def send_new_feature_message(new_feature_message):
10-
users = fb.get_all_user_id()
10+
users = fs.get_all_user_id()
1111
for user_id in users:
1212
bot.send_message(chat_id=user_id, text=new_feature_message)
1313
print("Message has been pushed to all users")
14-
15-
16-
def update_gsheet():
17-
sheet_ids = fb.get_all_sheet_id()
18-
for sheet_id in sheet_ids:
19-
pass # gs.update_gsheet(sheet_id)

bot/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ParseMode
3+
import re
4+
5+
def create_inline_markup(list):
6+
keyboard_markup_list = []
7+
for reply in list:
8+
if reply:
9+
keyboard_markup_list.append(
10+
[InlineKeyboardButton(reply, callback_data=reply)]
11+
)
12+
return InlineKeyboardMarkup(keyboard_markup_list)
13+
14+
15+
def is_valid_price(price):
16+
pattern = r"^-?\d{0,10}(\.\d{0,2})?$"
17+
return bool(re.match(pattern, price))
18+
19+
20+
def check_date_format(date_string):
21+
pattern = r"\b\d{1,2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b"
22+
return bool(re.fullmatch(pattern, date_string, re.IGNORECASE))
23+
24+
25+
def check_month_format(month_string):
26+
pattern = r"^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)$"
27+
return bool(re.fullmatch(pattern, month_string, re.IGNORECASE))

0 commit comments

Comments
 (0)