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 { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { youtubeAuth } from '../common/auth';
import { listCommentsOutputSchema } from '../output-schemas';

export const youtubeListCommentsAction = createAction({
  auth: youtubeAuth,
  outputSchema: listCommentsOutputSchema,
  name: 'list_comments',
  classification: 'SEARCH',
  displayName: 'List Comments',
  description: 'List the top-level comment threads on a video.',
  audience: 'human',
  aiMetadata: {
    description:
      'Lists comment threads on a YouTube video using commentThreads.list, returning each top-level comment with its author, text, like count and reply count. Use it to read audience feedback on a video for triage, sentiment or moderation flows. Comments must be enabled on the video, otherwise YouTube answers 403. Read-only and idempotent.',
    idempotent: true,
  },
  props: {
    videoId: Property.ShortText({
      displayName: 'Video ID',
      description: "The v= value in the video's URL.",
      placeholder: 'dQw4w9WgXcQ',
      required: true,
    }),
    order: Property.StaticDropdown({
      displayName: 'Order',
      description: 'How to sort the comments. Empty: newest first.',
      required: false,
      options: {
        options: [
          { label: 'Relevance', value: 'relevance' },
          { label: 'Time', value: 'time' },
        ],
      },
    }),
    maxResults: Property.Number({
      displayName: 'Max Results',
      description: 'How many comment threads to return, up to 100.',
      display: 'stepper',
      min: 1,
      max: 100,
      step: 1,
      required: false,
      defaultValue: 20,
    }),
    pageToken: Property.ShortText({
      displayName: 'Page Token',
      description: 'The nextPageToken from an earlier run, to get the next page.',
      required: false,
      advanced: true,
    }),
  },
  propertyGroups: [
    {
      key: 'video',
      display: 'section',
      label: 'Video',
      icon: 'file',
      props: ['videoId'],
    },
    {
      key: 'results',
      display: 'section',
      label: 'Results',
      icon: 'sliders',
      props: ['order', 'maxResults'],
    },
  ],
  async run(context) {
    const accessToken = context.auth.access_token;
    const { videoId, order, maxResults, pageToken } = context.propsValue;

    if (maxResults !== undefined && maxResults !== null) {
      const maxResultsNumber = Math.trunc(Number(maxResults));
      if (!Number.isFinite(maxResultsNumber) || maxResultsNumber < 1 || maxResultsNumber > 100) {
        throw new Error('Max Results must be between 1 and 100.');
      }
    }

    const queryParams: Record<string, string> = {
      part: 'snippet,replies',
      videoId,
      maxResults: String(Math.trunc(Number(maxResults ?? 20))),
    };
    if (order) {
      queryParams['order'] = order;
    }
    if (pageToken) {
      queryParams['pageToken'] = pageToken;
    }

    const response = await httpClient.sendRequest({
      method: HttpMethod.GET,
      url: 'https://www.googleapis.com/youtube/v3/commentThreads',
      headers: { Authorization: `Bearer ${accessToken}` },
      queryParams,
    });

    return response.body;
  },
});