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.1 KB
Raw Blame
import {
  TriggerStrategy,
  createTrigger,
  Property,
  AppConnectionValueForAuthProperty,
} from '@activepieces/pieces-framework';
import {
  DedupeStrategy,
  httpClient,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import { zendeskAuth } from '../auth';
import { getZendeskAuthentication, getZendeskBaseUrl } from '../common/client';
import { zendeskCreateTicketOutputSchema } from '../output-schemas';

export const newTicketInView = createTrigger({
  auth: zendeskAuth,
  name: 'new_ticket_in_view',
  outputSchema: zendeskCreateTicketOutputSchema,
  classification: 'READ',
  displayName: 'New ticket in view',
  description: 'Triggers when a new ticket is created in a view',
  aiMetadata: {
    description: 'Fires when a new ticket appears in a selected Zendesk view (polled on a schedule). Represents a ticket newly matching that view\'s criteria; the view to monitor is required.',
  },
  type: TriggerStrategy.POLLING,
  props: {
    view_id: Property.Dropdown({
      auth: zendeskAuth,
      displayName: 'View',
      description: 'The view to monitor for new tickets',
      refreshers: [],
      required: true,
      options: async ({ auth }) => {
        const authentication = auth;
        if (
          !authentication
        ) {
          return {
            placeholder: 'Fill your authentication first',
            disabled: true,
            options: [],
          };
        }
        const response = await httpClient.sendRequest<{ views: any[] }>({
          url: `${getZendeskBaseUrl(auth)}/views.json`,
          method: HttpMethod.GET,
          authentication: getZendeskAuthentication(auth),
        });
        return {
          placeholder: 'Select a view',
          options: response.body.views.map((view: any) => ({
            label: view.title,
            value: view.id,
          })),
        };
      },
    }),
  },
  sampleData: {
    url: 'https://activepieceshelp.zendesk.com/api/v2/tickets/5.json',
    id: 5,
    external_id: null,
    via: {
      channel: 'web',
      source: {
        from: {},
        to: {},
        rel: null,
      },
    },
    created_at: '2023-03-25T02:39:41Z',
    updated_at: '2023-03-25T02:39:41Z',
    type: null,
    subject: 'Subject',
    raw_subject: 'Raw Subject',
    description: 'Description',
    priority: null,
    status: 'open',
    recipient: null,
    requester_id: 8193592318236,
    submitter_id: 8193592318236,
    assignee_id: 8193592318236,
    organization_id: 8193599387420,
    group_id: 8193569448092,
    collaborator_ids: [],
    follower_ids: [],
    email_cc_ids: [],
    forum_topic_id: null,
    problem_id: null,
    has_incidents: false,
    is_public: true,
    due_at: null,
    tags: [],
    custom_fields: [],
    satisfaction_rating: null,
    sharing_agreement_ids: [],
    custom_status_id: 8193592472348,
    fields: [],
    followup_ids: [],
    ticket_form_id: 8193569410076,
    brand_id: 8193583542300,
    allow_channelback: false,
    allow_attachments: true,
    from_messaging_channel: false,
  },
  onEnable: async (context) => {
    await pollingHelper.onEnable(polling, context);
  },
  onDisable: async (context) => {
    await pollingHelper.onDisable(polling, context);
  },
  run: async (context) => {
    return await pollingHelper.poll(polling, context);
  },
  test: async (context) => {
    return await pollingHelper.test(polling, context);
  },
});



const polling: Polling<AppConnectionValueForAuthProperty<typeof zendeskAuth>, { view_id: string }> = {
  strategy: DedupeStrategy.LAST_ITEM,
  items: async ({ auth, propsValue }) => {
    const items = await getTickets(auth, propsValue.view_id);
    return items.map((item) => ({
      id: item.id,
      data: item,
    }));
  },
};

async function getTickets(authentication: AppConnectionValueForAuthProperty<typeof zendeskAuth>, view_id: string) {
  const response = await httpClient.sendRequest<{ tickets: any[] }>({
    url: `${getZendeskBaseUrl(authentication)}/views/${view_id}/tickets.json?sort_order=desc&sort_by=created_at&per_page=200`,
    method: HttpMethod.GET,
    authentication: getZendeskAuthentication(authentication),
  });
  return response.body.tickets;
}