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
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { DedupeStrategy, Polling, pollingHelper } from '@activepieces/pieces-common';
import { getHubspotAccessToken, HubspotAuthValue, hubspotAuth } from '../auth';
import {
createTrigger,
DropdownOption,
Property,
TriggerStrategy,
} from '@activepieces/pieces-framework';
import { Client } from '@hubspot/api-client';
import { MarkdownVariant } from '@activepieces/pieces-framework';
import { getDefaultPropertiesForObject, standardObjectPropertiesDropdown } from '../common/props';
import { OBJECT_TYPE } from '../common/constants';
import dayjs from 'dayjs';
import { newContactInListTriggerOutputSchema } from '../output-schemas';
import { AppConnectionValueForAuthProperty } from '@activepieces/pieces-framework';
type Props = {
listId: string;
additionalPropertiesToRetrieve?: string | string[];
};
const polling: Polling<AppConnectionValueForAuthProperty<typeof hubspotAuth>, Props> = {
strategy: DedupeStrategy.TIMEBASED,
async items({ auth, propsValue, lastFetchEpochMS }) {
const listId = propsValue.listId;
const additionalProperties = propsValue.additionalPropertiesToRetrieve ?? [];
const defaultContactProperties = getDefaultPropertiesForObject(OBJECT_TYPE.CONTACT);
const propertiesToRetrieve = [...defaultContactProperties, ...additionalProperties];
const client = new Client({ accessToken: getHubspotAccessToken(auth) });
const isTestMode = lastFetchEpochMS === 0;
let listMembers = [];
let after;
// Fetch members from the list
do {
const response = await client.crm.lists.membershipsApi.getPageOrderedByAddedToListDate(
listId,
after,
undefined,
isTestMode ? 10 : 100,
);
after = response.paging?.next?.after;
listMembers.push(...response.results);
if (isTestMode) {
break;
}
} while (after);
if (!isTestMode) {
listMembers = listMembers.filter(
(member) => dayjs(member.membershipTimestamp).valueOf() > lastFetchEpochMS,
);
}
// Fetch detailed contact properties in batches (HubSpot batch API limit is 100)
const BATCH_SIZE = 100;
const enrichedMembers = [];
for (let i = 0; i < listMembers.length; i += BATCH_SIZE) {
const chunk = listMembers.slice(i, i + BATCH_SIZE);
const contactDetailsResponse = await client.crm.contacts.batchApi.read({
inputs: chunk.map((member) => ({ id: member.recordId })),
properties: propertiesToRetrieve,
propertiesWithHistory: [],
});
// Merge `membershipTimestamp` with contact properties for this chunk
const enrichedChunk = contactDetailsResponse.results.map((contact) => {
const correspondingMember = chunk.find((member) => member.recordId === contact.id);
return {
...contact,
membershipTimestamp: correspondingMember?.membershipTimestamp,
};
});
enrichedMembers.push(...enrichedChunk);
}
return enrichedMembers.map((member) => ({
epochMilliSeconds: dayjs(member.membershipTimestamp).valueOf(),
data: member,
}));
},
};
export const newContactInListTrigger = createTrigger({
auth: hubspotAuth,
name: 'new-contact-in-list',
classification: 'READ',
displayName: 'New Contact in List',
description: 'Triggers when a new contact is added to the specified list.',
aiMetadata: {
description:
'Fires when a contact is added to the selected HubSpot contact list. Each event represents one contact whose membership was added since the last poll, enriched with the contact record properties (name, email, etc.) plus the timestamp it joined the list. Tracked by list-membership date.',
},
outputSchema: newContactInListTriggerOutputSchema,
type: TriggerStrategy.POLLING,
propertyGroups: [
{
key: 'list',
display: 'section',
label: 'List',
icon: 'users',
props: ['listId'],
},
],
props: {
listId: Property.Dropdown({
displayName: 'Contact List',
auth: hubspotAuth,
refreshers: [],
required: true,
options: async ({ auth }) => {
if (!auth) {
return {
disabled: true,
options: [],
placeholder: 'Please connect your account.',
};
}
const authValue = auth as HubspotAuthValue;
const client = new Client({ accessToken: getHubspotAccessToken(authValue) });
let offset = 0;
let hasMore = true;
const options: DropdownOption<string>[] = [];
do {
const response = await client.crm.lists.listsApi.doSearch({
count: 100,
offset: offset,
});
for (const list of response.lists) {
options.push({
label: list.name,
value: list.listId,
});
}
hasMore = response.hasMore;
offset += 100;
} while (hasMore);
return {
disabled: false,
options,
};
},
}),
markdown: Property.MarkDown({
variant: MarkdownVariant.INFO,
value: `Returned by default: firstname, lastname, email, company, website, mobilephone, phone, fax, address, city, state, zip, salutation, country, jobtitle, hs_createdate, hs_email_domain, hs_object_id, lastmodifieddate, hs_persona, hs_language, lifecyclestage, createdate, numemployees, annualrevenue, industry.
Pick more under **Advanced**.`,
}),
additionalPropertiesToRetrieve: standardObjectPropertiesDropdown({
objectType: OBJECT_TYPE.CONTACT,
displayName: 'Additional Properties to Retrieve',
required: false,
advanced: true,
}),
},
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: {
id: '123',
archived: false,
createdAt: '2023-06-13T10:24:42.392Z',
updatedAt: '2023-06-30T06:16:51.869Z',
properties: {
email: '[email protected]',
lastname: 'Last',
firstname: 'First',
createdate: '2023-06-13T10:24:42.392Z',
hs_object_id: '123',
lastmodifieddate: '2023-06-30T06:16:51.869Z',
},
},
});