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

2.1 KB
Raw Blame
import { TriggerStrategy, createTrigger } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { qwilrAuth } from '../../lib/common/auth';

const subscribeWebhook = async (auth: string, event: string, targetUrl: string) => {
  const response = await httpClient.sendRequest({
    method: HttpMethod.POST,
    url: 'https://api.qwilr.com/v1/webhooks',
    headers: {
      'Authorization': `Bearer ${auth}`,
      'Content-Type': 'application/json'
    },
    body: {
      event,
      targetUrl
    }
  });
  return response.body;
};

const unsubscribeWebhook = async (auth: string, subscriptionId: string) => {
  await httpClient.sendRequest({
    method: HttpMethod.DELETE,
    url: `https://api.qwilr.com/v1/webhooks/${subscriptionId}`,
    headers: {
      'Authorization': `Bearer ${auth}`
    }
  });
};

export const pageRevivedLiveTrigger = createTrigger({
  auth: qwilrAuth,
  name: 'page_revived_live',
  displayName: 'Page Revived Live',
  description: 'Triggers when a Qwilr page is revived to live status',
  aiMetadata: {
    description: 'Fires when a previously disabled or expired Qwilr page is revived back to live status, making it accessible again. Use to react when an old proposal is reactivated for a recipient.',
  },
  type: TriggerStrategy.WEBHOOK,
  props: {},
  async onEnable(context) {
    const webhookData = await subscribeWebhook(
      context.auth.secret_text,
      'pageRevivedLive',
      context.webhookUrl
    );

    await context.store?.put('_page_revived_live_trigger', {
      subscriptionId: webhookData.id,
    });
  },
  async onDisable(context) {
    const response: { subscriptionId: string } | null = await context.store?.get('_page_revived_live_trigger');

    if (response !== null && response !== undefined) {
      await unsubscribeWebhook(context.auth.secret_text, response.subscriptionId);
    }
  },
  async run(context) {
    return [context.payload.body];
  },
  sampleData: {
    event: 'pageRevivedLive',
    idempotencyKey: 'b1c51b5b4844a81f7c3745002dbbd871fdda6cabe019961bcc79f972d924a2b0',
    page: {
      id: '6ee0f841f3cc8900090d82dc',
      metadata: {}
    }
  }
});