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.9 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { synthesiaAuth } from '../common/auth';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';

interface WebhookData {
  webhookId: string;
}

export const newVideoReady = createTrigger({
  auth: synthesiaAuth,
  name: 'newVideoReady',
  displayName: 'New Video Ready',
  description: 'Trigger when a new video is completed in Synthesia',
  aiMetadata: {
    description:
      'Fires when a Synthesia video finishes rendering and becomes ready (the video.completed event), delivered via a registered webhook. Represents a single completed video; use it to act on a video once generation has finished.',
  },
  props: {},
  sampleData: {},
  type: TriggerStrategy.WEBHOOK,
  async onEnable(context) {
    const webhookUrl = context.webhookUrl;
    const response = await httpClient.sendRequest({
      method: HttpMethod.POST,
      url: 'https://api.synthesia.io/v2/webhooks',
      headers: {
        Authorization: `Bearer ${context.auth.secret_text}`,
        'Content-Type': 'application/json',
      },
      body: {
        url: webhookUrl,
        events: ['video.completed'],
      },
    });

    await context.store.put<WebhookData>('webhookData', {
      webhookId: response.body.id,
    });
  
  },
  async onDisable(context) {
    const webhookData = await context.store.get<WebhookData>('webhookData');
    if (webhookData) {
      await httpClient.sendRequest({
        method: HttpMethod.DELETE,
        url: `https://api.synthesia.io/v2/webhooks/${webhookData.webhookId}`,
        headers: {
          Authorization: `Bearer ${context.auth.secret_text}`,
          'Content-Type': 'application/json',
        },
      });
    }
    await context.store.delete('webhookData');
  },
  async run(context) {
    const payload = context.payload as any;
    if (payload.type !== 'video.completed') {
      return [];
    }
    return [context.payload.body];
  },
});