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.0 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { sendMessageResultOutputSchema } from '../../../output-schemas';
import { HttpMethod } from '@activepieces/pieces-common';
import { whatsscaleAuth } from '../../../auth';
import { whatsscaleClient } from '../../../common/client';
import { ConductorSendMessageResult, flattenSendMessageResult } from '../../../common/messaging';
import { whatsscaleProps } from '../../../common/props';
import { prepareMediaFile } from '../../../common/prepare-file';
import { pollJob } from '../../../common/poll-job';

export const sendDocumentToGroupAction = createAction({
  auth: whatsscaleAuth,
  name: 'whatsscale_send_document_to_group',
  classification: 'WRITE',
  displayName: 'Send a Document to a Group',
  description: 'Send a document to a WhatsApp group selected from the dropdown.',
  audience: 'human',
  aiMetadata: { description: 'Sends a document/file to a WhatsApp group whose chat ID is chosen from the session group list, with an optional display filename and caption. Pick this when the recipient is a known group; use the manual-entry, contact, CRM-contact, or channel variants for other recipient types. Takes either a directly downloadable document URL or a file from a previous step. Not idempotent: each call delivers another document.', idempotent: false },
  outputSchema: sendMessageResultOutputSchema,
  props: {
    session: whatsscaleProps.session,
    group: whatsscaleProps.group,
    documentUrl: Property.File({
      displayName: 'Document',
      description: 'A direct URL to the document, or a file from a previous step.',
      required: true,
    }),
    filename: Property.ShortText({
      displayName: 'Filename',
      description: 'Optional filename shown in WhatsApp (e.g. report.pdf). Auto-detected from the file if not provided.',
      required: false,
    }),
    caption: Property.LongText({
      displayName: 'Caption',
      description: 'Optional caption for the document (max 1024 characters).',
      required: false,
    }),
  },
  propertyGroups: [
    { key: 'destination', display: 'section' as const, label: 'Destination', icon: 'send', props: ['session', 'group'] },
    { key: 'content', display: 'section' as const, label: 'Document', icon: 'file', props: ['documentUrl', 'filename', 'caption'] },
  ],
  async run(context) {
    const { session, group, documentUrl, filename, caption } = context.propsValue;
    const apiKey = context.auth.secret_text;

    const preparedUrl = await prepareMediaFile({ apiKey, file: documentUrl, files: context.files, mediaType: 'document' });

    const body: Record<string, unknown> = {
      session,
      chatId: group,
      file: preparedUrl,
      caption: caption ?? '',
    };
    if (filename) body['filename'] = filename;

    const sendResponse = await whatsscaleClient(apiKey, HttpMethod.POST, '/api/sendDocument', body);
    const { jobId } = sendResponse.body as { jobId: string };
    const result = await pollJob(apiKey, jobId);
    return flattenSendMessageResult(result as ConductorSendMessageResult);
  },
});