Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static select to block kit library #203

Merged
merged 2 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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