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

4.8 KB
Raw Blame
import { intercomAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';
import { intercomClient, Operator } from '../common';
import dayjs from 'dayjs';
import { findConversationOutputSchema } from '../output-schemas';

export const findConversationAction = createAction({
    auth: intercomAuth,
    name: 'find-conversation',
    classification: 'SEARCH',
    displayName: 'Find Conversation',
    description: 'Searches for conversations using various criteria',
    audience: 'human',
    aiMetadata: { description: 'Search conversations by a chosen field (ID, subject, message body, author email, assigned admin, team, or tag IDs) with a contains/equals/starts-with match (IDs: equals only), optionally filtered by open/closed/snoozed status and an update-time window. Read-only and repeatable; returns the first match plus a found flag. Use when you do not already have the conversation ID — otherwise use Retrieve a Conversation.', idempotent: true },
    props: {
        searchField: Property.StaticDropdown({
            displayName: 'Search Field',
            required: true,
            options: {
                disabled: false,
                options: [
                    { label: 'Conversation ID', value: 'id' },
                    { label: 'Subject', value: 'source.subject' },
                    { label: 'Message Body', value: 'source.body' },
                    { label: 'Author Email', value: 'source.author.email' },
                    { label: 'Assigned Admin', value: 'admin_assignee_id' },
                    { label: 'Team', value: 'team_assignee_id' },
                    { label: 'Tag IDs', value: 'tag_ids' },
                ],
            },
        }),
        matchType: Property.StaticDropdown({
            displayName: 'Match Type',
            required: true,
            options: {
                disabled: false,
                options: [
                    { label: 'Contains', value: 'contains' },
                    { label: 'Equals', value: 'equals' },
                    { label: 'Starts With', value: 'starts_with' },
                ],
            },
        }),
        searchTerm: Property.ShortText({
            displayName: 'Search Term',
            description: 'Conversation ID, Assigned Admin, Team and Tag IDs only support Equals.',
            required: true,
        }),
        status: Property.StaticDropdown({
            displayName: 'Status',
            required: false,
            options: {
                disabled: false,
                options: [
                    { label: 'Open', value: 'open' },
                    { label: 'Closed', value: 'closed' },
                    { label: 'Snoozed', value: 'snoozed' },
                ],
            },
        }),
        updateAfter: Property.DateTime({
            displayName: 'Update After',
            description: 'Intercom compares dates by calendar day in your workspace time zone.',
            required: false,
        }),
        updateBefore: Property.DateTime({
            displayName: 'Update Before',
            required: false,
        }),
    },
    outputSchema: findConversationOutputSchema,
    async run(context) {
        const { searchField, matchType, searchTerm, status, updateAfter, updateBefore } =
            context.propsValue;

        if (matchType !== 'equals' && EQUALS_ONLY_FIELDS.includes(searchField)) {
            throw new Error('Conversation ID, Assigned Admin, Team and Tag IDs can only be matched with Equals.');
        }

        const operator = matchType === 'contains' ? '~' : matchType === 'starts_with' ? '^' : '=';


        const client = await intercomClient(context.auth);

        const filter = [
            {
                field: searchField,
                operator: operator as Operator,
                value: searchTerm,
            },
        ];
        if (status) {
            filter.push({
                field: 'state',
                operator: "=",
                value: status,
            });
        }
        if (updateAfter) {
            filter.push({
                field: 'updated_at',
                operator: '>',
                value: dayjs(updateAfter).unix().toString(),
            });
        }
        if (updateBefore) {
            filter.push({
                field: 'updated_at',
                operator: "<",
                value: dayjs(updateBefore).unix().toString(),
            });
        }

        const response = await client.conversations.search({
            query: {
				operator: 'AND',
				value: filter,
			},
        });

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

const EQUALS_ONLY_FIELDS = ['id', 'admin_assignee_id', 'team_assignee_id', 'tag_ids'];