From 452fdf26e033fc2e128d5dca1e83486ebb9da414 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 7 Oct 2023 21:40:21 +0200 Subject: [PATCH] add chat command to set display_name --- src/teams_bot/bot.py | 23 +++++++++++++++++++---- src/teams_bot/cli.py | 2 ++ src/teams_bot/commands.py | 22 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/teams_bot/commands.py diff --git a/src/teams_bot/bot.py b/src/teams_bot/bot.py index 11357af..313e024 100644 --- a/src/teams_bot/bot.py +++ b/src/teams_bot/bot.py @@ -3,6 +3,9 @@ from threading import Event import deltachat from deltachat import account_hookimpl +from deltachat.capi import lib as dclib + +from .commands import set_display_name, help_message class SetupPlugin: @@ -51,9 +54,13 @@ class RelayPlugin: message.get_sender_contact().addr, message.text, ) - """:TODO handle command""" + arguments = message.text.split(" ") + if arguments[0] == "/help": + self.reply(message.chat, help_message(), quote=message) + if arguments[0] == "/set_name": + self.reply(message.chat, set_display_name(self.account, arguments[1]), quote=message) else: - logging.debug("Ignoring message, just admins chatting") + logging.debug("Ignoring message, just the crew chatting") elif self.is_relay_group(message.chat): if message.quote: @@ -64,14 +71,22 @@ class RelayPlugin: logging.debug("Forwarding message to outsider") self.forward_to_outside(message) else: - logging.debug("Ignoring message, just admins chatting") + logging.debug("Ignoring message, just the crew chatting") else: - logging.debug("Ignoring message, just admins chatting") + logging.debug("Ignoring message, just the crew chatting") else: logging.debug("Forwarding message to relay group") self.forward_to_relay_group(message) + def reply(self, chat: deltachat.Chat, text: str, quote: deltachat.Message = None): + """Send a reply to a chat, with optional quote.""" + msg = deltachat.Message.new_empty(self.account, view_type="text") + msg.set_text(text) + msg.quote = quote + sent_id = dclib.dc_send_msg(self.account._dc_context, chat.id, msg._dc_msg) + assert sent_id == msg.id + def forward_to_outside(self, message: deltachat.Message): """forward an answer to an outsider.""" bot_localpart = self.account.get_config('addr').split('@')[0] diff --git a/src/teams_bot/cli.py b/src/teams_bot/cli.py index 04647a2..744a29e 100644 --- a/src/teams_bot/cli.py +++ b/src/teams_bot/cli.py @@ -114,7 +114,9 @@ def run(ctx, db: str, verbose: int): set_log_level(verbose, db) ac = deltachat.Account(db) + display_name = ac.get_config("displayname") ac.run_account(account_plugins=[RelayPlugin(ac)], show_ffi=verbose) + ac.set_config("displayname", display_name) try: ac.wait_shutdown() except KeyboardInterrupt: diff --git a/src/teams_bot/commands.py b/src/teams_bot/commands.py new file mode 100644 index 0000000..7eab2d5 --- /dev/null +++ b/src/teams_bot/commands.py @@ -0,0 +1,22 @@ +import deltachat + + +def help_message() -> str: + """Get the help message + + :return: the help message + """ + help_text = """ +Change the bot's name:\t/set_name +Show this help text:\t\t/help + """ + return help_text + + +def set_display_name(account: deltachat.Account, display_name: str) -> str: + """Set the display name of the bot. + + :return: a success message + """ + account.set_config("displayname", display_name) + return "Display name changed to " + display_name