matrix-alertbot/matrix_alertbot/bot_commands.py

187 lines
6.5 KiB
Python
Raw Normal View History

import logging
2022-07-06 00:54:13 +02:00
from diskcache import Cache
2021-01-10 04:30:07 +01:00
from nio import AsyncClient, MatrixRoom, RoomMessageText
2019-09-25 14:26:29 +02:00
from matrix_alertbot.alertmanager import AlertmanagerClient
2022-06-13 20:55:01 +02:00
from matrix_alertbot.chat_functions import react_to_event, send_text_to_room
from matrix_alertbot.config import Config
2022-07-06 00:54:13 +02:00
from matrix_alertbot.errors import AlertmanagerError, AlertNotFoundError
logger = logging.getLogger(__name__)
2021-01-10 04:30:07 +01:00
class Command:
def __init__(
self,
client: AsyncClient,
cache: Cache,
alertmanager: AlertmanagerClient,
2021-01-10 04:30:07 +01:00
config: Config,
command: str,
room: MatrixRoom,
event: RoomMessageText,
):
"""A command made by a user.
2019-09-25 14:26:29 +02:00
Args:
client: The client to communicate to matrix with.
2019-09-25 14:26:29 +02:00
cache: Bot cache.
2019-09-25 14:26:29 +02:00
config: Bot configuration parameters.
command: The command and arguments.
2019-09-25 14:26:29 +02:00
room: The room the command was sent in.
2019-09-25 14:26:29 +02:00
event: The event describing the command.
2019-09-25 14:26:29 +02:00
"""
self.client = client
self.cache = cache
self.alertmanager = alertmanager
2019-10-26 01:40:05 +02:00
self.config = config
2019-09-25 14:26:29 +02:00
self.command = command
self.room = room
self.event = event
self.args = self.command.split()[1:]
2022-06-14 23:37:54 +02:00
async def process(self) -> None:
2019-09-25 14:26:29 +02:00
"""Process the command"""
if self.command.startswith("ack"):
await self._ack()
2022-07-06 00:54:13 +02:00
if self.command.startswith("unack") or self.command.startswith("nack"):
await self._unack()
elif self.command.startswith("react"):
await self._react()
2019-09-25 14:26:29 +02:00
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(
2022-07-06 00:54:13 +02:00
f"Receiving a command to create a silence 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
2022-07-05 23:35:19 +02:00
logger.debug(f"Read alert fingerprints for event {alert_event_id} from cache")
2022-07-06 00:54:13 +02:00
count_created_silences = 0
2022-07-05 23:35:19 +02:00
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}"
)
2022-07-06 00:54:13 +02:00
try:
silence_id = self.alertmanager.create_silence(
alert_fingerprint, duration, self.room.user_name(self.event.sender)
)
except (AlertNotFoundError, AlertmanagerError) as e:
logger.error(f"Unable to create silence: {e}")
continue
count_created_silences += 1
await send_text_to_room(
self.client,
self.room.room_id,
f"Created {count_created_silences} silences with a duration of {duration}",
)
async def _unack(self) -> None:
"""Delete an alert's acknowledgement of an alert and remove corresponding silence in Alertmanager"""
logger.debug(
f"Receiving a command to delete a silence | "
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")
count_removed_silences = 0
alert_fingerprints = self.cache[alert_event_id]
for alert_fingerprint in alert_fingerprints:
logger.debug(
f"Delete silence for alert with fingerprint {alert_fingerprint}"
2022-07-05 23:35:19 +02:00
)
2022-07-06 00:54:13 +02:00
try:
self.alertmanager.delete_silence(alert_fingerprint)
except (AlertNotFoundError, AlertmanagerError) as e:
logger.error(f"Unable to delete silence: {e}")
continue
count_removed_silences += 1
await send_text_to_room(
self.client,
self.room.room_id,
2022-07-06 00:54:13 +02:00
f"Removed {count_removed_silences} silences",
)
2019-09-25 14:26:29 +02:00
2022-06-14 23:37:54 +02:00
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
)
2022-06-14 23:37:54 +02:00
async def _show_help(self) -> None:
2019-09-25 14:26:29 +02:00
"""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}"
)
2019-09-25 14:26:29 +02:00
if not self.args:
2020-08-10 00:02:07 +02:00
text = (
"Hello, I am a bot made with matrix-nio! Use `help commands` to view "
"available commands."
)
2019-09-25 14:26:29 +02:00
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: ..."
2019-09-25 14:26:29 +02:00
else:
text = "Unknown help topic!"
await send_text_to_room(self.client, self.room.room_id, text)
2022-06-14 23:37:54 +02:00
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}"
)
2019-09-25 14:26:29 +02:00
await send_text_to_room(
self.client,
self.room.room_id,
f"Unknown command '{self.command}'. Try the 'help' command for more information.",
)