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.7 KB
Raw Blame
import { TriggerStrategy, createTrigger } from '@activepieces/pieces-framework';
import { smCommon } from '../common';
import { smAuth } from '../..';

export const newResponse = createTrigger({
  auth: smAuth,
  name: 'new_response',
  displayName: 'New Response',
  description: 'Triggers when a new response is submitted',
  aiMetadata: {
    description:
      'Fires when a respondent submits a new response to the selected SurveyMonkey survey. Emits the full submitted response, including the survey identifier, response ID, and the respondent answers, fetched from the SurveyMonkey API.',
  },
  type: TriggerStrategy.WEBHOOK,
  sampleData: {},
  props: {
    survey: smCommon.survey,
  },
  //Create the webhook in SurveyMonkey and save the webhook ID in store for disable behavior
  async onEnable(context) {
    const webhookId = await smCommon.subscribeWebhook(
      context.propsValue.survey as number,
      context.webhookUrl,
      context.auth['access_token']
    );

    await context.store?.put('_new_response_trigger', {
      webhookId: webhookId,
    });
  },
  //Delete the webhook from SurveyMonkey
  async onDisable(context) {
    const response: any = await context.store?.get('_new_response_trigger');

    if (response !== null && response !== undefined) {
      await smCommon.unsubscribeWebhook(
        response.webhookId,
        context.auth['access_token']
      );
    }
  },
  //Return new response
  async run(context) {
    const payloadBody = context.payload.body as PayloadBody;
    const responseData = await smCommon.getResponseDetails(
      context.auth['access_token'],
      context.propsValue['survey'] as number,
      payloadBody.object_id
    );

    return [responseData];
  },
});

type PayloadBody = {
  object_id: string | number;
};