diff --git a/components/campaign_monitor/actions/add-subscriber/add-subscriber.mjs b/components/campaign_monitor/actions/add-subscriber/add-subscriber.mjs new file mode 100644 index 0000000000000..4d5e440692e1d --- /dev/null +++ b/components/campaign_monitor/actions/add-subscriber/add-subscriber.mjs @@ -0,0 +1,84 @@ +import campaignMonitor from "../../campaign_monitor.app.mjs"; + +export default { + key: "campaign_monitor-add-subscriber", + name: "Add Subscriber", + description: "Creates a new subscriber on a specific list. [See the documentation](https://www.campaignmonitor.com/api/v3-3/subscribers/)", + version: "0.0.1", + type: "action", + props: { + campaignMonitor, + clientId: { + propDefinition: [ + campaignMonitor, + "clientId", + ], + }, + listId: { + propDefinition: [ + campaignMonitor, + "listId", + (c) => ({ + clientId: c.clientId, + }), + ], + }, + email: { + type: "string", + label: "Email", + description: "The email address of the subscriber", + }, + name: { + type: "string", + label: "Name", + description: "The name of the subscriber", + optional: true, + }, + phone: { + type: "string", + label: "Phone", + description: "The phone number of the subscriber. Example: `+5012398752`", + optional: true, + }, + consentToTrack: { + propDefinition: [ + campaignMonitor, + "consentToTrack", + ], + }, + consentToSendSMS: { + type: "string", + label: "Consent to Send SMS", + description: "Indicates if consent has been granted by the subscriber to receive Sms", + options: [ + "Yes", + "No", + "Unchanged", + ], + default: "Unchanged", + optional: true, + }, + resubscribe: { + type: "boolean", + label: "Resubscribe", + description: "Resubscribe if the email address has previously been unsubscribed", + optional: true, + }, + }, + async run({ $ }) { + const response = await this.campaignMonitor.createSubscriber({ + $, + listId: this.listId, + data: { + EmailAddress: this.email, + Name: this.name, + MobileNumber: this.phone, + ConsentToTrack: this.consentToTrack, + ConsentToSendSms: this.consentToSendSMS, + Resubscribe: this.resubscribe, + }, + }); + $.export("$summary", `Successfully added subscriber ${this.email}`); + return response; + }, +}; diff --git a/components/campaign_monitor/actions/send-smart-transactional-email/send-smart-transactional-email.mjs b/components/campaign_monitor/actions/send-smart-transactional-email/send-smart-transactional-email.mjs new file mode 100644 index 0000000000000..3507386a189f6 --- /dev/null +++ b/components/campaign_monitor/actions/send-smart-transactional-email/send-smart-transactional-email.mjs @@ -0,0 +1,64 @@ +import campaignMonitor from "../../campaign_monitor.app.mjs"; + +export default { + key: "campaign_monitor-send-smart-transactional-email", + name: "Send Smart Transactional Email", + description: "Sends an intelligent transactional email to a specified recipient. [See the documentation](https://www.campaignmonitor.com/api/v3-3/transactional/#send-smart-email)", + version: "0.0.1", + type: "action", + props: { + campaignMonitor, + clientId: { + propDefinition: [ + campaignMonitor, + "clientId", + ], + }, + smartEmailId: { + propDefinition: [ + campaignMonitor, + "smartEmailId", + (c) => ({ + clientId: c.clientId, + }), + ], + }, + to: { + type: "string", + label: "To", + description: "An array of email addresses to send the email to", + }, + cc: { + type: "string", + label: "CC", + description: "An array of email address to carbon copy the email to", + optional: true, + }, + bcc: { + type: "string", + label: "BCC", + description: "An array of email address to blind carbon copy the email to", + optional: true, + }, + consentToTrack: { + propDefinition: [ + campaignMonitor, + "consentToTrack", + ], + }, + }, + async run({ $ }) { + const response = await this.campaignMonitor.sendSmartEmail({ + $, + smartEmailId: this.smartEmailId, + data: { + To: this.to, + CC: this.cc, + BCC: this.bcc, + ConsentToTrack: this.consentToTrack, + }, + }); + $.export("$summary", "Successfully sent smart transactional email"); + return response; + }, +}; diff --git a/components/campaign_monitor/actions/unsubscribe/unsubscribe.mjs b/components/campaign_monitor/actions/unsubscribe/unsubscribe.mjs new file mode 100644 index 0000000000000..fb5d562932f90 --- /dev/null +++ b/components/campaign_monitor/actions/unsubscribe/unsubscribe.mjs @@ -0,0 +1,47 @@ +import campaignMonitor from "../../campaign_monitor.app.mjs"; + +export default { + key: "campaign_monitor-unsubscribe", + name: "Unsubscribe", + description: "Removes a subscriber from a mailing list given their email address. [See the documentation](https://www.campaignmonitor.com/api/v3-3/subscribers/#unsubscribing-a-subscriber)", + version: "0.0.1", + type: "action", + props: { + campaignMonitor, + clientId: { + propDefinition: [ + campaignMonitor, + "clientId", + ], + }, + listId: { + propDefinition: [ + campaignMonitor, + "listId", + (c) => ({ + clientId: c.clientId, + }), + ], + }, + subscriber: { + propDefinition: [ + campaignMonitor, + "subscriber", + (c) => ({ + listId: c.listId, + }), + ], + }, + }, + async run({ $ }) { + const response = await this.campaignMonitor.unsubscribeSubscriber({ + $, + listId: this.listId, + data: { + EmailAddress: this.subscriber, + }, + }); + $.export("$summary", `Successfully unsubscribed ${this.subscriber}`); + return response; + }, +}; diff --git a/components/campaign_monitor/campaign_monitor.app.mjs b/components/campaign_monitor/campaign_monitor.app.mjs index 87a1742c0dad1..ead348f6733b7 100644 --- a/components/campaign_monitor/campaign_monitor.app.mjs +++ b/components/campaign_monitor/campaign_monitor.app.mjs @@ -1,11 +1,270 @@ +import { axios } from "@pipedream/platform"; +const DEFAULT_PAGE_SIZE = 1000; + export default { type: "app", app: "campaign_monitor", - propDefinitions: {}, + propDefinitions: { + clientId: { + type: "string", + label: "Client ID", + description: "The ID of the client", + async options() { + const clients = await this.listClients(); + return clients?.map(({ + ClientID: value, Name: label, + }) => ({ + value, + label, + })) || []; + }, + }, + campaignId: { + type: "string", + label: "Campaign ID", + description: "The ID of a sent or scheduled campaign", + async options({ clientId }) { + const campaigns = await this.listCampaigns(clientId); + return campaigns?.map(({ + CampaignID: value, Name: label, + }) => ({ + value, + label, + })) || []; + }, + }, + subscriber: { + type: "string", + label: "Subscriber", + description: "The email address of the subscriber", + async options({ + listId, page, + }) { + const { Results: subscribers } = await this.listSubscribers({ + listId, + params: { + page: page + 1, + }, + }); + return subscribers?.map(({ + EmailAddress: value, Name: label, + }) => ({ + value, + label, + })) || []; + }, + }, + listId: { + type: "string", + label: "List ID", + description: "The ID of the list", + async options({ clientId }) { + const lists = await this.listLists({ + clientId, + }); + return lists?.map(({ + ListID: value, Name: label, + }) => ({ + value, + label, + })) || []; + }, + }, + smartEmailId: { + type: "string", + label: "Smart Email ID", + description: "The ID of the smart email to send", + async options({ clientId }) { + const emails = await this.listSmartEmails({ + params: { + clientId, + }, + }); + return emails?.map(({ + ID: value, Name: label, + }) => ({ + value, + label, + })) || []; + }, + }, + consentToTrack: { + type: "string", + label: "Consent to Track", + description: "Whether the subscriber has given permission to have their email opens and clicks tracked", + options: [ + "Yes", + "No", + "Unchanged", + ], + default: "Unchanged", + optional: true, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://api.createsend.com/api/v3.3"; + }, + async _makeRequest(opts = {}) { + const { + $ = this, + path, + ...otherOpts + } = opts; + const requestFn = async () => { + return await axios($, { + ...otherOpts, + url: `${this._baseUrl()}${path}`, + headers: { + Authorization: `Bearer ${this.$auth.oauth_access_token}`, + }, + }); + }; + return await this.retryWithExponentialBackoff(requestFn); + }, + // The API has been observed to occasionally return - + // {"Code":120,"Message":"Invalid OAuth Token"} + // Retry if a 401 Unauthorized or 429 (Rate limit exceeded) + // status is returned + async retryWithExponentialBackoff(requestFn, retries = 3, backoff = 500) { + try { + return await requestFn(); + } catch (error) { + if (retries > 0 && (error.response?.status === 401 || error.response?.status === 429)) { + await new Promise((resolve) => setTimeout(resolve, backoff)); + return this.retryWithExponentialBackoff(requestFn, retries - 1, backoff * 2); + } + throw error; + } + }, + listClients(opts = {}) { + return this._makeRequest({ + path: "/clients.json", + ...opts, + }); + }, + async listCampaigns(clientId) { + const scheduledCampaigns = await this.listScheduledCampaigns({ + clientId, + }); + const { Results: sentCampaigns } = await this.listSentCampaigns({ + clientId, + params: { + pagesize: DEFAULT_PAGE_SIZE, + }, + }); + return [ + ...scheduledCampaigns, + ...sentCampaigns, + ]; + }, + listSentCampaigns({ + clientId, ...opts + }) { + return this._makeRequest({ + path: `/clients/${clientId}/campaigns.json`, + ...opts, + }); + }, + listScheduledCampaigns({ + clientId, ...opts + }) { + return this._makeRequest({ + path: `/clients/${clientId}/scheduled.json`, + ...opts, + }); + }, + listBounces({ + campaignId, ...opts + }) { + return this._makeRequest({ + path: `/campaigns/${campaignId}/bounces.json`, + ...opts, + }); + }, + listOpens({ + campaignId, ...opts + }) { + return this._makeRequest({ + path: `/campaigns/${campaignId}/opens.json`, + ...opts, + }); + }, + listSubscribers({ + listId, ...opts + }) { + return this._makeRequest({ + path: `/lists/${listId}/active.json`, + ...opts, + }); + }, + listLists({ + clientId, ...opts + }) { + return this._makeRequest({ + path: `/clients/${clientId}/lists.json`, + ...opts, + }); + }, + listSmartEmails(opts = {}) { + return this._makeRequest({ + path: "/transactional/smartEmail", + ...opts, + }); + }, + sendSmartEmail({ + smartEmailId, ...opts + }) { + return this._makeRequest({ + method: "POST", + path: `/transactional/smartEmail/${smartEmailId}/send`, + ...opts, + }); + }, + unsubscribeSubscriber({ + listId, ...opts + }) { + return this._makeRequest({ + method: "POST", + path: `/subscribers/${listId}/unsubscribe.json`, + ...opts, + }); + }, + createSubscriber({ + listId, ...opts + }) { + return this._makeRequest({ + method: "POST", + path: `/subscribers/${listId}.json`, + ...opts, + }); + }, + async *paginate({ + fn, + args, + max, + }) { + args = { + ...args, + params: { + ...args?.params, + pagesize: DEFAULT_PAGE_SIZE, + }, + }; + let hasMore, count = 0; + do { + const { + Results: results, NumberOfPages: numPages, + } = await fn(args); + for (const item of results) { + yield item; + if (max && ++count >= max) { + return; + } + } + hasMore = args.params.page < numPages; + args.params.page++; + } while (hasMore); }, }, -}; \ No newline at end of file +}; diff --git a/components/campaign_monitor/package.json b/components/campaign_monitor/package.json index d3f633809b89e..2a5c8328cfbcc 100644 --- a/components/campaign_monitor/package.json +++ b/components/campaign_monitor/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/campaign_monitor", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Campaign Monitor Components", "main": "campaign_monitor.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } -} \ No newline at end of file +} diff --git a/components/campaign_monitor/sources/common/base.mjs b/components/campaign_monitor/sources/common/base.mjs new file mode 100644 index 0000000000000..df120d4c3fe96 --- /dev/null +++ b/components/campaign_monitor/sources/common/base.mjs @@ -0,0 +1,82 @@ +import campaignMonitor from "../../campaign_monitor.app.mjs"; +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; + +export default { + props: { + campaignMonitor, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + clientId: { + propDefinition: [ + campaignMonitor, + "clientId", + ], + }, + }, + hooks: { + async deploy() { + await this.processEvent(25); + }, + }, + methods: { + _getLastTs() { + return this.db.get("lastTs") || 0; + }, + _setLastTs(lastTs) { + this.db.set("lastTs", lastTs); + }, + getArgs() { + return {}; + }, + getTsField() { + return "Date"; + }, + getResourceFn() { + throw new Error("getResourceFn is not implemented"); + }, + generateMeta() { + throw new Error("generateMeta is not implemented"); + }, + async processEvent(max) { + const lastTs = this._getLastTs(); + const fn = this.getResourceFn(); + const args = this.getArgs(); + const tsField = this.getTsField(); + + const results = this.campaignMonitor.paginate({ + fn, + args, + max, + }); + + const items = []; + for await (const item of results) { + const ts = Date.parse(item[tsField]); + if (ts >= lastTs) { + items.push(item); + } else { + break; + } + } + + if (!items?.length) { + return; + } + + this._setLastTs(Date.parse(items[0][tsField])); + + items.forEach((item) => { + const meta = this.generateMeta(item); + this.$emit(item, meta); + }); + }, + }, + async run() { + await this.processEvent(); + }, +}; diff --git a/components/campaign_monitor/sources/new-bounce/new-bounce.mjs b/components/campaign_monitor/sources/new-bounce/new-bounce.mjs new file mode 100644 index 0000000000000..868af3f4d7f31 --- /dev/null +++ b/components/campaign_monitor/sources/new-bounce/new-bounce.mjs @@ -0,0 +1,46 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "campaign_monitor-new-bounce", + name: "New Bounce", + description: "Emit new event when a campaign email bounces", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + campaignId: { + propDefinition: [ + common.props.campaignMonitor, + "campaignId", + (c) => ({ + clientId: c.clientId, + }), + ], + }, + }, + methods: { + ...common.methods, + getResourceFn() { + return this.campaignMonitor.listBounces; + }, + getArgs() { + return { + campaignId: this.campaignId, + params: { + orderfield: "date", + orderdirection: "desc", + }, + }; + }, + generateMeta(bounce) { + const ts = Date.parse(bounce[this.getTsField()]); + return { + id: `${bounce.EmailAddress}-${ts}`, + summary: `New Bounce: ${bounce.EmailAddress}`, + ts, + }; + }, + }, +}; diff --git a/components/campaign_monitor/sources/new-email-open/new-email-open.mjs b/components/campaign_monitor/sources/new-email-open/new-email-open.mjs new file mode 100644 index 0000000000000..199bf6ed132ee --- /dev/null +++ b/components/campaign_monitor/sources/new-email-open/new-email-open.mjs @@ -0,0 +1,46 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "campaign_monitor-new-email-open", + name: "New Email Open", + description: "Emit new event when an email from a campaign is opened", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + campaignId: { + propDefinition: [ + common.props.campaignMonitor, + "campaignId", + (c) => ({ + clientId: c.clientId, + }), + ], + }, + }, + methods: { + ...common.methods, + getResourceFn() { + return this.campaignMonitor.listOpens; + }, + getArgs() { + return { + campaignId: this.campaignId, + params: { + orderfield: "date", + orderdirection: "desc", + }, + }; + }, + generateMeta(open) { + const ts = Date.parse(open[this.getTsField()]); + return { + id: `${open.EmailAddress}-${ts}`, + summary: `New Email Open: ${open.EmailAddress}`, + ts, + }; + }, + }, +}; diff --git a/components/campaign_monitor/sources/new-subscriber/new-subscriber.mjs b/components/campaign_monitor/sources/new-subscriber/new-subscriber.mjs new file mode 100644 index 0000000000000..5149782b83839 --- /dev/null +++ b/components/campaign_monitor/sources/new-subscriber/new-subscriber.mjs @@ -0,0 +1,45 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "campaign_monitor-new-subscriber", + name: "New Subscriber Added", + description: "Emit new event when a new subscriber is added to a specific list", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + listId: { + propDefinition: [ + common.props.campaignMonitor, + "listId", + (c) => ({ + clientId: c.clientId, + }), + ], + }, + }, + methods: { + ...common.methods, + getResourceFn() { + return this.campaignMonitor.listSubscribers; + }, + getArgs() { + return { + listId: this.listId, + params: { + orderfield: "date", + orderdirection: "desc", + }, + }; + }, + generateMeta(subscriber) { + return { + id: subscriber.EmailAddress, + summary: `New Subscriber: ${subscriber.EmailAddress}`, + ts: Date.parse(subscriber[this.getTsField()]), + }; + }, + }, +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f7315a9f70c3f..d5a384ebbea32 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1485,7 +1485,10 @@ importers: '@pipedream/platform': 1.5.1 components/campaign_monitor: - specifiers: {} + specifiers: + '@pipedream/platform': ^3.0.3 + dependencies: + '@pipedream/platform': 3.0.3 components/campaignhq: specifiers: {}