Skip to content

Commit 57cd483

Browse files
committed
elastic_email init
1 parent 6e2214c commit 57cd483

File tree

8 files changed

+617
-1
lines changed

8 files changed

+617
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import elastic_email from "../../elastic_email.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "elastic_email-add-contact",
6+
name: "Add Contact to Mailing List",
7+
description: "Adds a new contact to a mailing list. [See the documentation]()",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
elastic_email: {
12+
type: "app",
13+
app: "elastic_email",
14+
},
15+
addContactEmail: {
16+
propDefinition: [
17+
elastic_email,
18+
"addContactEmail",
19+
],
20+
},
21+
addContactListName: {
22+
propDefinition: [
23+
elastic_email,
24+
"addContactListName",
25+
],
26+
optional: true,
27+
},
28+
},
29+
async run({ $ }) {
30+
const response = await this.elastic_email.addContact();
31+
if (this.addContactListName) {
32+
$.export(
33+
"$summary",
34+
`Successfully added contact ${this.addContactEmail} to the mailing list ${this.addContactListName}`,
35+
);
36+
} else {
37+
$.export(
38+
"$summary",
39+
`Successfully added contact ${this.addContactEmail} to the mailing list`,
40+
);
41+
}
42+
return response;
43+
},
44+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import elasticEmail from "../../elastic_email.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "elastic_email-send-email",
6+
name: "Send Email",
7+
description: "Sends an email to one or more recipients. [See the documentation]()",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
elastic_email,
12+
sendEmailRecipients: {
13+
propDefinition: [
14+
"elastic_email",
15+
"sendEmailRecipients",
16+
],
17+
},
18+
sendEmailSubject: {
19+
propDefinition: [
20+
"elastic_email",
21+
"sendEmailSubject",
22+
],
23+
},
24+
sendEmailBody: {
25+
propDefinition: [
26+
"elastic_email",
27+
"sendEmailBody",
28+
],
29+
},
30+
},
31+
async run({ $ }) {
32+
const response = await this.elastic_email.sendBulkEmails();
33+
$.export("$summary", `Emails sent successfully to ${this.sendEmailRecipients.join(", ")}`);
34+
return response;
35+
},
36+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import elastic_email from "../../elastic_email.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "elastic_email-unsubscribe-contact",
6+
name: "Unsubscribe Contact",
7+
description: "Unsubscribes a contact from future emails. [See the documentation]()",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
elastic_email,
12+
unsubscribeEmail: {
13+
propDefinition: [
14+
"elastic_email",
15+
"unsubscribeEmail",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const response = await this.elastic_email.unsubscribeContact();
21+
$.export("$summary", `Unsubscribed contact ${this.unsubscribeEmail} successfully`);
22+
return response;
23+
},
24+
};

components/elastic_email/app/elastic_email.app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ export default defineApp({
1010
console.log(Object.keys(this.$auth));
1111
},
1212
},
13-
});
13+
});
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import { axios } from "@pipedream/platform";
2+
3+
export default {
4+
type: "app",
5+
app: "elastic_email",
6+
version: "0.0.{{ts}}",
7+
propDefinitions: {
8+
sendEmailRecipients: {
9+
type: "string[]",
10+
label: "Recipient Email Addresses",
11+
description: "Email addresses of the recipients",
12+
},
13+
sendEmailSubject: {
14+
type: "string",
15+
label: "Email Subject",
16+
description: "Subject of the email",
17+
},
18+
sendEmailBody: {
19+
type: "string",
20+
label: "Email Body",
21+
description: "Body content of the email",
22+
},
23+
addContactEmail: {
24+
type: "string",
25+
label: "Contact Email Address",
26+
description: "Email address of the contact to add",
27+
},
28+
addContactListName: {
29+
type: "string",
30+
label: "Mailing List Name",
31+
description: "Name of the mailing list to add the contact to",
32+
optional: true,
33+
},
34+
unsubscribeEmail: {
35+
type: "string",
36+
label: "Email Address to Unsubscribe",
37+
description: "Email address of the contact to unsubscribe",
38+
},
39+
webhookUrl: {
40+
type: "string",
41+
label: "Webhook URL",
42+
description: "URL to receive webhook events from Elastic Email",
43+
optional: true,
44+
},
45+
listNames: {
46+
type: "string[]",
47+
label: "List Names",
48+
description: "Names of the mailing lists",
49+
optional: true,
50+
},
51+
},
52+
methods: {
53+
_baseUrl() {
54+
return "https://api.elasticemail.com/v4";
55+
},
56+
async _makeRequest(opts = {}) {
57+
const {
58+
$ = this,
59+
method = "GET",
60+
path = "/",
61+
headers,
62+
params,
63+
data,
64+
...otherOpts
65+
} = opts;
66+
return axios($, {
67+
...otherOpts,
68+
method,
69+
url: this._baseUrl() + path,
70+
headers: {
71+
...headers,
72+
"Content-Type": "application/json",
73+
"X-ElasticEmail-ApiKey": this.$auth.api_key,
74+
},
75+
params,
76+
data,
77+
});
78+
},
79+
async sendBulkEmails(opts = {}) {
80+
const recipients = this.sendEmailRecipients.map((email) => ({
81+
Email: email,
82+
}));
83+
const data = {
84+
Recipients: recipients,
85+
Content: {
86+
Body: this.sendEmailBody,
87+
Subject: this.sendEmailSubject,
88+
From: this.$auth.from_email,
89+
IsPlainText: false,
90+
},
91+
Options: {
92+
// Add any additional email options here if needed
93+
},
94+
};
95+
return this._makeRequest({
96+
method: "POST",
97+
path: "/emails",
98+
data,
99+
...opts,
100+
});
101+
},
102+
async sendTransactionalEmails(opts = {}) {
103+
const recipients = this.sendEmailRecipients.map((email) => ({
104+
Email: email,
105+
}));
106+
const data = {
107+
Recipients: recipients,
108+
Content: {
109+
Body: this.sendEmailBody,
110+
Subject: this.sendEmailSubject,
111+
From: this.$auth.from_email,
112+
IsPlainText: false,
113+
},
114+
Options: {
115+
// Add any additional transactional email options here if needed
116+
},
117+
};
118+
return this._makeRequest({
119+
method: "POST",
120+
path: "/emails/transactional",
121+
data,
122+
...opts,
123+
});
124+
},
125+
async addContact(opts = {}) {
126+
const data = [
127+
{
128+
Email: this.addContactEmail,
129+
Status: "Active",
130+
...(this.addContactListName && {
131+
Lists: [
132+
this.addContactListName,
133+
],
134+
}),
135+
},
136+
];
137+
return this._makeRequest({
138+
method: "POST",
139+
path: "/contacts",
140+
data,
141+
...opts,
142+
});
143+
},
144+
async unsubscribeContact(opts = {}) {
145+
const data = [
146+
{
147+
Email: this.unsubscribeEmail,
148+
Status: "Unsubscribed",
149+
},
150+
];
151+
return this._makeRequest({
152+
method: "POST",
153+
path: "/contacts",
154+
data,
155+
...opts,
156+
});
157+
},
158+
async registerWebhook(opts = {}) {
159+
if (!this.webhookUrl) {
160+
throw new Error("Webhook URL is required to register a webhook.");
161+
}
162+
const data = {
163+
Events: [
164+
"Open",
165+
"Click",
166+
"ContactAdded",
167+
],
168+
Url: this.webhookUrl,
169+
};
170+
return this._makeRequest({
171+
method: "POST",
172+
path: "/webhooks",
173+
data,
174+
...opts,
175+
});
176+
},
177+
async paginate(fn, ...opts) {
178+
const results = [];
179+
let hasMore = true;
180+
let page = 0;
181+
while (hasMore) {
182+
const response = await fn({
183+
...opts,
184+
page,
185+
});
186+
if (!response || response.length === 0) {
187+
hasMore = false;
188+
} else {
189+
results.push(...response);
190+
page += 1;
191+
}
192+
}
193+
return results;
194+
},
195+
},
196+
};

0 commit comments

Comments
 (0)