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.4 KB
Raw Blame
import { Property, TriggerStrategy, createTrigger } from '@activepieces/pieces-framework';
import { appWebhookSetupInfo, singleSelectChannelInfo, slackChannel, onlyBotChannels } from '../common/props';
import { slackAuth } from '../auth';
import { getTeamId, SlackAuthValue } from '../common/auth-helpers';
import { newMessageInChannelTriggerOutputSchema } from '../output-schemas';



export const newMessageInChannelTrigger = createTrigger({
	auth: slackAuth,
	name: 'new-message-in-channel',
	classification: 'READ',
	displayName: 'New Message Posted to Channel',
	description: 'Triggers when a new message is posted to a specific #channel you choose.',
	aiMetadata: {
		description:
			'Fires when a new message is posted to the specific channel selected in the trigger configuration. Only channel or group messages in the chosen channel fire; bot messages can be optionally ignored. The event payload is the Slack message event, including its channel, text, and sender.',
	},
	propertyGroups: [
		{
			key: 'channel_picker',
			display: 'section',
			label: 'Channel',
			icon: 'markdown',
			props: ['webhookInfo', 'info', 'onlyBotChannels', 'channel'],
		},
		{
			key: 'filters',
			display: 'section',
			label: 'Filters',
			icon: 'filter',
			props: ['ignoreBots'],
		},
	],
	props: {
		webhookInfo: appWebhookSetupInfo,
		info: singleSelectChannelInfo,
		onlyBotChannels,
		channel: slackChannel(true),
		ignoreBots: Property.Checkbox({
			displayName: 'Ignore Bot Messages',
			description: 'Skip messages posted by bots and apps.',
			required: false,
			defaultValue: false,
		}),
	},
	type: TriggerStrategy.APP_WEBHOOK,
	sampleData: undefined,
	outputSchema: newMessageInChannelTriggerOutputSchema,
	async onEnable(context) {
		const teamId = await getTeamId(context.auth as SlackAuthValue);
		context.app.createListeners({
			events: ['message'],
			identifierValue: teamId,
		});
	},
	async onDisable(context) {
		// Ignored
	},

	async run(context) {
		const payloadBody = context.payload.body as PayloadBody;

		// check if it's channel message
		if (!['channel','group'].includes(payloadBody.event.channel_type)) {
			return [];
		}
		
		if (payloadBody.event.channel === context.propsValue.channel) {
			// check for bot messages
			if (context.propsValue.ignoreBots && payloadBody.event.bot_id) {
				return [];
			}
			return [payloadBody.event];
		}

		return [];
	},
});

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