import logging from nio import AsyncClient, MatrixRoom, RoomMessageText from matrix_alertbot.alertmanager import AlertmanagerClient from matrix_alertbot.cache import Cache from matrix_alertbot.chat_functions import react_to_event, send_text_to_room from matrix_alertbot.config import Config logger = logging.getLogger(__name__) class Command: def __init__( self, client: AsyncClient, cache: Cache, alertmanager: AlertmanagerClient, config: Config, command: str, room: MatrixRoom, event: RoomMessageText, ): """A command made by a user. Args: client: The client to communicate to matrix with. cache: Bot cache. config: Bot configuration parameters. command: The command and arguments. room: The room the command was sent in. event: The event describing the command. """ self.client = client self.cache = cache self.alertmanager = alertmanager self.config = config self.command = command self.room = room self.event = event self.args = self.command.split()[1:] async def process(self) -> None: """Process the command""" if self.command.startswith("ack"): await self._ack() elif self.command.startswith("react"): await self._react() elif self.command.startswith("help"): await self._show_help() else: await self._unknown_command() async def _ack(self) -> None: """Acknowledge an alert and silence it for a certain duration in Alertmanager""" if len(self.args) > 0: duration = " ".join(self.args) else: duration = "1d" logger.debug( f"Acknowledging alert with fingerprint {self.room.display_name} for a duration of {duration} | " f"{self.room.user_name(self.event.sender)}: {self.event.body}" ) source_content = self.event.source["content"] try: alert_event_id = source_content["m.relates_to"]["m.in_reply_to"]["event_id"] except KeyError: logger.debug("Unable to find the event ID of the alert") return logger.debug(f"Read alert fingerprints for event {alert_event_id} from cache") silence_ids = [] alert_fingerprints = self.cache[alert_event_id] for alert_fingerprint in alert_fingerprints: logger.debug( f"Create silence for alert with fingerprint {alert_fingerprint} for a duration of {duration}" ) silence_id = self.alertmanager.create_silence( alert_fingerprint, duration, self.room.user_name(self.event.sender) ) silence_ids.append(silence_id) silences = ", ".join(silence_ids) await send_text_to_room( self.client, self.room.room_id, f"Created silences {silences} for a duration of {duration}", ) async def _react(self) -> None: """Make the bot react to the command message""" # React with a start emoji reaction = "⭐" logger.debug( f"Reacting with {reaction} to room {self.room.display_name} | " f"{self.room.user_name(self.event.sender)}: {self.event.body}" ) await react_to_event( self.client, self.room.room_id, self.event.event_id, reaction ) # React with some generic text reaction = "Some text" await react_to_event( self.client, self.room.room_id, self.event.event_id, reaction ) async def _show_help(self) -> None: """Show the help text""" logger.debug( f"Displaying help to room {self.room.display_name} | " f"{self.room.user_name(self.event.sender)}: {self.event.body}" ) if not self.args: text = ( "Hello, I am a bot made with matrix-nio! Use `help commands` to view " "available commands." ) await send_text_to_room(self.client, self.room.room_id, text) return topic = self.args[0] if topic == "rules": text = "These are the rules!" elif topic == "commands": text = "Available commands: ..." else: text = "Unknown help topic!" await send_text_to_room(self.client, self.room.room_id, text) async def _unknown_command(self) -> None: logger.debug( f"Sending unknown command response to room {self.room.display_name} | " f"{self.room.user_name(self.event.sender)}: {self.event.body}" ) await send_text_to_room( self.client, self.room.room_id, f"Unknown command '{self.command}'. Try the 'help' command for more information.", )