Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -36,11 +37,29 @@ def _client() -> WebClient:
class PostMessageHandler:
async def apply(self, params: dict[str, Any], ctx: ApplyContext) -> ActionResult:
channel = params["channel"]
metadata = {
"event_type": "notification",
"source": {
"ref_id": ctx.run_id,
"ref_url": f"{HACKBOT_UI_URL}/runs/{ctx.run_id}",
},
"event_payload": {
"context": {
"agent": ctx.agent,
"run_id": ctx.run_id,
},
},
}
Comment on lines +40 to +52

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))
Expand Down
16 changes: 15 additions & 1 deletion libs/hackbot-runtime/tests/test_slack_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest
from hackbot_runtime.actions.handlers import ApplyContext, slack_handler
from hackbot_runtime.actions.slack import HACKBOT_UI_URL
from slack_sdk.errors import SlackApiError


Expand Down Expand Up @@ -48,7 +49,20 @@ async def test_posts_recorded_message_and_returns_the_timestamp(monkeypatch):
{"channel": "#sheriff-notifications", "text": "a test regressed"}, _ctx()
)
assert client.calls == [
{"channel": "#sheriff-notifications", "text": "a test regressed"}
{
"channel": "#sheriff-notifications",
"text": "a test regressed",
"metadata": {
"event_type": "notification",
"source": {
"ref_id": "run-1",
"ref_url": f"{HACKBOT_UI_URL}/runs/run-1",
},
"event_payload": {
"context": {"agent": "test-agent", "run_id": "run-1"},
},
},
Comment on lines +55 to +64
}
]
assert result.status == "applied"
assert result.result == {"channel": "C1", "ts": "1700000000.000100"}
Expand Down