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.2 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { aircallAuth } from '../common/auth';
import { makeRequest } from '../common/client';
import { HttpMethod } from '@activepieces/pieces-common';
import { tagIdDropdown } from '../common/props';

export const findCalls = createAction({
  auth: aircallAuth,
  name: 'findCalls',
  classification: 'SEARCH',
  displayName: 'Find Call',
  description: 'Finds specific call based on provided filter.',
  audience: 'both',
  aiMetadata: { description: 'Searches Aircall calls and returns the single most recent match, optionally narrowed by direction (inbound/outbound), phone number, and tag IDs; with no filters it returns the latest call overall. Use to look up a call before commenting, tagging, or fetching its full details. Read-only and idempotent.', idempotent: true },
  props: {
    direction: Property.StaticDropdown({
      displayName: 'Call Direction',
      description: 'Filter by call direction',
      required: false,
      options: {
        options: [
          { label: 'Inbound', value: 'inbound' },
          { label: 'Outbound', value: 'outbound' },
        ],
      },
    }),
    phone_number: Property.ShortText({
      displayName: 'Phone Number',
      description: 'The calling or receiving phone number of calls.',
      required: false,
    }),
    tags: tagIdDropdown,
  },
  async run(context) {
    const { direction, phone_number, tags } = context.propsValue;

    // Build query parameters
    const queryParams = new URLSearchParams();
    queryParams.set('order', 'desc');
    queryParams.set('fetch_contact', 'true');
    queryParams.set('fetch_short_urls', 'true');

    if (direction) queryParams.set('direction', direction);
    if (phone_number) queryParams.set('phone_number', phone_number);

    if (tags && tags.length > 0) {
      tags.forEach((tagId) => queryParams.append('tags[]', tagId.toString()));
    }

    const queryString = queryParams.toString();
    const path = `/calls/search${queryString ? `?${queryString}` : ''}`;

    const response = await makeRequest(context.auth, HttpMethod.GET, path);

    const { calls } = response as { calls: { id: number }[] };

    return {
      found: calls.length > 0,
      data: calls.length > 0 ? calls[0] : {},
    };
  },
});