From 0cf49344682b223212ebc56c417c1746ab802590 Mon Sep 17 00:00:00 2001 From: Ahmed Bahajjaj <42177597+madanalogy@users.noreply.github.com> Date: Thu, 16 Nov 2023 01:05:59 +0800 Subject: [PATCH] update constants --- actions.py | 17 ++++++++++++----- constants.py | 47 +++++++++++++++++++++++++++++++---------------- main.py | 10 ++++++---- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/actions.py b/actions.py index b3702fd..f8de07b 100644 --- a/actions.py +++ b/actions.py @@ -22,10 +22,11 @@ def run_add(chat_id, text): if not is_valid_amount(amount): return constants.ERROR_PRECONDITION amount = float(amount) + payer = core[2].strip().lower(), details = { "name": core[0].strip().lower(), "amount": amount, - "payer": core[2].strip().lower(), + "payer": payer, "timestamp": firestore.SERVER_TIMESTAMP } @@ -36,22 +37,28 @@ def run_add(chat_id, text): parsed = line.split(",") if not parsed or len(parsed) > 2: return constants.ERROR_ADD_FORMAT + debtor = parsed[0].strip().lower() if len(parsed) == 2: owed = parsed[1].strip() if not is_valid_amount(owed): return constants.ERROR_PRECONDITION owed = float(owed) - owed_amounts[parsed[0].strip()] = owed + owed_amounts[debtor] = owed running_sum += owed else: - owed_amounts[parsed[0].strip()] = 0 + if debtor == payer: + return constants.ERROR_PRECONDITION + owed_amounts[debtor] = 0 split_count += 1 if running_sum > amount: return constants.ERROR_SUM_MISMATCH if running_sum == amount and split_count != 0: return constants.ERROR_SUM_MISMATCH if split_count != 0: - debt_each = (amount - running_sum) / (split_count + 1) + if payer in owed_amounts: + debt_each = (amount - running_sum) / (split_count) + else: + debt_each = (amount - running_sum) / (split_count + 1) for debtor in owed_amounts: if owed_amounts[debtor] == 0: owed_amounts[debtor] = debt_each @@ -60,7 +67,7 @@ def run_add(chat_id, text): update_time, trans_ref = transactions.add(details) debt_ref = trans_ref.collection("debtors") for debtor in owed_amounts: - debt_ref.add({"name": debtor.lower(), "amount": owed_amounts[debtor]}) + debt_ref.add({"name": debtor, "amount": owed_amounts[debtor]}) return "Added successfully! Use /list if you want to see all pending transactions" diff --git a/constants.py b/constants.py index 3ef7418..575ccfd 100644 --- a/constants.py +++ b/constants.py @@ -2,17 +2,32 @@ /add Label, Amount, Payer_Name Debtor_Name, [Optional_Amount] -Debtor_Name, [Optional_Amount]''' +Debtor_Name, [Optional_Amount] + +Use the command /examples to show examples''' EXAMPLES = '''== Examples == -/add Dinner, 456, John -Andy, 123 +John paid for dinner, everyone pays exactly what they ordered: +/add Dinner, 242, John +Andy, 62 Bob, 78 +Bob paid for drinks, evenly split amongst all parties: /add Drinks, 654, Bob Andy John + +Andy paid for breakfast, and boy was he not happy about that: +/add Breakfast, 120, Andy +Andy, 10 +Mary +Bob +John + +Bob was gonna cab back with Mary but John asked if they could add a stop: +/add Cab Ride, 42.50, Bob +John, 7.50 Mary''' COMMANDS = '''- /add to add a transaction to the list. @@ -22,29 +37,31 @@ - /settle to settle up all pending transactions. This will remove all transactions. - /help to bring up the available instructions and format.''' -ASSUMPTIONS = '''- Amounts support up to 2 decimal digits. Any rounding difference in division will go to the payer. -- Each transaction requires at least 1 debtor. Otherwise what's the point honestly.''' +ASSUMPTIONS = '''Tips: +- Make sure the spelling of each name is consistent across transactions. The name is not case sensitive. +- Amounts support up to 2 decimal digits. There might be a small rounding difference in division. +- If the payer is also a debtor in the same transaction, there must be an amount indicated. +- Each transaction requires at least 1 debtor.''' EXPLAINER = '''How it works: - If a debtor's amount is specified in a transaction, that share will first be deducted from the amount. -- The remaining amount will be split amongst all debtors that do not have an amount specified. -- The bot will then calculate all relationships between transactions to come up with a final tally. +- The remaining amount will be split evenly amongst the payer and all debtors that do not have an amount specified. +- The payer will not be included in the even split only if the payer is also a debtor with an amount specified. +- The /settle command will then calculate all relationships between transactions to come up with a final tally. -Tips: -- Make sure the spelling of each name is consistent across transactions. The name is not case sensitive. {ASSUMPTIONS}'''.format(ASSUMPTIONS=ASSUMPTIONS) INSTRUCTIONS = '''{COMMANDS} {TRANSACTION_FORMAT} -{EXAMPLES} +{EXPLAINER}'''.format(COMMANDS=COMMANDS, TRANSACTION_FORMAT=TRANSACTION_FORMAT, EXPLAINER=EXPLAINER) -{EXPLAINER}'''.format(COMMANDS=COMMANDS, TRANSACTION_FORMAT=TRANSACTION_FORMAT, EXAMPLES=EXAMPLES, EXPLAINER=EXPLAINER) +INTRO = '''Hey there, here's how you can use me :) -INTRO = '''Hey there, here's how you can use me ;) +{INSTRUCTIONS} -{INSTRUCTIONS}'''.format(INSTRUCTIONS=INSTRUCTIONS) +Feel free to text me directly for more privacy or add me to a group for more transparency!'''.format(INSTRUCTIONS=INSTRUCTIONS) ERROR_GENERIC = '''Hey sorry I didn't quite get that. Please see the command list below: @@ -52,9 +69,7 @@ ERROR_ADD_FORMAT = '''Think you got the format wrong for that one. Please see the format below: -{TRANSACTION_FORMAT} - -{EXAMPLES}'''.format(TRANSACTION_FORMAT=TRANSACTION_FORMAT, EXAMPLES=EXAMPLES) +{TRANSACTION_FORMAT}'''.format(TRANSACTION_FORMAT=TRANSACTION_FORMAT) ERROR_PRECONDITION = '''You're missing out on one of the requirements below: diff --git a/main.py b/main.py index 5bbd1c1..879e028 100644 --- a/main.py +++ b/main.py @@ -40,15 +40,17 @@ async def process(request): return chat_id = update.message.chat.id response = get_response(chat_id, update.message.text) + if response == "": + return await bot.sendMessage(chat_id=chat_id, text=response) def get_response(chat_id, text): print(f"Received message: {text}") if not text: - return + return "" clean = text.replace("@madsplit_bot", "") - if clean.startswith("/start"): + if clean.startswith("/start") or clean.startswith("/help"): return constants.INTRO if clean.startswith("/add"): return actions.run_add(chat_id, clean[len("/add"):]) @@ -60,8 +62,8 @@ def get_response(chat_id, text): return actions.run_delete(chat_id, clean[len("/delete"):]) if clean.startswith("/settle"): return actions.run_settle(chat_id) - if clean.startswith("/help"): - return constants.INTRO + if clean.startswith("/examples"): + return constants.EXAMPLES return constants.ERROR_GENERIC