Skip to content

Commit

Permalink
update constants
Browse files Browse the repository at this point in the history
  • Loading branch information
madanalogy committed Nov 15, 2023
1 parent 30ae42e commit 0cf4934
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
17 changes: 12 additions & 5 deletions actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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"

Expand Down
47 changes: 31 additions & 16 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -22,39 +37,39 @@
- /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:
{COMMANDS}'''.format(COMMANDS=COMMANDS)

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:
Expand Down
10 changes: 6 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):])
Expand All @@ -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


Expand Down

0 comments on commit 0cf4934

Please sign in to comment.