aboutsummaryrefslogtreecommitdiff
path: root/geldschieberbot.py
diff options
context:
space:
mode:
Diffstat (limited to 'geldschieberbot.py')
-rw-r--r--geldschieberbot.py47
1 files changed, 36 insertions, 11 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py
index 969540f..ccfe96f 100644
--- a/geldschieberbot.py
+++ b/geldschieberbot.py
@@ -1,7 +1,9 @@
#!/usr/bin/env python3
"""Bot to manage a groups finances"""
+import argparse
from datetime import date, datetime, timedelta
+from dataclasses import dataclass
import json
import os
import subprocess
@@ -16,6 +18,13 @@ SEND_CMD = os.environ["GSB_SEND_CMD"]
GROUP_SEND_CMD = SEND_CMD + GROUP_ID
+@dataclass
+class Quote:
+ """Class representing a message to quote"""
+ timestamp: str
+ author: str
+
+
def to_cent(euro):
"""Parse string containing euros into a cent value"""
if '.' in euro:
@@ -93,11 +102,13 @@ class Geldschieberbot:
self.balance[donor][recipient] += amount
self.balance[recipient][donor] -= amount
- def send(self, msg, attachment=None, cmd=SEND_CMD):
+ def send(self, msg, attachment=None, cmd=SEND_CMD, quote: Quote = None):
"""Send a message with optional attachment"""
if not self.quiet:
if attachment:
cmd += f' -a {attachment}'
+ if quote:
+ cmd += f' --quote-timestamp={quote.timestamp} --quote-author={quote.author}'
subprocess.run(cmd.split(' '), input=msg.encode(), check=False)
def create_summary(self, user) -> str:
@@ -937,8 +948,9 @@ class Geldschieberbot:
self.aliases[alias] = users
return {'msg': f'New alias "{alias}" registered'}
- def __init__(self, dry_run=False):
+ def __init__(self, dry_run=False, quote_cmd=True):
self.dry_run = dry_run # Run without changing the stored state
+ self.quote_cmd = quote_cmd # Quote the message causing the reply
self.load_state()
self.quiet = False # Run without sending messages
self.record_changes = True # Should changes be recorded
@@ -1005,14 +1017,14 @@ class Geldschieberbot:
if self.record_changes and not self.dry_run:
self.changes[user].append(change)
- def handle(self, message: dict):
+ def handle(self, envelope: dict):
"""Parse and respond to a message"""
- sender_number = message["source"]
- if not "dataMessage" in message or not message[
- "dataMessage"] or not message["dataMessage"]["message"]:
+ sender_number = envelope["source"]
+ if not "dataMessage" in envelope or not envelope[
+ "dataMessage"] or not envelope["dataMessage"]["message"]:
return
- message = message["dataMessage"]
+ message = envelope["dataMessage"]
if message["groupInfo"] and message["groupInfo"]["groupId"] != GROUP_ID:
return
@@ -1021,6 +1033,7 @@ class Geldschieberbot:
if len(body) == 0 or not body[0].startswith('!'):
return
+ quote = Quote(timestamp=message["timestamp"], author=sender_number)
args = body[0].split(' ')
cmd = args[0][1:]
if cmd in self.cmds:
@@ -1030,7 +1043,9 @@ class Geldschieberbot:
else:
if not 'msg' in ret:
print(ret)
- self.send(ret['msg'], attachment=ret.get('attachment', None))
+ self.send(ret['msg'],
+ attachment=ret.get('attachment', None),
+ quote=quote if self.quote_cmd else None)
else:
self.send(
'ERROR: unknown cmd. Enter !help for a list of commands.')
@@ -1086,11 +1101,21 @@ class Geldschieberbot:
def main():
- dry_run = len(sys.argv) > 1 and sys.argv[1] in ["-d", "--dry-run"]
- if dry_run:
+ parser = argparse.ArgumentParser()
+ parser.add_argument('-d',
+ '--dry-run',
+ help='do not persist changes',
+ action='store_true')
+ parser.add_argument('-nq',
+ '--no-quote',
+ help='not quote the message causing the reply',
+ action='store_true')
+ args = parser.parse_args()
+
+ if args.dry_run:
print("Dry Run no changes will apply!")
- bot = Geldschieberbot(dry_run=dry_run)
+ bot = Geldschieberbot(dry_run=args.dry_run, quote_cmd=not args.no_quote)
# Read cmds from stdin
for l in sys.stdin.read().splitlines():