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

@@ -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 { logVerbose } from "../globals.js";
import { resolveSlackAccount } from "./accounts.js";
@@ -147,7 +147,11 @@ export async function listSlackReactions(
export async function sendSlackMessage(
to: string,
content: string,
opts: SlackActionClientOpts & { mediaUrl?: string; threadTs?: string } = {},
opts: SlackActionClientOpts & {
mediaUrl?: string;
threadTs?: string;
blocks?: (Block | KnownBlock)[];
} = {},
) {
return await sendMessageSlack(to, content, {
accountId: opts.accountId,
@@ -155,6 +159,7 @@ export async function sendSlackMessage(
mediaUrl: opts.mediaUrl,
client: opts.client,
threadTs: opts.threadTs,
blocks: opts.blocks,
});
}

View File

@@ -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 {
chunkMarkdownTextWithMode,
@@ -41,6 +46,7 @@ type SlackSendOpts = {
client?: WebClient;
threadTs?: string;
identity?: SlackSendIdentity;
blocks?: (Block | KnownBlock)[];
};
function hasCustomIdentity(identity?: SlackSendIdentity): boolean {
@@ -79,11 +85,13 @@ async function postSlackMessageBestEffort(params: {
text: string;
threadTs?: string;
identity?: SlackSendIdentity;
blocks?: (Block | KnownBlock)[];
}) {
const basePayload = {
channel: params.channelId,
text: params.text,
thread_ts: params.threadTs,
...(params.blocks?.length ? { blocks: params.blocks } : {}),
};
try {
// Slack Web API types model icon_url and icon_emoji as mutually exclusive.
@@ -214,8 +222,9 @@ export async function sendMessageSlack(
opts: SlackSendOpts = {},
): Promise<SlackSendResult> {
const trimmedMessage = message?.trim() ?? "";
if (!trimmedMessage && !opts.mediaUrl) {
throw new Error("Slack send requires text or media");
const blocks = opts.blocks?.length ? opts.blocks : undefined;
if (!trimmedMessage && !opts.mediaUrl && !blocks) {
throw new Error("Slack send requires text, blocks, or media");
}
const cfg = loadConfig();
const account = resolveSlackAccount({
@@ -231,6 +240,23 @@ export async function sendMessageSlack(
const client = opts.client ?? createSlackWebClient(token);
const recipient = parseRecipient(to);
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 chunkLimit = Math.min(textLimit, SLACK_TEXT_LIMIT);
const tableMode = resolveMarkdownTableMode({