-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create service for handling block actions
We'll send all block actions to this service for sorting and handling. Currently demonstrate that we can react to a template selection action.
- Loading branch information
1 parent
c09e353
commit 9b5f673
Showing
5 changed files
with
70 additions
and
1 deletion.
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,4 @@ | ||
"""Application constants.""" | ||
|
||
SELECT_PROJECT_TEMPLATE_ACTION = "templatebot_select_project_template" | ||
"""Action ID of the static select menu for selecting a project template.""" |
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
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
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,54 @@ | ||
"""Slack service for handling block actions.""" | ||
|
||
from __future__ import annotations | ||
|
||
from rubin.squarebot.models.kafka import SquarebotSlackBlockActionsValue | ||
from rubin.squarebot.models.slack import ( | ||
SlackBlockActionBase, | ||
SlackStaticSelectAction, | ||
) | ||
from structlog.stdlib import BoundLogger | ||
|
||
from templatebot.constants import SELECT_PROJECT_TEMPLATE_ACTION | ||
from templatebot.storage.slack import SlackWebApiClient | ||
|
||
__all__ = ["SlackBlockActionsService"] | ||
|
||
|
||
class SlackBlockActionsService: | ||
"""A service for processing Slack block actions.""" | ||
|
||
def __init__( | ||
self, logger: BoundLogger, slack_client: SlackWebApiClient | ||
) -> None: | ||
self._logger = logger | ||
self._slack_client = slack_client | ||
|
||
async def handle_block_actions( | ||
self, payload: SquarebotSlackBlockActionsValue | ||
) -> None: | ||
"""Handle a Slack block_actions interaction.""" | ||
for action in payload.actions: | ||
if action.action_id == SELECT_PROJECT_TEMPLATE_ACTION: | ||
await self.handle_project_template_selection( | ||
action=action, payload=payload | ||
) | ||
|
||
async def handle_project_template_selection( | ||
self, | ||
*, | ||
action: SlackBlockActionBase, | ||
payload: SquarebotSlackBlockActionsValue, | ||
) -> None: | ||
"""Handle a project template selection.""" | ||
if not isinstance(action, SlackStaticSelectAction): | ||
raise TypeError( | ||
f"Expected action for {SELECT_PROJECT_TEMPLATE_ACTION} to be " | ||
f"a SlackStaticSelectAction, but got {type(action)}" | ||
) | ||
selected_option = action.selected_option | ||
self._logger.debug( | ||
"Selected project template", | ||
value=selected_option.value, | ||
text=selected_option.text.text, | ||
) |
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