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

10.3 KB
Raw Blame
import { MarkdownVariant, Property } from '@activepieces/pieces-framework';
import { ConversationsListResponse, UsersConversationsResponse, UsersListResponse, WebClient } from '@slack/web-api';
import { slackAuth } from '../auth';
import { getBotToken, SlackAuthValue } from '../common/auth-helpers';
export const multiSelectChannelInfo = Property.MarkDown({
  value:
    "The list shows the first 2000 channels. Can't find yours? Invite the bot: type **/invite** in the channel, choose **Add apps** and pick the bot. Or click **ƒ** and paste IDs as `{`{ ['C012AB3CD', 'C045EF6GH'] `}`}.",
  variant: MarkdownVariant.INFO,
});

export const singleSelectChannelInfo = Property.MarkDown({
  value:
    "The list shows the first 2000 channels. Can't find yours? Invite the bot: type **/invite** in the channel, choose **Add apps** and pick the bot. Or click **ƒ** and paste the channel ID.",
  variant: MarkdownVariant.INFO,
});

export const appWebhookSetupInfo = Property.MarkDown({
  value:
    "This trigger needs Slack's App Webhook manually configured on self-hosted instances before it will receive events, see the [setup guide](https://www.activepieces.com/docs/install/configure-operate/setup-app-webhooks#slack).",
  variant: MarkdownVariant.INFO,
});

export const interactivitySetupInfo = Property.MarkDown({
  value:
    "This trigger needs Slack's Interactivity & Shortcuts Request URL manually configured on self-hosted instances before it will receive events, see the [setup guide](https://www.activepieces.com/docs/install/configure-operate/setup-app-webhooks#slack).",
  variant: MarkdownVariant.INFO,
});

export const slackChannel = <R extends boolean>(required: R) =>
  Property.Dropdown<string, R, typeof slackAuth>({
    auth: slackAuth,
    displayName: 'Channel',
    description: 'Private channels appear only after the bot is added to them.',
    required,
    refreshers: ['onlyBotChannels'],
    async options({ auth, onlyBotChannels }) {
      if (!auth) {
        return {
          disabled: true,
          placeholder: 'connect slack account',
          options: [],
        };
      }
      const accessToken = getBotToken(auth as SlackAuthValue);

      const channels = await getChannels({ accessToken, onlyBotChannels: onlyBotChannels === true });

      return {
        disabled: false,
        placeholder: 'Select channel',
        options: channels,
      };
    },
  });

export const onlyBotChannels = Property.Checkbox({
  displayName: 'Only Channels the Bot Is In',
  description: 'Show only channels the bot is a member of.',
  required: false,
  defaultValue: true,
});

export const slackChannels = Property.MultiSelectDropdown<string, false, typeof slackAuth>({
  auth: slackAuth,
  displayName: 'Channels',
  description: 'Empty means every channel the bot is in.',
  required: false,
  refreshers: ['onlyBotChannels'],
  async options({ auth, onlyBotChannels }) {
    if (!auth) {
      return {
        disabled: true,
        placeholder: 'connect slack account',
        options: [],
      };
    }
    const accessToken = getBotToken(auth as SlackAuthValue);

    const channels = await getChannels({ accessToken, onlyBotChannels: onlyBotChannels === true });

    return {
      disabled: false,
      placeholder: 'Select channels',
      options: channels,
    };
  },
});

export const username = Property.ShortText({
  displayName: 'Username',
  description: "Overrides the bot's display name for this message.",
  required: false,
  advanced: true,
});

export const profilePicture = Property.ShortText({
  displayName: 'Profile Picture',
  description: "Image URL used as the sender's avatar.",
  placeholder: 'https://example.com/avatar.png',
  required: false,
  advanced: true,
});

export const iconEmoji = Property.ShortText({
  displayName: 'Icon Emoji',
  description: "Emoji used as the sender's avatar.",
  placeholder: ':robot_face:',
  required: false,
  advanced: true,
});

export const threadTs = Property.ShortText({
  displayName: 'Reply to Thread',
  description: 'Timestamp or link of the parent message to reply under.',
  placeholder: '1710304378.475129',
  required: false,
});

export const mentionOriginFlow = Property.Checkbox({
  displayName: 'Mention Origin Flow',
  description: 'Append a link to this flow at the end of the message.',
  required: false,
  defaultValue: false,
  advanced: true,
});

export const blocks = Property.Json({
  displayName: 'Block Kit Blocks',
  description: 'JSON array of blocks from the Block Kit Builder.',
  required: false,
  defaultValue: [],
});

export const messageTs = Property.ShortText({
  displayName: 'Message Timestamp',
  description: 'Timestamp of the target message, from its link or a trigger output.',
  placeholder: '1710304378.475129',
  required: true,
});

export const replyBroadcast = Property.Checkbox({
  displayName: 'Also Post to Channel',
  description: 'When replying in a thread, also show the reply in the channel.',
  required: false,
  defaultValue: false,
  advanced: true,
});

export const unfurlLinks = Property.Checkbox({
  displayName: 'Unfurl Links',
  description: 'Show link previews in the message.',
  required: false,
  defaultValue: true,
  advanced: true,
});

export const userId = <R extends boolean>(required: R) =>
  Property.Dropdown<string, R, typeof slackAuth>({
    auth: slackAuth,
    displayName: 'User',
    description: 'Search by name or handle.',
    required,
    refreshers: [],
    async options({ auth }) {
      if (!auth) {
        return {
          disabled: true,
          placeholder: 'connect slack account',
          options: [],
        };
      }
      const accessToken = getBotToken(auth as SlackAuthValue);
      const users = await getUsers(accessToken);
      return {
        disabled: false,
        placeholder: 'Select User',
        options: users,
      };
    },
  });

export const userIds = Property.MultiSelectDropdown({
  auth: slackAuth,
  displayName: 'Users',
  description: 'Pick one or more members.',
  required: false,
  refreshers: [],
  async options({ auth }) {
    if (!auth) {
      return {
        disabled: true,
        placeholder: 'connect slack account',
        options: [],
      };
    }
    const accessToken = getBotToken(auth as SlackAuthValue);
    const users = await getUsers(accessToken);
    return {
      disabled: false,
      placeholder: 'Select Users',
      options: users,
    };
  },
});

export const usergroupIds = Property.MultiSelectDropdown({
  auth: slackAuth,
  displayName: 'User Groups',
  description: 'Pick one or more user groups.',
  required: false,
  refreshers: [],
  async options({ auth }) {
    if (!auth) {
      return {
        disabled: true,
        placeholder: 'connect slack account',
        options: [],
      };
    }
    const accessToken = getBotToken(auth as SlackAuthValue);
    const client = new WebClient(accessToken);
    const response = await client.usergroups.list();
    const usergroups = (response.usergroups ?? [])
      .filter((ug) => !ug.date_delete)
      .map((ug) => ({
        label: ug.handle || ug.name || '',
        value: ug.id || '',
      }));
    return {
      disabled: false,
      placeholder: 'Select User Groups',
      options: usergroups,
    };
  },
});

export const text = Property.LongText({
  displayName: 'Message',
  description: 'Slack mrkdwn formatting is supported.',
  required: true,
});

export const actions = Property.Array({
  displayName: 'Action Buttons',
  description: 'Each button becomes a choice the recipient can click.',
  required: true,
  properties: {
    label: Property.ShortText({
      displayName: 'Label',
      description: 'Text shown on the button.',
      placeholder: 'Approve',
      required: true,
    }),
    style: Property.StaticDropdown({
      displayName: 'Style',
      description: 'Primary is green, Danger is red.',
      required: false,
      defaultValue: null,
      options: {
        options: [
          { label: 'Default', value: null },
          { label: 'Primary', value: 'primary' },
          { label: 'Danger', value: 'danger' },
        ],
      },
    }),
  },
});

export async function getUsers(accessToken: string) {
  const client = new WebClient(accessToken);
  const users: { label: string; value: string }[] = [];
  for await (const page of client.paginate('users.list', {
    limit: 1000,
  })) {
    const response = page as UsersListResponse;
    if (response.members) {
      users.push(
        ...response.members
          .filter((member) => !member.deleted)
          .map((member) => {
            const handle = member.name ?? '';
            const realName = member.real_name ?? member.profile?.real_name;
            return {
              label: realName ? `${realName} (@${handle})` : `@${handle}`,
              value: member.id ?? '',
            };
          })
      );
    }
  }
  return users;
}

export async function getChannels({
  accessToken,
  onlyBotChannels,
}: {
  accessToken: string;
  onlyBotChannels?: boolean;
}) {
  const client = new WebClient(accessToken);
  const channels: { label: string; value: string }[] = [];
  const CHANNELS_LIMIT = 2000;

  let cursor: string | undefined;
  do {
    const params = {
      types: 'public_channel,private_channel',
      exclude_archived: true,
      limit: 1000,
      cursor,
    };
    const response: ConversationsListResponse | UsersConversationsResponse = onlyBotChannels
      ? await client.users.conversations(params)
      : await client.conversations.list(params);

    if (response.channels) {
      channels.push(
        ...response.channels.map((channel) => {
          return { label: channel.name || '', value: channel.id || '' };
        })
      );
    }

    cursor = response.response_metadata?.next_cursor;
  } while (cursor && channels.length < CHANNELS_LIMIT);

  return channels;
}

export const threadCursor = Property.ShortText({
  displayName: 'Continue From Cursor',
  description: 'Next Cursor from a previous run whose Has More was true.',
  required: false,
  advanced: true,
});

export const customBlocks = Property.Json({
  displayName: 'Blocks',
  description:
    'Optional Block Kit blocks rendered above the action buttons, e.g. dropdowns. Give each interactive element a block_id and action_id so its selection appears under payload.state.values when an action is clicked. Do not use the block_id "actions", it is reserved for the action buttons. See https://api.slack.com/block-kit',
  required: false,
  defaultValue: [],
});