Skip to content

Commit

Permalink
added attachment and message resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklambourne committed Aug 1, 2019
1 parent aeb4173 commit c629bd4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
47 changes: 47 additions & 0 deletions slackblocks/attachments.py
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)


29 changes: 26 additions & 3 deletions slackblocks/messages.py
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 added test/__init__.py
Empty file.

0 comments on commit c629bd4

Please sign in to comment.