Skip to content

Commit 01e06f8

Browse files
committed
[v3] Add Dispatch Action in Block Kit #841
1 parent 5217e17 commit 01e06f8

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

slack_sdk/models/blocks/basic_components.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,35 @@ def to_dict(self, option_type: str = "block") -> dict: # skipcq: PYL-W0221
465465
if self._style:
466466
json["style"] = self._style
467467
return json
468+
469+
470+
class DispatchActionConfig(JsonObject):
471+
attributes = {"trigger_actions_on"}
472+
473+
@classmethod
474+
def parse(cls, config: Union["DispatchActionConfig", dict]):
475+
if config:
476+
if isinstance(config, DispatchActionConfig): # skipcq: PYL-R1705
477+
return config
478+
elif isinstance(config, dict):
479+
return DispatchActionConfig(**config)
480+
else:
481+
# Not yet implemented: show some warning here
482+
return None
483+
return None
484+
485+
def __init__(
486+
self, *, trigger_actions_on: Optional[list] = None,
487+
):
488+
"""
489+
Determines when a plain-text input element will return a block_actions interaction payload.
490+
https://api.slack.com/reference/block-kit/composition-objects#dispatch_action_config
491+
"""
492+
self._trigger_actions_on = trigger_actions_on or []
493+
494+
def to_dict(self) -> dict: # skipcq: PYL-W0221
495+
self.validate_json()
496+
json = {}
497+
if self._trigger_actions_on:
498+
json["trigger_actions_on"] = self._trigger_actions_on
499+
return json

slack_sdk/models/blocks/block_elements.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
)
1414
from .basic_components import ButtonStyles
1515
from .basic_components import ConfirmObject
16+
from .basic_components import DispatchActionConfig
1617
from .basic_components import MarkdownTextObject
1718
from .basic_components import Option
1819
from .basic_components import OptionGroup
@@ -957,7 +958,13 @@ class PlainTextInputElement(InputInteractiveElement):
957958
@property
958959
def attributes(self) -> Set[str]:
959960
return super().attributes.union(
960-
{"initial_value", "multiline", "min_length", "max_length"}
961+
{
962+
"initial_value",
963+
"multiline",
964+
"min_length",
965+
"max_length",
966+
"dispatch_action_config",
967+
}
961968
)
962969

963970
def __init__(
@@ -970,6 +977,7 @@ def __init__(
970977
multiline: Optional[bool] = None,
971978
min_length: Optional[int] = None,
972979
max_length: Optional[int] = None,
980+
dispatch_action_config: Optional[Union[dict, DispatchActionConfig]] = None,
973981
**others: dict,
974982
):
975983
"""
@@ -989,6 +997,7 @@ def __init__(
989997
self.multiline = multiline
990998
self.min_length = min_length
991999
self.max_length = max_length
1000+
self.dispatch_action_config = dispatch_action_config
9921001

9931002

9941003
# -------------------------------------------------

slack_sdk/models/blocks/blocks.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ class InputBlock(Block):
283283

284284
@property
285285
def attributes(self) -> Set[str]:
286-
return super().attributes.union({"label", "hint", "element", "optional"})
286+
return super().attributes.union(
287+
{"label", "hint", "element", "optional", "dispatch_action"}
288+
)
287289

288290
def __init__(
289291
self,
@@ -292,6 +294,7 @@ def __init__(
292294
element: Union[str, dict, InputInteractiveElement],
293295
block_id: Optional[str] = None,
294296
hint: Optional[Union[str, dict, PlainTextObject]] = None,
297+
dispatch_action: Optional[bool] = None,
295298
optional: Optional[bool] = None,
296299
**others: dict,
297300
):
@@ -306,6 +309,7 @@ def __init__(
306309
self.label = TextObject.parse(label, default_type=PlainTextObject.type)
307310
self.element = BlockElement.parse(element)
308311
self.hint = TextObject.parse(hint, default_type=PlainTextObject.type)
312+
self.dispatch_action = dispatch_action
309313
self.optional = optional
310314

311315
@JsonValidator(f"label attribute cannot exceed {label_max_length} characters")

tests/slack_sdk/web/classes/test_blocks.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,19 @@ def test_document(self):
545545
"label": {"type": "plain_text", "text": "Label", "emoji": True,},
546546
"hint": {"type": "plain_text", "text": "some hint", "emoji": True,},
547547
},
548+
{
549+
"dispatch_action": True,
550+
"type": "input",
551+
"element": {
552+
"type": "plain_text_input",
553+
"action_id": "plain_text_input-action"
554+
},
555+
"label": {
556+
"type": "plain_text",
557+
"text": "Label",
558+
"emoji": True
559+
}
560+
}
548561
]
549562
for input in blocks:
550563
self.assertDictEqual(input, InputBlock(**input).to_dict())

tests/slack_sdk/web/classes/test_elements.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,15 @@ def test_document_2(self):
685685
}
686686
self.assertDictEqual(input, PlainTextInputElement(**input).to_dict())
687687

688+
def test_document_3(self):
689+
input = {
690+
"type": "plain_text_input",
691+
"multiline": True,
692+
"dispatch_action_config": {
693+
"trigger_actions_on": ["on_character_entered"]
694+
}
695+
}
696+
self.assertDictEqual(input, PlainTextInputElement(**input).to_dict())
688697

689698
# -------------------------------------------------
690699
# Radio Buttons

0 commit comments

Comments
 (0)