diff --git a/libs/hackbot-runtime/hackbot_runtime/actions/handlers/slack_handler.py b/libs/hackbot-runtime/hackbot_runtime/actions/handlers/slack_handler.py index d703b4338b..829235b8c5 100644 --- a/libs/hackbot-runtime/hackbot_runtime/actions/handlers/slack_handler.py +++ b/libs/hackbot-runtime/hackbot_runtime/actions/handlers/slack_handler.py @@ -17,6 +17,7 @@ from slack_sdk import WebClient from hackbot_runtime.actions.handlers.base import ActionResult, ApplyContext +from hackbot_runtime.actions.slack import HACKBOT_UI_URL log = logging.getLogger(__name__) @@ -36,11 +37,30 @@ def _client() -> WebClient: class PostMessageHandler: async def apply(self, params: dict[str, Any], ctx: ApplyContext) -> ActionResult: channel = params["channel"] + metadata = { + "event_type": "notification", + "event_payload": { + "notification_type": "info", + "source": { + "ref_id": ctx.run_id, + "ref_url": f"{HACKBOT_UI_URL}/runs/{ctx.run_id}", + }, + "context": { + "agent": ctx.agent, + "run_id": ctx.run_id, + }, + }, + } + try: # Slack reports application errors in a 200 body; the SDK raises # ``SlackApiError`` on them, so "channel_not_found" cannot read as a # delivered message. - response = _client().chat_postMessage(channel=channel, text=params["text"]) + response = _client().chat_postMessage( + channel=channel, + text=params["text"], + metadata=metadata, + ) except Exception as exc: log.exception("Failed to post to Slack channel %s", channel) return ActionResult.failed(str(exc)) diff --git a/libs/hackbot-runtime/tests/test_slack_handler.py b/libs/hackbot-runtime/tests/test_slack_handler.py index 361b7947c3..fd50ad48dc 100644 --- a/libs/hackbot-runtime/tests/test_slack_handler.py +++ b/libs/hackbot-runtime/tests/test_slack_handler.py @@ -42,18 +42,6 @@ def _fake_client(monkeypatch, error=None): return client -async def test_posts_recorded_message_and_returns_the_timestamp(monkeypatch): - client = _fake_client(monkeypatch) - result = await slack_handler.PostMessageHandler().apply( - {"channel": "#sheriff-notifications", "text": "a test regressed"}, _ctx() - ) - assert client.calls == [ - {"channel": "#sheriff-notifications", "text": "a test regressed"} - ] - assert result.status == "applied" - assert result.result == {"channel": "C1", "ts": "1700000000.000100"} - - async def test_posts_to_a_channel_id_as_recorded(monkeypatch): client = _fake_client(monkeypatch) await slack_handler.PostMessageHandler().apply(