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

3.0 KB
Raw Blame
import {
  Property,
  TriggerStrategy,
  createTrigger,
} from '@activepieces/pieces-framework';
import { slackAuth } from '../auth';
import { appWebhookSetupInfo, userId } from '../common/props';
import { getTeamId, getUserId, SlackAuthValue } from '../common/auth-helpers';
import { newMentionInDirectMessageTriggerOutputSchema } from '../output-schemas';

export const newMentionInDirectMessageTrigger = createTrigger({
  auth: slackAuth,
  name: 'new-mention-in-direct-message',
  classification: 'READ',
  displayName: 'New Mention in Direct Message',
  description:
    'Triggers when a username is mentioned in a direct message channel.',
  aiMetadata: {
    description:
      'Fires when the configured user is @-mentioned in a direct message (im channel). Only messages in DM channels that contain the mention token for the selected user fire; bot messages and the user\'s own messages can be optionally ignored. The event payload is the Slack message event including its text, channel, and sender.',
  },
  propertyGroups: [
    {
      key: 'user_picker',
      display: 'section',
      label: 'User',
      icon: 'user',
      props: ['info', 'user'],
    },
    {
      key: 'filters',
      display: 'section',
      label: 'Filters',
      icon: 'filter',
      props: ['ignoreBots', 'ignoreSelfMessages'],
    },
  ],
  props: {
    info: appWebhookSetupInfo,
    user: userId(true),
    ignoreBots: Property.Checkbox({
      displayName: 'Ignore Bot Messages',
      description: 'Skip messages posted by bots and apps.',
      required: false,
      defaultValue: false,
    }),
    ignoreSelfMessages: Property.Checkbox({
      displayName: 'Ignore My Own Messages',
      description: 'Skip messages sent by the connected user. Needs a user token.',
      required: false,
      defaultValue: false,
    }),
  },
  type: TriggerStrategy.APP_WEBHOOK,
  sampleData: undefined,
  outputSchema: newMentionInDirectMessageTriggerOutputSchema,
  onEnable: async (context) => {
    // Older OAuth2 has team_id, newer has team.id
  		const teamId = await getTeamId(context.auth as SlackAuthValue);

    context.app.createListeners({
      events: ['message'],
      identifierValue: teamId,
    });
  },
  onDisable: async (context) => {
    // Ignored
  },

  async run(context) {
    const payloadBody = context.payload.body as PayloadBody;
    const userId = await getUserId(context.auth as SlackAuthValue)

    if (payloadBody.event.channel_type !== 'im') {
      return [];
    }

    // check for bot messages
    if (
      (context.propsValue.ignoreBots && payloadBody.event.bot_id) ||
      (context.propsValue.ignoreSelfMessages &&
        payloadBody.event.user === userId)
    ) {
      return [];
    }

    //check for mention
    if (
      context.propsValue.user &&
      payloadBody.event.text?.includes(`<@${context.propsValue.user}>`)
    ) {
      return [payloadBody.event];
    }

    return [];
  },
});

type PayloadBody = {
  event: {
    channel: string;
    bot_id?: string;
    user: string;
    channel_type: string;
    text?: string;
  };
};