Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.8 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { slackAuth } from '../auth';
import { assertNotNullOrUndefined } from '@activepieces/pieces-framework';
import { Block, KnownBlock } from '@slack/web-api';
import { slackSendMessage, textToSectionBlocks } from '../common/utils';
import { getBotToken, SlackAuthValue } from '../common/auth-helpers';
import { chatPostMessageOutputSchema } from '../output-schemas';

export const slackSendDirectMessageAiAction = createAction({
  auth: slackAuth,
  name: 'slack_send_direct_message',
  classification: 'WRITE',
  displayName: 'Send Direct Message',
  description: 'Send a direct message (DM) to a single Slack user.',
  audience: 'ai',
  aiMetadata: {
    description:
      'Open a DM with one user (by user ID, e.g. U0123ABCD) and post a message to them privately, optionally with Block Kit blocks. Resolve a handle or email to a user ID first with Find User by Email or Find User by Handle. Use Post Message to send to a channel instead. Each call posts a new DM, so it is not idempotent.',
    idempotent: false,
  },
  outputSchema: chatPostMessageOutputSchema,
  props: {
    userId: Property.ShortText({
      displayName: 'User ID',
      description:
        'The Slack user ID to DM (e.g. U0123ABCD). Resolve a handle/email to an ID first with Find User by Handle / Find User by Email.',
      required: true,
    }),
    text: Property.LongText({
      displayName: 'Message',
      description:
        'The text of the direct message. Renders as a section above any blocks, and is used as the notification fallback. Leave empty to send blocks only.',
      required: false,
    }),
    unfurlLinks: Property.Checkbox({
      displayName: 'Unfurl Links',
      description: 'Enable link unfurling for this message.',
      required: false,
      defaultValue: true,
    }),
    blocks: Property.Json({
      displayName: 'Block Kit Blocks',
      description:
        'Optional Block Kit blocks array. See https://api.slack.com/block-kit',
      required: false,
    }),
  },
  async run(context) {
    const token = getBotToken(context.auth as SlackAuthValue);
    const { text, userId, blocks, unfurlLinks } = context.propsValue;

    assertNotNullOrUndefined(token, 'token');
    assertNotNullOrUndefined(userId, 'userId');
    if (!text && (!blocks || !Array.isArray(blocks) || blocks.length === 0)) {
      throw new Error('Either Message or Block Kit blocks must be provided');
    }

    const blockList: (KnownBlock | Block)[] = [];
    if (text) {
      blockList.push(...textToSectionBlocks(text));
    }
    if (blocks && Array.isArray(blocks)) {
      blockList.push(...(blocks as unknown as (KnownBlock | Block)[]));
    }

    return slackSendMessage({
      token,
      text: text || undefined,
      conversationId: userId,
      blocks: blockList,
      unfurlLinks,
    });
  },
});