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.1 KB
Raw Blame
import {
  AuthenticationType,
  httpClient,
  HttpMethod,
} from '@activepieces/pieces-common';
import { smailyAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';

export const createOrUpdateSubscriberAction = createAction({
  auth: smailyAuth,
  name: 'create-or-update-subscriber',
  displayName: 'Create or Update Subscriber',
  description:
    'Creates a new subscriber or update an existing subscriber by email.',
  audience: 'both',
  aiMetadata: {
    description:
      'Upsert a subscriber in a Smaily account, keyed on the email address: creates the contact if the email is new, otherwise updates the existing one (including subscription status and custom fields). Use to add or sync contact data. Idempotent — repeating the same input leaves the subscriber in the same state rather than creating duplicates.',
    idempotent: true,
  },
  props: {
    email: Property.ShortText({
      displayName: 'Email',
      required: true,
    }),
    is_unsubscribed: Property.StaticDropdown({
      displayName: 'Subscription Status',
      required: true,
      defaultValue: 0,
      options: {
        disabled: false,
        options: [
          { label: 'Unsubscribed', value: 1 },
          { label: 'Subscribed', value: 0 },
        ],
      },
    }),
    custom_fields: Property.Object({
      displayName: 'Custom Fields',
      description: `Go to **Subscribers Tab-> All Subscribers -> Manage Fields** to get custom field names.`,
      required: false,
    }),
  },
  async run(context) {
    const response = await httpClient.sendRequest({
      method: HttpMethod.POST,
      url: `https://${context.auth.props.domain}.sendsmaily.net/api/contact.php`,
      headers: {
        'Content-Type': 'application/json',
      },
      authentication: {
        type: AuthenticationType.BASIC,
            username: context.auth.props.username,
        password: context.auth.props.password,
      },
      body: {
        email: context.propsValue.email,
        is_unsubscribed: context.propsValue.is_unsubscribed,
        ...context.propsValue.custom_fields,
      },
    });

    return response.body;
  },
});