mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-12 11:11:10 +00:00
Slack: support Block Kit blocks in sendMessage actions
This commit is contained in:
@@ -137,9 +137,76 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Hello thread", {
|
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Hello thread", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: "1234567890.123456",
|
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 () => {
|
it("auto-injects threadTs from context when replyToMode=all", async () => {
|
||||||
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
|
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
|
||||||
sendSlackMessage.mockClear();
|
sendSlackMessage.mockClear();
|
||||||
@@ -159,6 +226,7 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Auto-threaded", {
|
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Auto-threaded", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: "1111111111.111111",
|
threadTs: "1111111111.111111",
|
||||||
|
blocks: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -182,6 +250,7 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "First", {
|
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "First", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: "1111111111.111111",
|
threadTs: "1111111111.111111",
|
||||||
|
blocks: undefined,
|
||||||
});
|
});
|
||||||
expect(hasRepliedRef.value).toBe(true);
|
expect(hasRepliedRef.value).toBe(true);
|
||||||
|
|
||||||
@@ -194,6 +263,7 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Second", {
|
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Second", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: undefined,
|
threadTs: undefined,
|
||||||
|
blocks: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -221,6 +291,7 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Explicit", {
|
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Explicit", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: "2222222222.222222",
|
threadTs: "2222222222.222222",
|
||||||
|
blocks: undefined,
|
||||||
});
|
});
|
||||||
expect(hasRepliedRef.value).toBe(true);
|
expect(hasRepliedRef.value).toBe(true);
|
||||||
|
|
||||||
@@ -232,6 +303,7 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Second", {
|
expect(sendSlackMessage).toHaveBeenLastCalledWith("channel:C123", "Second", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: undefined,
|
threadTs: undefined,
|
||||||
|
blocks: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -247,6 +319,7 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "No ref", {
|
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "No ref", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: undefined,
|
threadTs: undefined,
|
||||||
|
blocks: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -269,6 +342,7 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Off mode", {
|
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Off mode", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: undefined,
|
threadTs: undefined,
|
||||||
|
blocks: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -291,6 +365,7 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C999", "Different channel", {
|
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C999", "Different channel", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: undefined,
|
threadTs: undefined,
|
||||||
|
blocks: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -314,6 +389,7 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Explicit thread", {
|
expect(sendSlackMessage).toHaveBeenCalledWith("channel:C123", "Explicit thread", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: "2222222222.222222",
|
threadTs: "2222222222.222222",
|
||||||
|
blocks: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -336,6 +412,7 @@ describe("handleSlackAction", () => {
|
|||||||
expect(sendSlackMessage).toHaveBeenCalledWith("C123", "No prefix", {
|
expect(sendSlackMessage).toHaveBeenCalledWith("C123", "No prefix", {
|
||||||
mediaUrl: undefined,
|
mediaUrl: undefined,
|
||||||
threadTs: "1111111111.111111",
|
threadTs: "1111111111.111111",
|
||||||
|
blocks: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
|
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
|
||||||
|
import type { Block, KnownBlock } from "@slack/web-api";
|
||||||
import type { OpenClawConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
import { resolveSlackAccount } from "../../slack/accounts.js";
|
import { resolveSlackAccount } from "../../slack/accounts.js";
|
||||||
import {
|
import {
|
||||||
@@ -84,6 +85,27 @@ function resolveThreadTsFromContext(
|
|||||||
return undefined;
|
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(
|
export async function handleSlackAction(
|
||||||
params: Record<string, unknown>,
|
params: Record<string, unknown>,
|
||||||
cfg: OpenClawConfig,
|
cfg: OpenClawConfig,
|
||||||
@@ -174,17 +196,22 @@ export async function handleSlackAction(
|
|||||||
switch (action) {
|
switch (action) {
|
||||||
case "sendMessage": {
|
case "sendMessage": {
|
||||||
const to = readStringParam(params, "to", { required: true });
|
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 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(
|
const threadTs = resolveThreadTsFromContext(
|
||||||
readStringParam(params, "threadTs"),
|
readStringParam(params, "threadTs"),
|
||||||
to,
|
to,
|
||||||
context,
|
context,
|
||||||
);
|
);
|
||||||
const result = await sendSlackMessage(to, content, {
|
const result = await sendSlackMessage(to, content ?? "", {
|
||||||
...writeOpts,
|
...writeOpts,
|
||||||
mediaUrl: mediaUrl ?? undefined,
|
mediaUrl: mediaUrl ?? undefined,
|
||||||
threadTs: threadTs ?? undefined,
|
threadTs: threadTs ?? undefined,
|
||||||
|
blocks,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Keep "first" mode consistent even when the agent explicitly provided
|
// Keep "first" mode consistent even when the agent explicitly provided
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { WebClient } from "@slack/web-api";
|
import type { Block, KnownBlock, WebClient } from "@slack/web-api";
|
||||||
import { loadConfig } from "../config/config.js";
|
import { loadConfig } from "../config/config.js";
|
||||||
import { logVerbose } from "../globals.js";
|
import { logVerbose } from "../globals.js";
|
||||||
import { resolveSlackAccount } from "./accounts.js";
|
import { resolveSlackAccount } from "./accounts.js";
|
||||||
@@ -147,7 +147,11 @@ export async function listSlackReactions(
|
|||||||
export async function sendSlackMessage(
|
export async function sendSlackMessage(
|
||||||
to: string,
|
to: string,
|
||||||
content: string,
|
content: string,
|
||||||
opts: SlackActionClientOpts & { mediaUrl?: string; threadTs?: string } = {},
|
opts: SlackActionClientOpts & {
|
||||||
|
mediaUrl?: string;
|
||||||
|
threadTs?: string;
|
||||||
|
blocks?: (Block | KnownBlock)[];
|
||||||
|
} = {},
|
||||||
) {
|
) {
|
||||||
return await sendMessageSlack(to, content, {
|
return await sendMessageSlack(to, content, {
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
@@ -155,6 +159,7 @@ export async function sendSlackMessage(
|
|||||||
mediaUrl: opts.mediaUrl,
|
mediaUrl: opts.mediaUrl,
|
||||||
client: opts.client,
|
client: opts.client,
|
||||||
threadTs: opts.threadTs,
|
threadTs: opts.threadTs,
|
||||||
|
blocks: opts.blocks,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
import { type FilesUploadV2Arguments, type WebClient } from "@slack/web-api";
|
import {
|
||||||
|
type Block,
|
||||||
|
type FilesUploadV2Arguments,
|
||||||
|
type KnownBlock,
|
||||||
|
type WebClient,
|
||||||
|
} from "@slack/web-api";
|
||||||
import type { SlackTokenSource } from "./accounts.js";
|
import type { SlackTokenSource } from "./accounts.js";
|
||||||
import {
|
import {
|
||||||
chunkMarkdownTextWithMode,
|
chunkMarkdownTextWithMode,
|
||||||
@@ -41,6 +46,7 @@ type SlackSendOpts = {
|
|||||||
client?: WebClient;
|
client?: WebClient;
|
||||||
threadTs?: string;
|
threadTs?: string;
|
||||||
identity?: SlackSendIdentity;
|
identity?: SlackSendIdentity;
|
||||||
|
blocks?: (Block | KnownBlock)[];
|
||||||
};
|
};
|
||||||
|
|
||||||
function hasCustomIdentity(identity?: SlackSendIdentity): boolean {
|
function hasCustomIdentity(identity?: SlackSendIdentity): boolean {
|
||||||
@@ -79,11 +85,13 @@ async function postSlackMessageBestEffort(params: {
|
|||||||
text: string;
|
text: string;
|
||||||
threadTs?: string;
|
threadTs?: string;
|
||||||
identity?: SlackSendIdentity;
|
identity?: SlackSendIdentity;
|
||||||
|
blocks?: (Block | KnownBlock)[];
|
||||||
}) {
|
}) {
|
||||||
const basePayload = {
|
const basePayload = {
|
||||||
channel: params.channelId,
|
channel: params.channelId,
|
||||||
text: params.text,
|
text: params.text,
|
||||||
thread_ts: params.threadTs,
|
thread_ts: params.threadTs,
|
||||||
|
...(params.blocks?.length ? { blocks: params.blocks } : {}),
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
// Slack Web API types model icon_url and icon_emoji as mutually exclusive.
|
// Slack Web API types model icon_url and icon_emoji as mutually exclusive.
|
||||||
@@ -214,8 +222,9 @@ export async function sendMessageSlack(
|
|||||||
opts: SlackSendOpts = {},
|
opts: SlackSendOpts = {},
|
||||||
): Promise<SlackSendResult> {
|
): Promise<SlackSendResult> {
|
||||||
const trimmedMessage = message?.trim() ?? "";
|
const trimmedMessage = message?.trim() ?? "";
|
||||||
if (!trimmedMessage && !opts.mediaUrl) {
|
const blocks = opts.blocks?.length ? opts.blocks : undefined;
|
||||||
throw new Error("Slack send requires text or media");
|
if (!trimmedMessage && !opts.mediaUrl && !blocks) {
|
||||||
|
throw new Error("Slack send requires text, blocks, or media");
|
||||||
}
|
}
|
||||||
const cfg = loadConfig();
|
const cfg = loadConfig();
|
||||||
const account = resolveSlackAccount({
|
const account = resolveSlackAccount({
|
||||||
@@ -231,6 +240,23 @@ export async function sendMessageSlack(
|
|||||||
const client = opts.client ?? createSlackWebClient(token);
|
const client = opts.client ?? createSlackWebClient(token);
|
||||||
const recipient = parseRecipient(to);
|
const recipient = parseRecipient(to);
|
||||||
const { channelId } = await resolveChannelId(client, recipient);
|
const { channelId } = await resolveChannelId(client, recipient);
|
||||||
|
if (blocks) {
|
||||||
|
if (opts.mediaUrl) {
|
||||||
|
throw new Error("Slack send does not support blocks with mediaUrl");
|
||||||
|
}
|
||||||
|
const response = await postSlackMessageBestEffort({
|
||||||
|
client,
|
||||||
|
channelId,
|
||||||
|
text: trimmedMessage || " ",
|
||||||
|
threadTs: opts.threadTs,
|
||||||
|
identity: opts.identity,
|
||||||
|
blocks,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
messageId: response.ts ?? "unknown",
|
||||||
|
channelId,
|
||||||
|
};
|
||||||
|
}
|
||||||
const textLimit = resolveTextChunkLimit(cfg, "slack", account.accountId);
|
const textLimit = resolveTextChunkLimit(cfg, "slack", account.accountId);
|
||||||
const chunkLimit = Math.min(textLimit, SLACK_TEXT_LIMIT);
|
const chunkLimit = Math.min(textLimit, SLACK_TEXT_LIMIT);
|
||||||
const tableMode = resolveMarkdownTableMode({
|
const tableMode = resolveMarkdownTableMode({
|
||||||
|
|||||||
Reference in New Issue
Block a user