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.8 KB
Raw Blame
import {
  createTrigger,
  Property,
  TriggerStrategy,
} from '@activepieces/pieces-framework';
import { HttpMethod, httpClient } from '@activepieces/pieces-common';
import { togglTrackAuth } from '../auth';
import { togglApi } from '../common/client';
import { togglCommon } from '../common';
import {
  generateTogglWebhookInstructions,
  TOGGL_WEBHOOK_EVENTS,
} from '../common/webhook-instructions';

export const newTask = createTrigger({
  auth: togglTrackAuth,
  name: 'new_task',
  classification: 'READ',
  displayName: 'New Task',
  description: 'Fires when a new task is created.',
  aiMetadata: {
    description: 'Fires when a task is created in the workspace, delivering the task; an optional filter narrows it to one project. Needs a webhook created manually in Toggl. Classic only.',
  },
  props: {
    workspace_id: togglCommon.workspace_id,
    optional_project_id: togglCommon.optional_project_id,
    setupInstructions: Property.MarkDown({
      value: generateTogglWebhookInstructions(
        TOGGL_WEBHOOK_EVENTS.TASK_CREATED,
        'New Task',
        'Create a test task to ensure events are received',
        `This trigger will fire when tasks are created and will include:
- Task ID and details
- Project association
- Workspace information
- Creation timestamp
- Creator information
- Task settings (estimated time, active status, etc.)

**Note:** You can filter tasks by project using the optional project filter above.`
      ),
    }),
  },
  sampleData: {
    id: 789123456,
    name: 'Design homepage mockup',
    project_id: 123456789,
    workspace_id: 987654,
    user_id: 6,
    estimated_seconds: 7200,
    active: true,
    at: '2025-08-29T10:15:30+00:00',
    tracked_seconds: 0,
  },
  type: TriggerStrategy.WEBHOOK,

  async onEnable(context) {
    if (togglApi.isTwo(context.auth)) {
      throw togglApi.classicOnlyError(WEBHOOK_TRIGGER_NAME);
    }
  },

  async onDisable() {
    return;
  },

  async run(context) {
    const payload = context.payload.body as any;
    
    if (payload?.payload === 'ping' || payload?.validation_code || payload?.validation_code_url) {
      try {
        await httpClient.sendRequest({
          method: HttpMethod.GET,
          url: payload.validation_code_url,
          headers: {
            'Content-Type': 'application/json',
          },
        });
        
      } catch (error) {
        console.error('Failed to auto-validate webhook:', error);
      }
      
      return [
        {
          message: 'Webhook validation successful',
          timestamp: new Date().toISOString(),
          status: 'validated',
        },
      ];
    }

    if (context.propsValue.optional_project_id && payload?.project_id) {
      if (payload.project_id !== context.propsValue.optional_project_id) {
        return [];
      }
    }

    return [payload];
  },
});

const WEBHOOK_TRIGGER_NAME = 'The New Task trigger';