Skip to content

Commit

Permalink
Add static select to block kit library (#203)
Browse files Browse the repository at this point in the history
Added static select to block kit and action handler for this type of action
  • Loading branch information
evnsio authored Mar 9, 2020
1 parent ea1dfb6 commit c15ef62
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
35 changes: 35 additions & 0 deletions response/slack/block_kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,38 @@ def serialize(self):
"type": self.text_type,
"text": self.text if not self.title else f"*{self.title}*\n{text}",
}


class StaticSelectOption:
def __init__(self, text, value):
self.text = text
self.value = value

def serialize(self):
return {
"text": {"type": "plain_text", "emoji": True, "text": self.text},
"value": self.value,
}


class StaticSelect:
def __init__(self, options, action_id, placeholder_text=None):
self.options = options
self.placeholder_text = placeholder_text
self.action_id = action_id

def serialize(self):
serialized = {
"type": "static_select",
"action_id": self.action_id,
"options": [o.serialize() for o in self.options],
}

if self.placeholder_text:
serialized["placeholder"] = {
"type": "plain_text",
"emoji": True,
"text": self.placeholder_text,
}

return serialized
24 changes: 16 additions & 8 deletions response/slack/decorators/action_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@


class ActionContext:
def __init__(
self, incident, user_id, message, button_value, trigger_id, response_url
):
def __init__(self, incident, user_id, message, value, trigger_id, response_url):
self.incident = incident
self.user_id = user_id
self.message = message
self.button_value = button_value
self.value = value
self.trigger_id = trigger_id
self.response_url = response_url

Expand All @@ -39,8 +37,10 @@ def remove_action_handler(callback_id):
@after_response.enable
def handle_action(payload):
actions = payload["actions"]

if actions:
action_id = actions[0]["action_id"]
action = actions[0]
action_id = action["action_id"]

if action_id not in SLACK_ACTION_MAPPINGS:
logger.error(f"Can't find handler for action id {action_id}")
Expand All @@ -50,19 +50,27 @@ def handle_action(payload):

user_id = payload["user"]["id"]
channel_id = payload["channel"]["id"]
button_value = actions[0]["value"]
message = payload["message"]
trigger_id = payload["trigger_id"]
response_url = payload["response_url"]

action_type = action["type"]
if action_type == "button":
value = action["value"]
elif action_type == "static_select":
value = action["selected_option"]["value"]
else:
logger.error(f"Can't handle action with type {action_type}")
return

# we want to tie all actions to an incident, and have two ways to do this:
# - if action comes from a comms channel, lookup the incident by comms channel id
# - if not in comms channel, we rely on the button value containing the incident id
try:
comms_channel = CommsChannel.objects.get(channel_id=channel_id)
incident = comms_channel.incident
except CommsChannel.DoesNotExist:
incident_id = button_value
incident_id = value
incident = Incident.objects.get(pk=incident_id)
except Incident.DoesNotExist:
logger.error(
Expand All @@ -74,7 +82,7 @@ def handle_action(payload):
incident=incident,
user_id=user_id,
message=message,
button_value=button_value,
value=value,
trigger_id=trigger_id,
response_url=response_url,
)
Expand Down

0 comments on commit c15ef62

Please sign in to comment.