diff --git a/src/teams_bot/bot.py b/src/teams_bot/bot.py index f1ad9fe..90c0eb4 100644 --- a/src/teams_bot/bot.py +++ b/src/teams_bot/bot.py @@ -6,7 +6,14 @@ import deltachat from deltachat import account_hookimpl from deltachat.capi import lib as dclib -from .commands import help_message, set_display_name, set_avatar, start_chat +from .commands import ( + crew_help, + set_display_name, + set_avatar, + start_chat, + outside_help, + set_outside_help, +) class SetupPlugin: @@ -74,7 +81,7 @@ class RelayPlugin: ) arguments = message.text.split(" ") if arguments[0] == "/help": - self.reply(message.chat, help_message(), quote=message) + self.reply(message.chat, crew_help(), quote=message) if arguments[0] == "/set_name": self.reply( message.chat, @@ -95,6 +102,18 @@ class RelayPlugin: for msg in outside_chat.get_messages(): self.forward_to_relay_group(msg, started_by_crew=True) self.reply(message.chat, result, quote=message) + if arguments[0] == "/set_outside_help": + try: + help_message = message.text.split("/set_outside_help ")[1] + except IndexError: + set_outside_help(self.kvstore, "") + return self.reply(message.chat, "Removed help message for outsiders", quote=message) + set_outside_help(self.kvstore, help_message) + self.reply( + message.chat, + f"Set help message for outsiders to {help_message}", + quote=message, + ) else: logging.debug("Ignoring message, just the crew chatting") @@ -112,6 +131,22 @@ class RelayPlugin: logging.debug("Ignoring message, just the crew chatting") else: + if message.text.startswith("/help"): + logging.info("Outsider %s asked for help", message.get_sender_contact().addr) + help_message = outside_help(self.kvstore) + if help_message == False: + help_message = f"I forward messages to the {self.account.get_config('displayname')} team." + if help_message == "": + logging.debug( + "Help message empty, forwarding message to relay group" + ) + else: + logging.info( + "Sending help text to %s: %s", + message.get_sender_contact().addr, + help_message, + ) + return self.reply(message.chat, help_message, quote=message) logging.debug("Forwarding message to relay group") self.forward_to_relay_group(message) @@ -132,6 +167,7 @@ class RelayPlugin: message.chat.id, ) return + """:TODO don't forward if message is the explanation message""" outside_chat.send_msg(message) def forward_to_relay_group(self, message: deltachat.Message, started_by_crew=False): diff --git a/src/teams_bot/commands.py b/src/teams_bot/commands.py index 1e03c65..ec221f5 100644 --- a/src/teams_bot/commands.py +++ b/src/teams_bot/commands.py @@ -1,24 +1,44 @@ import logging import deltachat +import pickledb from deltachat.capi import lib as dclib from deltachat.message import _view_type_mapping -def help_message() -> str: - """Get the help message +def crew_help() -> str: + """Get the help message for the crew chat :return: the help message """ help_text = """ Start a chat:\t/start_chat alice@example.org,bob@example.org Chat_Title Hello friends! -Change the bot's name:\t/set_name -Change the bot's avatar:\t/set_avatar (attach image) +Change the bot's name:\t/set_name Name +Change the bot's avatar:\t/set_avatar Show this help text:\t\t/help +Change the help message for outsiders:\t/set_outside_help Hello outsider """ return help_text +def outside_help(kvstore: pickledb.PickleDB) -> str: + """Get the help message for outsiders + + :param kvstore: the pickledDB key-value-store + :return: the help message + """ + return kvstore.get("outside_help_message") + + +def set_outside_help(kvstore: pickledb.PickleDB, help_message: str): + """Set the help message for outsiders + + :param kvstore: the pickeDB key-value-store + """ + logging.debug("Setting outside_help_message to %s", help_message) + kvstore.set("outside_help_message", help_message) + + def set_display_name(account: deltachat.Account, display_name: str) -> str: """Set the display name of the bot. @@ -81,7 +101,7 @@ def start_chat( return chat, "Chat successfully created." else: logging.error("Can't send message. sent_id: %s, msg.id: %s", sent_id, msg.id) - return chat, "Something went wrong...\n\n" + help_message() + return chat, "Something went wrong...\n\n" + crew_help() def get_message_view_type(message: deltachat.Message) -> str: