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.3 KB
Raw Blame
import { HttpMethod } from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { knockAuth } from '../auth';
import { knockApiCall } from '../common/client';

export const triggerWorkflow = createAction({
  auth: knockAuth,
  name: 'trigger_workflow',
  displayName: 'Trigger Workflow',
  description: 'Trigger a Knock notification workflow for one or more recipients.',
  audience: 'both',
  aiMetadata: {
    description: 'Triggers a Knock notification workflow (identified by its workflow key) to deliver notifications to one or more recipient user IDs, optionally attaching template data, an actor, and a tenant. Choose this to send notifications to users; the recipients must already exist in Knock or be identified inline. Not idempotent — each call enqueues a fresh notification run.',
    idempotent: false,
  },
  props: {
    workflowKey: Property.ShortText({
      displayName: 'Workflow Key',
      description: 'The key of the workflow to trigger (e.g. "new-comment").',
      required: true,
    }),
    recipients: Property.Array({
      displayName: 'Recipient IDs',
      description: 'One or more user IDs to receive the notification.',
      required: true,
    }),
    actorId: Property.ShortText({
      displayName: 'Actor ID',
      description: 'The user ID of the person performing the action (optional).',
      required: false,
    }),
    data: Property.Object({
      displayName: 'Data',
      description: 'Key-value data passed to the workflow template (optional).',
      required: false,
    }),
    tenant: Property.ShortText({
      displayName: 'Tenant',
      description: 'Optional tenant identifier for multi-tenant workflows.',
      required: false,
    }),
  },
  async run(context) {
    const { workflowKey, recipients, actorId, data, tenant } =
      context.propsValue;

    const body: Record<string, unknown> = {
      recipients,
    };

    if (actorId) {
      body['actor'] = actorId;
    }
    if (data && Object.keys(data).length > 0) {
      body['data'] = data;
    }
    if (tenant) {
      body['tenant'] = tenant;
    }

    return knockApiCall({
      apiKey: context.auth.secret_text,
      method: HttpMethod.POST,
      path: `/workflows/${encodeURIComponent(workflowKey)}/trigger`,
      body,
    });
  },
});