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.6 KB
Raw Blame
import { slackAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';
import { singleSelectChannelInfo, slackChannel, onlyBotChannels, messageTs } from '../common/props';

import { WebClient } from '@slack/web-api';
import { processMessageTimestamp } from '../common/utils';
import {
  getBotToken,
  getUserToken,
  requireUserToken,
  SlackAuthValue,
} from '../common/auth-helpers';

export const addRectionToMessageAction = createAction({
  auth: slackAuth,
  name: 'slack-add-reaction-to-message',
  classification: 'WRITE',
  displayName: 'Add Reaction to Message',
  description: 'Add an emoji reaction to a message.',
  audience: 'human',
  aiMetadata: { description: 'Add an emoji reaction to a specific message identified by its channel and timestamp, optionally reacting as the authenticated user instead of the bot. Adding the same reaction twice has no extra effect (Slack returns an already-reacted error), so the end state is stable. Provide the emoji name without colons, e.g. thumbsup.', idempotent: true },

  propertyGroups: [
    {
      key: 'channel_picker',
      display: 'section',
      label: 'Channel',
      icon: 'markdown',
      props: ['info', 'onlyBotChannels', 'channel'],
    },
    {
      key: 'message',
      display: 'section',
      label: 'Message',
      icon: 'text',
      props: ['ts'],
    },
    {
      key: 'reaction_emoji',
      display: 'section',
      label: 'Reaction',
      icon: 'tag',
      props: ['reaction', 'reactAsUser'],
    },
  ],
  props: {
    info: singleSelectChannelInfo,
    onlyBotChannels,
    channel: slackChannel(true),
    ts: messageTs,
    reaction: Property.ShortText({
      displayName: 'Emoji',
      description: 'Emoji name without colons.',
      placeholder: 'thumbsup',
      required: true,
    }),
    reactAsUser: Property.Checkbox({
      displayName: 'React as User',
      description:
        'Add the reaction as the connected user instead of the bot.',
      required: false,
      defaultValue: false,
    }),
  },

  async run(context) {
    const { channel, ts, reaction, reactAsUser } = context.propsValue;

    const token = reactAsUser
      ? requireUserToken(context.auth as SlackAuthValue)
      : getBotToken(context.auth as SlackAuthValue);

    const slack = new WebClient(token);

    const messageTimestamp = processMessageTimestamp(ts);

    if (messageTimestamp) {
      const response = await slack.reactions.add({
        channel,
        timestamp: messageTimestamp,
        name: reaction,
      });

      return response;
    } else {
      throw new Error('Invalid Timestamp Value.');
    }
  },
});