Skip to content

Commit

Permalink
Add option to add instructions url to the help messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Era-Dorta committed Jan 5, 2025
1 parent 781d12e commit 7d05633
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
20 changes: 15 additions & 5 deletions src/signalblast/broadcastbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,40 @@ 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)

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

Expand Down
10 changes: 10 additions & 0 deletions src/signalblast/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
Expand Down Expand Up @@ -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:
Expand All @@ -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()
21 changes: 17 additions & 4 deletions src/signalblast/message_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7d05633

Please sign in to comment.