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.6 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 dayjs from 'dayjs';
import { newBlogArticleTriggerOutputSchema } from '../output-schemas';
import { AppConnectionValueForAuthProperty } from '@activepieces/pieces-framework';

type Props = {
	articleState: string;
};

type ListBlogPostsResponse = {
	results: Array<Record<string, any>>;
	paging?: {
		next?: {
			after: string;
		};
	};
};

const polling: Polling<AppConnectionValueForAuthProperty<typeof hubspotAuth>, Props> = {
	strategy: DedupeStrategy.TIMEBASED,
	async items({ auth, propsValue, lastFetchEpochMS }) {
		const articleState = propsValue.articleState;
		const isTestMode = lastFetchEpochMS === 0;

		const qs: QueryParams = { limit: '100', sort: '-createdAt' };
		if (articleState !== 'BOTH') {
			qs.state = articleState;
		}
		if (!isTestMode) {
			if(articleState === 'PUBLISHED') {
                qs['publishDate__gt']=lastFetchEpochMS.toString();
            }
            else
            {
                qs['createdAt__gt']=lastFetchEpochMS.toString();
            }
		}

		const items = [];

		let after;

		do {
			const response = await httpClient.sendRequest<ListBlogPostsResponse>({
				method: HttpMethod.GET,
				url: 'https://api.hubapi.com/cms/v3/blogs/posts',
				queryParams: qs,
				authentication: { type: AuthenticationType.BEARER_TOKEN, token: getHubspotAccessToken(auth) },
			});

			after = response.body.paging?.next?.after;
			if(response.body.paging?.next?.after){
				qs.after = response.body.paging?.next?.after;
			}
			items.push(...response.body.results);
			if (isTestMode) {
				break;
			}
		} while (after);

		return items.map((item) => {
			return {
				epochMilliSeconds: articleState === 'PUBLISHED' ? dayjs(item.publishDate).valueOf() : dayjs(item.createdAt).valueOf(),
				data: item,
			}
		});
	},
};

export const newBlogArticleTrigger = createTrigger({
	auth: hubspotAuth,
	name: 'new-blog-article',
	classification: 'READ',
	displayName: 'New Blog Post',
	description: 'Triggers when a post is added to your HubSpot blog.',
	aiMetadata: {
		description:
			'Fires when a blog post is added to your HubSpot COS (CMS) blog. The configured article state (Published only, Draft only, or Both) determines which posts qualify; published posts are tracked by publish date and drafts by creation date. Each event represents one blog post with its full CMS metadata.',
	},
	outputSchema: newBlogArticleTriggerOutputSchema,
	type: TriggerStrategy.POLLING,
	props: {
		articleState: Property.StaticDropdown({
			displayName: 'Post Status',
			required: true,
			display: 'cards',
			options: {
				disabled: false,
				options: [
					{
						label: 'Published Only',
						value: 'PUBLISHED',
						description: 'Live posts only',
						icon: 'send',
					},
					{
						label: 'Draft Only',
						value: 'DRAFT',
						description: 'Unpublished only',
						icon: 'file',
					},
					{
						label: 'Both',
						value: 'BOTH',
						description: 'Drafts and live',
						icon: 'inbox',
					},
				],
			},
		}),
	},
	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: {},
});