From 7d0563374d9c6e9e3d513aaf13fb97bb97fadd96 Mon Sep 17 00:00:00 2001 From: Era Dorta Date: Sun, 5 Jan 2025 18:12:16 +0100 Subject: [PATCH] Add option to add instructions url to the help messages --- src/signalblast/broadcastbot.py | 20 +++++++++++++++----- src/signalblast/main.py | 10 ++++++++++ src/signalblast/message_handler.py | 21 +++++++++++++++++---- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/signalblast/broadcastbot.py b/src/signalblast/broadcastbot.py index 8c93927..4951c4a 100644 --- a/src/signalblast/broadcastbot.py +++ b/src/signalblast/broadcastbot.py @@ -84,13 +84,14 @@ def start(self) -> None: def scheduler(self) -> AsyncIOScheduler: return self._bot.scheduler - async def load_data( + async def load_data( # noqa: PLR0913 Too many arguments in function definition self, logger: Logger, admin_pass: str | None, expiration_time: int | None, signal_data_path: Path, welcome_message: str | None = None, + instructions_url: str | None = None, ) -> None: self.subscribers = await Users.load_from_file(self.subscribers_data_path) self.banned_users = await Users.load_from_file(self.banned_users_data_path) @@ -98,16 +99,25 @@ async def load_data( self.admin = await Admin.load_from_file(admin_pass) self.message_handler = MessageHandler(signal_data_path / "attachments") - self.help_message = self.message_handler.compose_help_message(is_help=True) - self.wrong_command_message = self.message_handler.compose_help_message(is_help=False) - self.admin_help_message = self.message_handler.compose_help_message(add_admin_commands=True) + self.help_message = self.message_handler.compose_help_message(instructions_url=instructions_url) + self.wrong_command_message = self.message_handler.compose_help_message( + is_help=False, + instructions_url=instructions_url, + ) + self.admin_help_message = self.message_handler.compose_help_message( + add_admin_commands=True, + instructions_url=instructions_url, + ) self.admin_wrong_command_message = self.message_handler.compose_help_message( add_admin_commands=True, is_help=False, + instructions_url=instructions_url, ) self.welcome_message = self.message_handler.compose_welcome_message(welcome_message) - self.must_subscribe_message = self.message_handler.compose_must_subscribe_message() + self.must_subscribe_message = self.message_handler.compose_must_subscribe_message( + instructions_url=instructions_url, + ) self.expiration_time = expiration_time diff --git a/src/signalblast/main.py b/src/signalblast/main.py index 4cb5ef6..9e6192b 100755 --- a/src/signalblast/main.py +++ b/src/signalblast/main.py @@ -45,6 +45,7 @@ async def initialise_bot( # noqa: PLR0913 Too many arguments in function defini storage: dict[str, str] | None = None, health_check_port: int = 15556, health_check_receiver: str | None = None, + instructions_url: str | None = None, ) -> BroadcasBot: config = { "signal_service": signal_service, @@ -66,6 +67,7 @@ async def initialise_bot( # noqa: PLR0913 Too many arguments in function defini expiration_time=expiration_time, signal_data_path=signal_data_path, welcome_message=welcome_message, + instructions_url=instructions_url, ) bot.register(Subscribe(bot=bot)) @@ -146,6 +148,13 @@ async def initialise_bot( # noqa: PLR0913 Too many arguments in function defini help="the contact or group to send messages for health checks", ) + args_parser.add_argument( + "--instructions_url", + type=str, + default=os.environ.get("SIGNALBLAST_INSTRUCTIONS_URL"), + help="URL for the help message", + ) + args = args_parser.parse_args() if args.phone_number is None: @@ -163,6 +172,7 @@ async def initialise_bot( # noqa: PLR0913 Too many arguments in function defini welcome_message=args.welcome_message, health_check_port=args.health_check_port, health_check_receiver=args.health_check_receiver, + instructions_url=args.instructions_url, ), ) bot.start() diff --git a/src/signalblast/message_handler.py b/src/signalblast/message_handler.py index 6009395..88f35b5 100644 --- a/src/signalblast/message_handler.py +++ b/src/signalblast/message_handler.py @@ -50,21 +50,34 @@ def _add_admin_commands(message: str) -> str: return message @staticmethod - def compose_help_message(*, add_admin_commands: bool = False, is_help: bool = True) -> str: + def compose_help_message( + *, + add_admin_commands: bool = False, + is_help: bool = True, + instructions_url: str | None = None, + ) -> str: message = MessageHandler._compose_help_message(add_admin_commands=add_admin_commands) + message_url = "" + if instructions_url is not None: + message_url = "\nPlease have a look at the instructions if you haven't already.\n" + message_url += instructions_url if is_help: - return "I'm happy to help! This are the commands that you can use:\n\n" + message + return "I'm happy to help! This are the commands that you can use:\n\n" + message + message_url message = "I'm sorry, I didn't understand you but I understand the following commands:\n\n" + message message += "\nPlease try again" return message @staticmethod - def compose_must_subscribe_message() -> str: + def compose_must_subscribe_message(instructions_url: str | None = None) -> str: message = "To be able to send messages you must sign up.\n" message += "Please sign up by sending:\n" message += f"\t{PublicCommandStrings.subscribe}\n" message += "and try again after that." - return message + message_url = "" + if instructions_url is not None: + message_url = "\nPlease have a look at the instructions if you haven't already.\n" + message_url += instructions_url + return message + message_url @staticmethod def compose_message_to_admin(message: str, user: str | None) -> str: