-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added attachment and message resolution
- Loading branch information
1 parent
aeb4173
commit c629bd4
Showing
3 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from enum import Enum | ||
from json import dumps | ||
from typing import Any, Dict, List, Optional, Union | ||
from .blocks import Block | ||
from .error import InvalidUsageError | ||
|
||
|
||
class Color(Enum): | ||
GOOD = "good" | ||
WARNING = "warning" | ||
DANGER = "danger" | ||
RED = "#ff0000" | ||
BLUE = "#0000ff" | ||
YELLOW = "#ffff00" | ||
GREEN = "#00ff00" | ||
ORANGE = "#ff8800" | ||
PURPLE = "#8800ff" | ||
BLACK = "#000000" | ||
|
||
|
||
class Attachment: | ||
""" | ||
Secondary content can be attached to messages to include lower priority content | ||
- content that doesn't necessarily need to be seen to appreciate the intent of | ||
the message, but perhaps adds further context or additional information. | ||
""" | ||
def __init__(self, | ||
blocks: Optional[List[Block]], | ||
color: Optional[Union[str, Color]]): | ||
self.blocks = blocks | ||
if type(color) is Color: | ||
self.color = color.value | ||
elif type(color) is str: | ||
if len(color) == 7 and color[0] == "#": | ||
self.color = color | ||
else: | ||
raise InvalidUsageError("Color must be a valid hex code (e.g. #ffffff)") | ||
|
||
def _resolve(self) -> Dict[str, Any]: | ||
attachment = dict() | ||
attachment["blocks"] = [block._resolve() for block in self.blocks] | ||
return attachment | ||
|
||
def __repr__(self) -> str: | ||
return dumps(self._resolve(), indent=4) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
from typing import List, Optional | ||
from json import dumps | ||
from typing import Any, Dict, List, Optional | ||
from .attachments import Attachment | ||
from .blocks import Block | ||
|
||
|
||
class Message: | ||
""" | ||
A Slack message object that can be converted to a JSON string for use with | ||
the Slack message API. | ||
""" | ||
def __init__(self, | ||
text: Optional[str], | ||
blocks: Optional[List[Block]], | ||
attachments, | ||
attachments: Optional[List[Attachment]], | ||
thread_ts: Optional[str] = None, | ||
mrkdwm: bool = True): | ||
mrkdwn: bool = True): | ||
self.blocks = blocks | ||
self.text = text | ||
self.attachments = attachments | ||
self.thread_ts = thread_ts | ||
self.mrkdwn = mrkdwn | ||
|
||
def _resolve(self) -> Dict[str, Any]: | ||
message = dict() | ||
message["blocks"] = [block._resolve() for block in self.blocks] | ||
message["attachments"] = [attachment._resolve() for attachment in self.attachments] | ||
message["mrkdwn"] = self.mrkdwn | ||
if self.thread_ts: | ||
message["thread_ts"] = self.thread_ts | ||
if self.text: | ||
message["text"] = self.text | ||
return message | ||
|
||
def __repr__(self) -> str: | ||
return dumps(self._resolve()) |
Empty file.