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.5 KB
Raw Blame
import {
	AuthenticationType,
	DedupeStrategy,
	httpClient,
	HttpMethod,
	Polling,
	pollingHelper,
	QueryParams,
} from '@activepieces/pieces-common';
import { getHubspotAccessToken, hubspotAuth } from '../auth';
import {
	createTrigger,
	Property,
	TriggerStrategy,
} from '@activepieces/pieces-framework';
import { AppConnectionValueForAuthProperty } from '@activepieces/pieces-framework';

type Props = {
	eventType?: string;
};

type EngagementResponse = {
	results: Array<Record<string, any>>;
	hasMore: boolean;
	offset: string;
};

const polling: Polling<AppConnectionValueForAuthProperty<typeof hubspotAuth>, Props> = {
	strategy: DedupeStrategy.TIMEBASED,
	async items({ auth, propsValue, lastFetchEpochMS }) {
		const eventType = propsValue.eventType;
		const engagements = [];

		let hasMore = true;
		const qs: QueryParams = { limit: '100' };
		do {
			const response = await httpClient.sendRequest<EngagementResponse>({
				method: HttpMethod.GET,
				url: 'https://api.hubapi.com/engagements/v1/engagements/paged',
				queryParams: qs,
				authentication: {
					type: AuthenticationType.BEARER_TOKEN,
					token: getHubspotAccessToken(auth),
				},
			});
			hasMore = response.body.hasMore;
			qs.offset = response.body.offset;
			for (const engagement of response.body.results) {
				engagements.push(engagement);
			}
		} while (hasMore);

		const filteredEngagements = eventType
			? engagements.filter((engagement) => engagement.engagement.type === eventType)
			: engagements;

		return filteredEngagements.map((item) => ({
			epochMilliSeconds: item.engagement.createdAt as number,
			data: item,
		}));
	},
};

export const newEngagementTrigger = createTrigger({
	auth: hubspotAuth,
	name: 'new-engagement',
	classification: 'READ',
	displayName: 'New Engagement',
	description: 'Triggers when a new engagement is created.',
	aiMetadata: {
		description:
			'Fires when a new engagement (interaction) is created in HubSpot, optionally filtered to a single type: Note, Task, Meeting, Email, or Call. Polls the Engagements API and emits each engagement with its metadata and associations to contacts, companies, and deals. Represents a logged activity or touchpoint on a CRM record.',
	},
	type: TriggerStrategy.POLLING,
	props: {
		eventType: Property.StaticDropdown({
			displayName: 'Engagement Type',
			description: 'Leave empty to trigger on every engagement type.',
			required: false,
			options: {
				disabled: false,
				options: [
					{
						label: 'Note',
						value: 'NOTE',
					},
					{
						label: 'Task',
						value: 'TASK',
					},
					{
						label: 'Meeting',
						value: 'MEETING',
					},
					{
						label: 'Email',
						value: 'EMAIL',
					},
					{
						label: 'Call',
						value: 'CALL',
					},
				],
			},
		}),
	},
	async onEnable(context) {
		await pollingHelper.onEnable(polling, context);
	},
	async onDisable(context) {
		await pollingHelper.onDisable(polling, context);
	},
	async test(context) {
		return await pollingHelper.test(polling, context);
	},
	async run(context) {
		return await pollingHelper.poll(polling, context);
	},
	sampleData: {
		engagement: {
			id: 29090716,
			portalId: 62515,
			active: true,
			createdAt: 1444223400781,
			lastUpdated: 1444223400781,
			createdBy: 215482,
			modifiedBy: 215482,
			ownerId: 70,
			type: 'NOTE',
			timestamp: 1444223400781,
		},
		associations: {
			contactIds: [247],
			companyIds: [],
			dealIds: [],
			ownerIds: [],
			workflowIds: [],
		},
		attachments: [],
		metadata: {
			body: 'This is a test note ',
		},
	},
});