import requests from slack_sdk import WebClient class Alarm: def __init__(self, slack): self.url = slack.url self.username = slack.username self.icon_emoji = slack.icon_emoji self.channel_id = slack.channel_id self.bot_token = slack.bot_token self.client = WebClient(self.bot_token) def _get_color(self, level) -> str: if level == "ignore": color = "#36A64F" # Green elif level == "warning": color = "#F08080" # Red return color def send(self, level, text): color = self._get_color(level) message = { "attachments": [{"text": text, "color": color}], "username": self.username, "icon_emoji": self.icon_emoji, } requests.post(self.url, json=message) def send_sdk(self, level, text): color = self._get_color(level) re = self.client.chat_postMessage( channel=self.channel_id, attachments=[{"fallback": "fallback", "text": text, "color": color}], icon_emoji=self.icon_emoji, user_name=self.username, ) return re.data["ts"] def post_reply_to_thread(self, level, thread_ts, text): color = self._get_color(level) self.client.chat_postMessage( channel=self.channel_id, attachments=[{"fallback": "fallback", "text": text, "color": color}], icon_emoji=self.icon_emoji, thread_ts=thread_ts, user_name=self.username, ) def post_reaction(self, thread_ts, emoji_name): # emoji_name ex. "x", "완료" self.client.reactions_add( channel=self.channel_id, name=emoji_name, timestamp=thread_ts ) class AlertLevel: IGNORE = "ignore" WARNING = "warning" MAJOR = "major" @classmethod def get_levels(self): return [self.IGNORE, self.WARNING, self.MAJOR]