Skip to content
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
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,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))
Expand Down
12 changes: 0 additions & 12 deletions libs/hackbot-runtime/tests/test_slack_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down