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

5.1 KB
Raw Blame
import {
    HttpMethod,
    propsValidation,
} from '@activepieces/pieces-common';
import { contiguityAuth } from '../../..';
import {
    InputPropertyMap,
    Property,
    createAction,
} from '@activepieces/pieces-framework';
import * as z from 'zod/mini'
import { _fetch } from '../../common/request';

export const send_iMessage = createAction({
    auth: contiguityAuth,
    name: 'send_imessage',
    classification: 'WRITE',
    displayName: 'Send iMessage',
    description: 'Send an iMessage',
    audience: 'both',
    aiMetadata: { description: 'Sends an outbound iMessage through Contiguity to a recipient phone number, optionally with attachments (up to 10 HTTPS file URLs) and an SMS/RCS fallback that can trigger when iMessage is unsupported and/or fails. Choose this to message Apple/iMessage recipients; the recipient number must be in E.164 format and the message body is required. Not idempotent: each call dispatches a new message.', idempotent: false },
    props: {
        to: Property.ShortText({
            displayName: 'To',
            description: 'Recipient phone number in E.164 format',
            required: true,
        }),
        from: Property.ShortText({
            displayName: 'From',
            description: 'Your leased iMessage number.',
            required: false,
        }),
        message: Property.LongText({
            displayName: 'Message',
            description: 'iMessage content',
            required: true,
        }),
        fallback: Property.Checkbox({
            displayName: 'SMS/RCS Fallback',
            description: 'Fallback to SMS/RCS when iMessage fails or unsupported',
            required: false,
            defaultValue: false,
        }),
        fallback_when: Property.DynamicProperties({
            auth: contiguityAuth,
            displayName: "When to fallback",
            description: 'Conditions that trigger SMS/RCS fallback',
            required: true,
            refreshers: ['fallback'],
            props: async (propsValue): Promise<InputPropertyMap> => {
                const fallback = propsValue['fallback'] as unknown as boolean;

                if (!fallback){
                    return{}
                }
                
                if (fallback){
                    return{
                        when: Property.StaticMultiSelectDropdown({
                            displayName: "When to fallback",
                            description: "Conditions that trigger SMS/RCS fallback",
                            required: true,
                            options: {
                                options:[
                                    { label: 'iMessage Unsupported', value: 'imessage_unsupported' },
                                    { label: 'iMessage Fails', value: 'imessage_fails' },
                                ]
                            }
                        }),
                        from: Property.ShortText({
                            displayName: 'Fallback From Number',
                            description: 'SMS/RCS number for fallback',
                            required: false,
                        }),
                    };
                }
                return{};
            },
        }),
        attachments: Property.Array({
            displayName: 'Attachments',
            description: 'File URLs (max 10, 50MB total, HTTPS required)',
            required: false,
            properties: {
                url: Property.ShortText({
                    displayName: 'File URL',
                    description: 'HTTPS URL with file extension',
                    required: true,
                }),
            },
        }),
    },
    async run(context) {
        await propsValidation.validateZod(context.propsValue, {
            to: z.string().check(z.regex(/^\+\d{1,4}\d+$/, 'Invalid E.164 format')),
            from: z.optional(z.string().check(z.regex(/^\+\d{1,4}\d+$/, 'Must be E.164 format'))),
            message: z.string().check(z.minLength(1, 'Message cannot be empty')),
            fallback: z.optional(z.object({
                when: z.array(z.enum(['imessage_unsupported', 'imessage_fails'])).check(z.minLength(1, 'Select at least one fallback condition')),
                from: z.optional(z.string().check(z.regex(/^\+\d{1,4}\d+$/, 'Must be E.164 format'))),
            })),
            attachments: z.optional(z.array(z.object({
                    url: z.string().check(z.url('Invalid URL format'), z.regex(/^https:\/\/.*\.[a-zA-Z0-9]+$/, 'Must be HTTPS URL with file extension'))
                })).check(z.maxLength(10, 'Maximum 10 attachments'))),
        });

        const { to, from, message, attachments } = context.propsValue;

        const body: any = { to, message };

        if (from) body.from = from;
        if (attachments?.length) {
            body.attachments = attachments.map(attachment => (attachment as {url: string}).url);
        }

        return await _fetch({
            method: HttpMethod.POST,
            endpoint: '/send/imessage',
            body: body,
            auth: context.auth.secret_text,
        });
    },
});