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

export const setCustomFieldAction = createAction({
	auth: manychatAuth,
	name: 'setCustomField',
	classification: 'WRITE',
	displayName: 'Set Custom Field',
	description: 'Ass or Updates a custom field value for a user.',
	audience: 'both',
	aiMetadata: { description: 'Sets or updates a custom field value on a Manychat subscriber, identified by subscriber ID and the chosen custom field, then returns the updated subscriber info. Use to store or change a single attribute on a known contact; the input type adapts to the field (text, number, date, or boolean). Idempotent: setting the same value again yields the same state.', idempotent: true },
	props: {
		subscriber_id: subscriberId,
		field_id: Property.Dropdown({
			auth: manychatAuth,
			displayName: 'Custom Field',
			refreshers: [],
			required: true,
			options: async ({ auth }) => {
				if (!auth) {
					return {
						placeholder: 'Please connect your account.',
						disabled: true,
						options: [],
					};
				}

				const response = await httpClient.sendRequest<{
					data: Array<{ id: number; name: string; type: string }>;
				}>({
					url: `${BASE_URL}/page/getCustomFields`,
					method: HttpMethod.GET,
					authentication: {
						type: AuthenticationType.BEARER_TOKEN,
						token: auth.secret_text
					},
				});
				return {
					disabled: false,
					options: response.body.data
						.filter((field) => field.type !== 'array')
						.map((field) => ({
							label: field.name,
							value: `${field.id}:::${field.type}`,
						})),
				};
			},
		}),
		field_value: Property.DynamicProperties({
			auth: manychatAuth,
				displayName: 'Field Value',
			required: true,
			refreshers: ['field_id'],
			props: async ({ auth, field_id }) => {
				if (!auth || !field_id) return {};

				const fields: DynamicPropsValue = {};

				const fieldType = (field_id as unknown as string).split(':::')[1];

				switch (fieldType) {
					case 'text':
						fields['value'] = Property.ShortText({
							displayName: 'Value',
							required: true,
						});
						break;
					case 'number':
						fields['value'] = Property.Number({
							displayName: 'Value',
							required: true,
						});
						break;
					case 'date':
					case 'datetime':
						fields['value'] = Property.DateTime({
							displayName: 'Value',
							required: true,
						});
						break;
					case 'boolean':
						fields['value'] = Property.Checkbox({
							displayName: 'Value',
							required: true,
						});
						break;
					default:
						break;
				}
				return fields;
			},
		}),
	},
	async run({ auth, propsValue }) {
		const { subscriber_id, field_id, field_value } = propsValue;

		const setCustomFieldResponse = await httpClient.sendRequest({
			method: HttpMethod.POST,
			url: `${BASE_URL}/subscriber/setCustomField`,
			authentication: {
				type: AuthenticationType.BEARER_TOKEN,
				token: auth.secret_text,
			},
			body: {
				subscriber_id,
				field_id: field_id.split(':::')[0],
				field_value: field_value['value'],
			},
		});

		if (setCustomFieldResponse.body.status !== 'success') {
			throw Error(`Unexpected Error occured : ${JSON.stringify(setCustomFieldResponse.body)}`);
		}

		const userResponse = await httpClient.sendRequest<{ data: Record<string, any> }>({
			method: HttpMethod.GET,
			url: `${BASE_URL}/subscriber/getInfo`,
			authentication: {
				type: AuthenticationType.BEARER_TOKEN,
				token: auth.secret_text,
			},
			queryParams: {
				subscriber_id: `${subscriber_id}`,
			},
		});

		return userResponse.body.data;
	},
});