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.3 KB
Raw Blame
import { createTrigger, TriggerStrategy, Property, PiecePropValueSchema } from '@activepieces/pieces-framework';
import { teamworkAuth } from '../common/auth';
import { teamworkRequest } from '../common/client';
import { HttpMethod } from '@activepieces/pieces-common';

export const newMessage = createTrigger({
	name: 'new_message',
	classification: 'READ',
	displayName: 'New Message',
	description: 'Fires when a new message is posted (in project messages or discussions).',
	aiMetadata: {
		description: 'Fires when a message is posted in Teamwork project messages or discussions (MESSAGE.CREATED webhook), optionally filtered to a single project. Each event represents one newly posted message.',
	},
	auth: teamworkAuth,
	props: {
		projectId: Property.Dropdown({
auth: teamworkAuth,
			displayName: 'Project',
			description: 'The project to watch for new messages. If not specified, all projects will be watched.',
			required: false,
			refreshers: [],
			options: async ({ auth }) => {
				if (!auth) {
					return {
						disabled: true,
						placeholder: 'Please authenticate first.',
						options: [],
					};
				}
				const res = await teamworkRequest(auth, {
					method: HttpMethod.GET,
					path: '/projects.json',
				});
				const options = res.data.projects.map((p: { id: string; name: string }) => ({
					label: p.name,
					value: p.id,
				}));
				return {
					disabled: false,
					options,
				};
			},
		}),
	},
	type: TriggerStrategy.WEBHOOK,
	async onEnable(context) {
		const res = await teamworkRequest(context.auth, {
			method: HttpMethod.POST,
			path: '/webhooks.json',
			body: {
				webhook: {
					event: 'MESSAGE.CREATED',
					url: context.webhookUrl,
					version: 'v3',
				},
			},
		});
		await context.store.put('webhookId', res.data.id);
	},
	async onDisable(context) {
		const webhookId = await context.store.get('webhookId');
		if (webhookId) {
			await teamworkRequest(context.auth, {
				method: HttpMethod.DELETE,
				path: `/webhooks/${webhookId}.json`,
			});
		}
	},
	async run(context) {
		const payload = context.payload.body as any;
		if (context.propsValue.projectId) {
			if (String(payload?.message?.projectId) === context.propsValue.projectId) {
				return [payload];
			}
			return [];
		}
		return [payload];
	},
	sampleData: {},
});