diff options
| -rwxr-xr-x | main.py (renamed from single_shot.py) | 91 | ||||
| -rwxr-xr-x | test.py | 2 |
2 files changed, 75 insertions, 18 deletions
@@ -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__": @@ -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, |
