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

export const newTaskCreatedTrigger = createTrigger({
  auth: kommoAuth,
  name: 'new_task_created',
  classification: 'READ',
  displayName: 'New Task Created',
  description: 'Triggered when a new task is created.',
  aiMetadata: {
    description: 'Fires when a new task is created in the Kommo CRM account, emitting the full new task record (including its linked entity and due date). Represents a follow-up or to-do being scheduled against a lead, contact, or company.',
  },
  type: TriggerStrategy.WEBHOOK,
  props: {},
  async onEnable(context) {
    const { subdomain, apiToken } = context.auth.props as { subdomain: string; apiToken: string };

    const webhook = await makeRequest(
      { subdomain, apiToken },
      HttpMethod.POST,
      `/webhooks`,
      {
        destination: context.webhookUrl,
        settings: ['add_task']
      }
    );

    await context.store.put('webhookId', webhook.id);
  },

  async onDisable(context) {
    const { subdomain, apiToken } = context.auth.props as { subdomain: string; apiToken: string };
    const webhookId = await context.store.get('webhookId');

    if (webhookId) {
      await makeRequest(
        { subdomain, apiToken },
        HttpMethod.DELETE,
                `/webhooks`,
        {destination:context.webhookUrl}
      );
    }
  },

  async run(context) {
    const { subdomain, apiToken } = context.auth.props as { subdomain: string; apiToken: string };

    const payload = context.payload.body as { task: { add: { id: number }[] } }
    const taskId = payload.task.add[0].id;
    if (!taskId) return [];


    const response = await makeRequest({ apiToken, subdomain }, HttpMethod.GET, `/tasks/${taskId}`)
    return [response]
  },
  sampleData: {
    "id": 12040,
    "created_by": 13290567,
    "updated_by": 13290567,
    "created_at": 1748805952,
    "updated_at": 1748805952,
    "responsible_user_id": 13290567,
    "group_id": 0,
    "entity_id": 722830,
    "entity_type": "contacts",
    "duration": 0,
    "is_completed": false,
    "task_type_id": 1,
    "text": "Test",
    "result": [],
    "complete_till": 1748975340,
    "account_id": 34678947,
    "_links": {
      "self": {
        "href": ""
      }
    }
  }
});