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

3.2 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { timelinesAiAuth, timelinesAiCommon } from '../common';
import { whatsappAccountDropdown } from '../common/properties';

export const findChat = createAction({
  auth: timelinesAiAuth,
  name: 'findChat',
  classification: 'SEARCH',
  displayName: 'Find Chat',
  description:
    'Look up a chat by parameters such as chat_id, phone, name, etc.',
  audience: 'both',
  aiMetadata: { description: 'Searches TimelinesAI WhatsApp chats, optionally filtered by labels, WhatsApp account, name, group/read/closed/autoresponse flags, responsible user, and creation date range; with no filters supplied it returns all chats (paginated). Use to find a chat or its chat_id before sending or closing. Read-only and idempotent.', idempotent: true },
  props: {
    label: Property.Array({
      displayName: 'Labels',
      description: 'Filter by labels assigned to the chat',
      required: false,
    }),
    whatsapp_account_id: whatsappAccountDropdown({ required: false }),
    group: Property.Checkbox({
      displayName: 'Is Group',
      description: 'Filter by whether the chat is a group chat',
      required: false,
    }),
    responsible: Property.Array({
      displayName: 'Responsible',
      description: 'Filter by the user responsible for the chat (email)',
      required: false,
    }),
    name: Property.ShortText({
      displayName: 'Chat Name',
      description: 'Filter by the name of the chat',
      required: false,
    }),
    read: Property.Checkbox({
      displayName: 'Is Read',
      description: 'Filter by whether the chat is marked as read',
      required: false,
    }),
    closed: Property.Checkbox({
      displayName: 'Is Closed',
      description: 'Filter by whether the chat is closed',
      required: false,
    }),
    chatgpt_autoresponse_enabled: Property.Checkbox({
      displayName: 'ChatGPT Autoresponse Enabled',
      description:
        'Filter by whether ChatGPT autoresponse is enabled for the chat',
      required: false,
    }),
    created_after: Property.DateTime({
      displayName: 'Created After',
      description: 'Filter chats created after this date',
      required: false,
    }),
    created_before: Property.DateTime({
      displayName: 'Created Before',
      description: 'Filter chats created before this date',
      required: false,
    }),
    page: Property.Number({
      displayName: 'Page',
      description: 'Page number for paginated results',
      required: false,
      defaultValue: 1,
    }),
  },
  async run({ auth: apiKey, propsValue }) {
    const { label, responsible, ...rest } = propsValue;
    const chatParams = {
      ...(label?.length && label.length > 0 ? { label: label.join(',') } : {}),
      ...(responsible?.length && responsible.length > 0
        ? { responsible: responsible.join(',') }
        : {}),
      ...rest,
    };
    console.log('Chat search parameters:', chatParams);
    const response = await timelinesAiCommon.getChats({
      apiKey,
      ...chatParams,
    });
    if (response.status !== 'ok') {
      throw new Error(
        `Error fetching chat: ${response.message || 'Unknown error'}`
      );
    }
    return response.data;
  },
});