aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-10-31 19:27:26 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-11-06 10:35:13 +0100
commit7d8f44cff2b53833a5c973c2ef36b8a34db2bd8d (patch)
tree0b4824553b990e467c11a5e5922b31c6636e68a1
parent8983a77010ee97f3f402098175afafdad5fee5e5 (diff)
downloadgeldschieberbot-7d8f44cff2b53833a5c973c2ef36b8a34db2bd8d.tar.gz
geldschieberbot-7d8f44cff2b53833a5c973c2ef36b8a34db2bd8d.zip
support dbus loop frontend for geldschieberbot
-rwxr-xr-xmain.py (renamed from single_shot.py)91
-rwxr-xr-xtest.py2
2 files changed, 75 insertions, 18 deletions
diff --git a/single_shot.py b/main.py
index 0c85947..c7a78d0 100755
--- a/single_shot.py
+++ b/main.py
@@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License along with
# geldschieberbot found in the LICENSE file. If not,
# see <https://www.gnu.org/licenses/>.
-"""Run geldschieberbot on all received messages, send replies, and exit"""
+"""Run geldschieberbot on all received messages and send replies"""
import argparse
from datetime import datetime
@@ -48,6 +48,72 @@ def die(msg: str, status=1):
sys.exit(status)
+def single_shot(bot: Geldschieberbot, send_cmd: str, no_quote: bool):
+ """Handle all messages read from stdin and run scheduled commands"""
+ # Read messages from stdin
+ for line in sys.stdin.read().splitlines():
+ try:
+ envelope = json.loads(line)['envelope']
+ except json.JSONDecodeError:
+ print(datetime.now(), line, "not valid json")
+ continue
+
+ quote = None
+ if not no_quote:
+ quote = Quote(timestamp=envelope['timestamp'],
+ author=envelope['source'])
+
+ send(send_cmd, bot.handle(envelope), quote)
+
+ send(send_cmd, bot.run_scheduled_cmds())
+
+
+def dbus_loop(bot: Geldschieberbot, send_cmd: str, no_quote: bool):
+ """Handle all messages read from stdin and run scheduled commands"""
+ from pydbus import SessionBus
+ from gi.repository import GLib, GObject
+
+ bus = SessionBus()
+ loop = GLib.MainLoop()
+
+ signal = bus.get('org.asamk.Signal')
+
+ # NOTE: when daemon was started without explicit `-u USERNAME`, replace the line above with
+ # signal = bus.get("org.asamk.Signal", "/org/asamk/Signal/_YOURPHONENUMBER")
+
+ def on_msg_recv(timestamp, source, groupid, message, _):
+ quote = None
+ if not no_quote:
+ quote = Quote(timestamp=timestamp, author=source)
+
+ envelope = {
+ "source": source,
+ "dataMessage": {
+ "timestamp": timestamp,
+ "message": message,
+ "groupInfo": {
+ "groupId": b64encode(bytes(groupid)).decode()
+ }
+ }
+ }
+ logfile_path = f'msgs/msg{datetime.now().isoformat()}.log'
+ with open(logfile_path, 'w', encoding='utf-8') as msglog:
+ print(f'{envelope}', file=msglog)
+ send(send_cmd, bot.handle(envelope), quote)
+ bot.save_state()
+
+ signal.onMessageReceived = on_msg_recv
+
+ def run_scheduled_cmds():
+ send(send_cmd, bot.run_scheduled_cmds())
+ bot.save_state()
+ return True
+
+ GObject.timeout_add_seconds(30, run_scheduled_cmds)
+
+ loop.run()
+
+
def main():
"""Read messages from stdin and send the bot's replies back"""
parser = argparse.ArgumentParser()
@@ -59,6 +125,9 @@ def main():
parser.add_argument('-g', '--group-id', help='the group id to listen to')
parser.add_argument('--send-cmd',
help='the shell command used to send messages')
+ parser.add_argument('--dbus',
+ action='store_true',
+ help='receive messages via dbus in a loop')
parser.add_argument('-nq',
'--no-quote',
help='not quote the message causing the reply',
@@ -81,22 +150,10 @@ def main():
die('A group id must be provided')
with Geldschieberbot(state_path, group_id, dry_run=args.dry_run) as bot:
- # Read cmds from stdin
- for line in sys.stdin.read().splitlines():
- try:
- envelope = json.loads(line)['envelope']
- except json.JSONDecodeError:
- print(datetime.now(), line, "not valid json")
- continue
-
- quote = None
- if not args.no_quote:
- quote = Quote(timestamp=envelope['timestamp'],
- author=envelope['source'])
-
- send(send_cmd, bot.handle(envelope), quote)
-
- send(send_cmd, bot.run_scheduled_cmds())
+ if not args.dbus:
+ single_shot(bot, send_cmd, args.no_quote)
+ else:
+ dbus_loop(bot, send_cmd, args.no_quote)
if __name__ == "__main__":
diff --git a/test.py b/test.py
index cd5b9bb..8edb7f7 100755
--- a/test.py
+++ b/test.py
@@ -77,7 +77,7 @@ def _run_bot(sender, cmd):
msg = msg_template.substitute(sender=sender, msg=cmd).replace("\n",
"\\n") + "\n"
res = subprocess.run(
- ["python3", "./single_shot.py", "--no-quote"],
+ ["python3", "./main.py", "--no-quote"],
text=True,
capture_output=True,
check=False,