From 2905730ccae5f59442b31e2ff85543316e1c2207 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 7 Oct 2023 15:02:45 +0200 Subject: [PATCH] initial logic for handling incoming messages --- src/teams_bot/bot.py | 41 +++++++++++++++++++++++++++++++++++++++++ src/teams_bot/cli.py | 12 +++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/teams_bot/bot.py b/src/teams_bot/bot.py index 13e6133..9fd19bd 100644 --- a/src/teams_bot/bot.py +++ b/src/teams_bot/bot.py @@ -25,6 +25,47 @@ class SetupPlugin: self.message_sent.set() +class RelayPlugin: + def __init__(self, account: deltachat.Account): + self.account = account + + @account_hookimpl + def ac_incoming_message(self, message: deltachat.Message): + """This method is called on every incoming message and decides what to do with it.""" + logging.info( + "New message from %s in chat %s: %s", + message.get_sender_contact().addr, + message.chat.get_name(), + message.text, + ) + + if message.is_system_message(): + logging.debug("This is a system message") + """:TODO handle chat name changes""" + return + + if message.chat.id == get_crew_id(self.account): + if message.text.startswith("/"): + logging.debug("handling command by %s: %s", message.get_sender_contact().addr, message.text) + """:TODO handle command""" + else: + logging.debug("Ignoring message, just admins chatting") + + elif message.chat.get_contacts() == self.account.get_chat_by_id( + get_crew_id(self.account) + ).get_contacts() and message.chat.get_name().startswith( + "[%s] " % (self.account.get_config("addr").split("@")[0],) + ): + if message.quote.get_sender_contact() == self.account.get_self_contact(): + """:TODO forward to original sender""" + else: + logging.debug("Ignoring message, just admins chatting") + + else: + logging.debug("Forwarding message to relay group") + """:TODO forward message to relay group""" + + def get_crew_id(ac: deltachat.Account, setupplugin: SetupPlugin = None) -> int: """Get the group ID of the crew group if it exists; warn old crews if they might still believe they are the crew. diff --git a/src/teams_bot/cli.py b/src/teams_bot/cli.py index 8aa8f2c..04647a2 100644 --- a/src/teams_bot/cli.py +++ b/src/teams_bot/cli.py @@ -5,7 +5,7 @@ import click import qrcode import deltachat -from .bot import SetupPlugin, get_crew_id +from .bot import SetupPlugin, RelayPlugin, get_crew_id def set_log_level(verbose: int, db: str): @@ -113,6 +113,16 @@ def init(ctx, email: str, password: str, db: str, verbose: int): def run(ctx, db: str, verbose: int): set_log_level(verbose, db) + ac = deltachat.Account(db) + ac.run_account(account_plugins=[RelayPlugin(ac)], show_ffi=verbose) + try: + ac.wait_shutdown() + except KeyboardInterrupt: + logging.info("Received KeyboardInterrupt") + print("Shutting down...") + ac.shutdown() + ac.wait_shutdown() + def main(): teams_bot(auto_envvar_prefix="TEAMS")