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

1.9 KB
Raw Blame
import { slackAuth } from '../auth';
import { AuthenticationType, httpClient, HttpMethod } from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { WebClient } from '@slack/web-api';
import { getBotToken, SlackAuthValue } from '../common/auth-helpers';
import { getFileActionOutputSchema } from '../output-schemas';

export const getFileAction = createAction({
	auth: slackAuth,
	name: 'get-file',
	classification: 'READ',
	displayName: 'Get File',
	description: 'Return information about a given file ID.',
	audience: 'human',
	aiMetadata: {
		description:
			'Look up metadata for a single Slack file by its file ID and download its contents. Pick this when you have a file ID (e.g. from a message trigger payload) and need the file details or bytes. Read-only and idempotent; fails if the file has no accessible download URL.',
		idempotent: true,
	},
	outputSchema: getFileActionOutputSchema,
	props: {
		fileId: Property.ShortText({
			displayName: 'File ID',
			description: 'Found in trigger output or the file link.',
			placeholder: 'F0123ABC456',
			required: true,
		}),
	},
	async run(context) {
		const botToken = getBotToken(context.auth as SlackAuthValue);
		const client = new WebClient(botToken);

		const fileData = await client.files.info({ file: context.propsValue.fileId });

		const fileDownloadUrl = fileData.file?.url_private_download;

		if (!fileDownloadUrl) {
			throw new Error('Unable to find the download URL.');
		}

		const response = await httpClient.sendRequest({
			method: HttpMethod.GET,
			url: fileDownloadUrl,
			authentication: {
				type: AuthenticationType.BEARER_TOKEN,
				token: botToken,
			},
			responseType: 'arraybuffer',
		});

		return {
			...fileData.file,
			data: await context.files.write({
				fileName: fileData.file?.name || `file`,
				data: Buffer.from(response.body),
			}),
		};
	},
});