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

1.8 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { heygenApiCall } from '../common/client';
import { heygenAuth } from '../common/auth';

const TRIGGER_KEY = 'video_generation_failed_trigger';

export const videoGenerationFailedTrigger = createTrigger({
	auth: heygenAuth,
	name: 'video_generation_failed',
	classification: 'READ',
	displayName: 'New Avatar Video Event (Fail)',
	description: 'Triggers when a video generation process fails.',
	aiMetadata: {
		description: 'Fires when an avatar video generation fails (HeyGen avatar_video.fail webhook event), providing the affected video ID and an error message so the failure can be handled.',
	},
	type: TriggerStrategy.WEBHOOK,
	props: {},

	sampleData: {
		event_type: 'avatar_video.fail',
		event_data: {
			video_id: 'abc',
			msg: 'Failed',
			callback_id: '123',
		},
	},

	async onEnable(context) {
		const webhook = (await heygenApiCall({
			apiKey: context.auth.secret_text,
			method: HttpMethod.POST,
			resourceUri: '/webhook/endpoint.add',
			apiVersion: 'v1',
			body: {
				url: context.webhookUrl,
				events: ['avatar_video.fail'],
			},
		})) as { data: { endpoint_id: string } };

		await context.store.put<string>(TRIGGER_KEY, webhook.data.endpoint_id);
	},

	async onDisable(context) {
		const webhookId = await context.store.get<string>(TRIGGER_KEY);

		if (webhookId) {
			await heygenApiCall({
				apiKey: context.auth.secret_text,
				method: HttpMethod.DELETE,
				resourceUri: '/webhook/endpoint.delete',
				apiVersion: 'v1',
				query: {
					endpoint_id: webhookId,
				},
			});
		}
	},

	async run(context) {
		const payload = context.payload.body as {
			event_type: string;
			event_data: Record<string, any>;
		};

		if (payload.event_type !== 'avatar_video.fail') return [];

		return [payload];
	},
});