diff options
| author | Florian Fischer <florian.fl.fischer@fau.de> | 2017-08-11 18:08:13 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fl.fischer@fau.de> | 2017-08-11 18:08:13 +0200 |
| commit | b8321fbccec3854968b2b9bf7b3d39a45cf34df8 (patch) | |
| tree | 8d17ccd1b9b105a1e63961201ed1230e23127254 | |
| parent | 2742ba91678963cef2f8f82d4142d4c6d5ab8958 (diff) | |
| download | geldschieberbot-b8321fbccec3854968b2b9bf7b3d39a45cf34df8.tar.gz geldschieberbot-b8321fbccec3854968b2b9bf7b3d39a45cf34df8.zip | |
add first prototype
| -rw-r--r-- | geldschieberbot.py | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py new file mode 100644 index 0000000..dbf6644 --- /dev/null +++ b/geldschieberbot.py @@ -0,0 +1,181 @@ +#!/usr/bin/env python3 + +import json +import os +import subprocess +import sys + +"""Dict of dicts associating a second person to an amount""" +balance = {} +store_path = "." + +name2num = {} +num2name = {} + +registration_path = "reg.json" + +group_id = "GELD!!!" + +send_cmd = "signal-cli -u irgendwer send -g irgendwem" + +def send(msg): + subprocess.run(send_cmd.split(' '), input=msg.encode()) + +def create_summary(): + summary = "" + + for person in balance: + summary += person + ":\n" + + p_balance = balance[person] + for person2 in p_balance: + amount = p_balance[person2] + summary += "\t" + ("<-" if amount < 0 else "->") + summary += " " + person2 + " " + str(abs(amount)) + "\n" + summary += "\n" + + return summary + +def create_members(): + r = "" + for m in name2num: + r += m + ": " + name2num[m] + "\n" + return r + +def create_help(): + h = "" + return h + +def handle_input(inp): + messages = inp.split("\n\n") + for message in messages: + lines = message.split('\n') + + # A message with Body must have at least 4 lines + if len(lines) < 4: + continue + + w = lines[0].split(' ') + + # TODO support linked devices + sender_number = w[-3] + + body_found = False + body = "" + + # message is not in the group with group_id + not_in_group = True + + for idx, line in enumerate(lines[1:]): + if line[0:6] == "Body: ": + body = line[6:] + body_found = True + # Only one line bodies are supported + elif body_found: + not_in_group = lines[idx+1].strip() == "Id: " + group_id + break + + if not_in_group: + continue + + # strip of '\n' + w = body.split(' ') + + # supported commands are: + # "!reg" register a name for this number + # "!sum" send a summary to the group + # "!schieb" give money to somebody + # "!zieh" add debt of somebody + # "!list" "!ls" list members + # "!help" print all commands + if w[0] == "!reg": + if len(w) != 2: + send('ERROR: not in form "!reg name"') + else: + print(w[1]) + if w[1] in name2num: + send("ERROR: name already registered") + elif sender_number in num2name: + send("ERROR: you are already registered") + else: + num2name[sender_number] = w[1] + name2num[w[1]] = sender_number + + # add to balance + nm = {} + for m in balance: + balance[m][w[1]] = 0 + nm[m] = 0 + balance[w[1]] = nm + + elif w[0] == "!sum": + if len(w) > 2: + send('ERROR: not in form "!sum [name]"') + else: + send(create_summary()) + + elif w[0] == "!schieb" or "!zieh": + if len(w) != 3: + send('ERROR: not in form "!schieb amount recipient"') + else: + if not sender_number in num2name: + send('ERROR: you must register first') + elif not w[2] in name2num: + send('ERROR: recipient not known') + else: + sender = num2name[sender_number] + try: + amount = int(w[1]) + except: + send("ERROR: amount musst be a number") + continue + + if w[0] == "!zieh": + amount *= -1 + + balance[sender][w[2]] -= amount + balance[w[2]][sender] += amount + + p_balance = balance[sender][w[2]] + + send("New Balance: {0} {1} {2} {3}\n".format(sender, ("->" if p_balance > 0 else "<-"), abs(p_balance), w[2])) + + elif w[0] == "!list" or w[0] == "!ls": + send(create_members()) + + elif w[0] == "!help": + send(create_help()) + +def main(): + global group_id + group_id = os.environ["GSB_GROUP_ID"] + + global balance + store_path = os.environ["GSB_STORE_PATH"] + balance_path = store_path + "/balance.json" + registration_path = store_path + "/registration.json" + + global name2num + global num2name + + global send_cmd + send_cmd = os.environ["GSB_SEND_CMD"] + + if os.path.isfile(balance_path): + balance = json.load(open(balance_path, "r")) + + if os.path.isfile(registration_path): + name2num = json.load(open(registration_path, "r")) + for name in name2num: + num2name[name2num[name]] = name + + handle_input(sys.stdin.read()) + + with open(balance_path, "w") as f: + json.dump(balance, f) + + with open(registration_path, "w") as f: + json.dump(name2num, f) + +if __name__ == "__main__": + main() |
