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.9 KB
Raw Blame
import { ApFile, Property, isNil } from '@activepieces/pieces-framework';
import mime from 'mime-types';
import { EUploadMimeType } from 'twitter-api-v2';

function describeStatus({
  code,
  notFoundHint,
}: {
  code?: number;
  notFoundHint: string;
}): string | undefined {
  if (code === 401) {
    return 'X API 401: the stored credentials were rejected. Reconnect the X connection.';
  }
  if (code === 403) {
    return 'X API 403: the X app permission level does not allow this operation, or the target resource does not belong to the authenticated account.';
  }
  if (code === 404) {
    return `X API 404: ${notFoundHint}`;
  }
  if (code === 429) {
    return 'X API 429: rate limit reached. Wait for the limit window to reset before retrying.';
  }
  return undefined;
}

function asTwitterError(value: unknown): TwitterErrorLike {
  if (typeof value === 'object' && value !== null) {
    return value;
  }
  return { message: String(value) };
}

function mediaMimeType(file: ApFile): string {
  const detected = isNil(file.extension) ? false : mime.lookup(file.extension);
  const accepted: string[] = [
    EUploadMimeType.Jpeg,
    EUploadMimeType.Png,
    EUploadMimeType.Gif,
    EUploadMimeType.Webp,
    EUploadMimeType.Mp4,
    EUploadMimeType.Mov,
  ];
  return detected && accepted.includes(detected) ? detected : EUploadMimeType.Png;
}

function buildError({
  error,
  notFoundHint,
}: {
  error: TwitterErrorLike;
  notFoundHint: string;
}): Error {
  const described = describeStatus({ code: error?.code, notFoundHint });
  if (described === undefined && error?.errors === undefined) {
    const cause =
      typeof error?.message === 'string' && error.message.length > 0
        ? error.message
        : String(error);
    return new Error(`X API request failed: ${cause}`);
  }
  return new Error(
    `${described ?? 'X API request failed.'} ${JSON.stringify({
      code: error?.code,
      errors: error?.errors,
    })}`
  );
}

export const twitterCommon = {
  text: Property.LongText({
    displayName: 'Text',
    description: 'Up to 280 characters, or more with X Premium.',
    required: true,
  }),
  image_1: Property.File({
    displayName: 'Media 1',
    description: 'An image, GIF or video. Add more images under Advanced.',
    required: false,
  }),
  image_2: Property.File({
    displayName: 'Media 2',
    description: 'Another image. GIFs and videos must be posted alone.',
    required: false,
    advanced: true,
  }),
  image_3: Property.File({
    displayName: 'Media 3',
    description: 'Another image. GIFs and videos must be posted alone.',
    required: false,
    advanced: true,
  }),
};

export const twitterHelpers = {
  buildError,
  asTwitterError,
  mediaMimeType,
};

export const twitterFieldSets = {
  user: 'created_at,description,public_metrics,verified,profile_image_url',
};

export type TwitterErrorLike = {
  code?: number;
  errors?: unknown;
  message?: string;
};