initial logic for handling incoming messages

This commit is contained in:
missytake 2023-10-07 15:02:45 +02:00
parent 2dc309dd09
commit 2905730cca
2 changed files with 52 additions and 1 deletions

View file

@ -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.

View file

@ -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")