Slack: support Block Kit blocks in sendMessage actions

This commit is contained in:
Colin
2026-02-16 12:04:18 -05:00
committed by Peter Steinberger
parent bd17587b2a
commit c9684a2678
4 changed files with 142 additions and 7 deletions

View File

@@ -137,9 +137,76 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Hello thread", {
mediaUrl: undefined,
threadTs: "1234567890.123456",
blocks: undefined,
});
});
it("accepts blocks JSON and allows empty content", async () => {
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
sendSlackMessage.mockClear();
await handleSlackAction(
{
action: "sendMessage",
to: "channel:C123",
blocks: JSON.stringify([
{ type: "section", text: { type: "mrkdwn", text: "*Deploy* status" } },
]),
},
cfg,
);
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "", {
mediaUrl: undefined,
threadTs: undefined,
blocks: [{ type: "section", text: { type: "mrkdwn", text: "*Deploy* status" } }],
});
});
it("accepts blocks arrays directly", async () => {
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
sendSlackMessage.mockClear();
await handleSlackAction(
{
action: "sendMessage",
to: "channel:C123",
blocks: [{ type: "divider" }],
},
cfg,
);
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "", {
mediaUrl: undefined,
threadTs: undefined,
blocks: [{ type: "divider" }],
});
});
it("rejects invalid blocks JSON", async () => {
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
await expect(
handleSlackAction(
{
action: "sendMessage",
to: "channel:C123",
blocks: "{bad-json",
},
cfg,
),
).rejects.toThrow(/blocks must be valid JSON/i);
});
it("requires at least one of content, blocks, or mediaUrl", async () => {
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
await expect(
handleSlackAction(
{
action: "sendMessage",
to: "channel:C123",
content: "",
},
cfg,
),
).rejects.toThrow(/requires content, blocks, or mediaUrl/i);
});
it("auto-injects threadTs from context when replyToMode=all", async () => {
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
sendSlackMessage.mockClear();
@@ -159,6 +226,7 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Auto-threaded", {
mediaUrl: undefined,
threadTs: "1111111111.111111",
blocks: undefined,
});
});
@@ -182,6 +250,7 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "First", {
mediaUrl: undefined,
threadTs: "1111111111.111111",
blocks: undefined,
});
expect(hasRepliedRef.value).toBe(true);
@@ -194,6 +263,7 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Second", {
mediaUrl: undefined,
threadTs: undefined,
blocks: undefined,
});
});
@@ -221,6 +291,7 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Explicit", {
mediaUrl: undefined,
threadTs: "2222222222.222222",
blocks: undefined,
});
expect(hasRepliedRef.value).toBe(true);
@@ -232,6 +303,7 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Second", {
mediaUrl: undefined,
threadTs: undefined,
blocks: undefined,
});
});
@@ -247,6 +319,7 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "No ref", {
mediaUrl: undefined,
threadTs: undefined,
blocks: undefined,
});
});
@@ -269,6 +342,7 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Off mode", {
mediaUrl: undefined,
threadTs: undefined,
blocks: undefined,
});
});
@@ -291,6 +365,7 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C999", "Different channel", {
mediaUrl: undefined,
threadTs: undefined,
blocks: undefined,
});
});
@@ -314,6 +389,7 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Explicit thread", {
mediaUrl: undefined,
threadTs: "2222222222.222222",
blocks: undefined,
});
});
@@ -336,6 +412,7 @@ describe("handleSlackAction", () => {
expect(sendSlackMessage).toHaveBeenCalledWith("C123", "No prefix", {
mediaUrl: undefined,
threadTs: "1111111111.111111",
blocks: undefined,
});
});

View File

@@ -1,4 +1,5 @@
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
import type { Block, KnownBlock } from "@slack/web-api";
import type { OpenClawConfig } from "../../config/config.js";
import { resolveSlackAccount } from "../../slack/accounts.js";
import {
@@ -84,6 +85,27 @@ function resolveThreadTsFromContext(
return undefined;
}
function readSlackBlocksParam(params: Record<string, unknown>) {
const raw = params.blocks;
if (raw == null) {
return undefined;
}
const parsed =
typeof raw === "string"
? (() => {
try {
return JSON.parse(raw);
} catch {
throw new Error("blocks must be valid JSON");
}
})()
: raw;
if (!Array.isArray(parsed)) {
throw new Error("blocks must be an array");
}
return parsed as (Block | KnownBlock)[];
}
export async function handleSlackAction(
params: Record<string, unknown>,
cfg: OpenClawConfig,
@@ -174,17 +196,22 @@ export async function handleSlackAction(
switch (action) {
case "sendMessage": {
const to = readStringParam(params, "to", { required: true });
const content = readStringParam(params, "content", { required: true });
const content = readStringParam(params, "content", { allowEmpty: true });
const mediaUrl = readStringParam(params, "mediaUrl");
const blocks = readSlackBlocksParam(params);
if (!content && !mediaUrl && !blocks) {
throw new Error("Slack sendMessage requires content, blocks, or mediaUrl.");
}
const threadTs = resolveThreadTsFromContext(
readStringParam(params, "threadTs"),
to,
context,
);
const result = await sendSlackMessage(to, content, {
const result = await sendSlackMessage(to, content ?? "", {
...writeOpts,
mediaUrl: mediaUrl ?? undefined,
threadTs: threadTs ?? undefined,
blocks,
});
// Keep "first" mode consistent even when the agent explicitly provided