Skip to content

Commit 9d9f86e

Browse files
committed
selzy init
1 parent ae1b6f8 commit 9d9f86e

File tree

7 files changed

+603
-6
lines changed

7 files changed

+603
-6
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import selzy from "../../selzy.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "selzy-create-email-campaign",
6+
name: "Create Email Campaign",
7+
description: "Creates a new email campaign. [See the documentation](https://selzy.com/en/support/api/messages/createcampaign/)",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
selzy,
12+
subject: {
13+
propDefinition: [
14+
selzy,
15+
"subject",
16+
],
17+
},
18+
senderName: {
19+
propDefinition: [
20+
selzy,
21+
"senderName",
22+
],
23+
},
24+
senderEmail: {
25+
propDefinition: [
26+
selzy,
27+
"senderEmail",
28+
],
29+
},
30+
messageContent: {
31+
propDefinition: [
32+
selzy,
33+
"messageContent",
34+
],
35+
},
36+
listId: {
37+
propDefinition: [
38+
selzy,
39+
"listId",
40+
],
41+
},
42+
},
43+
async run({ $ }) {
44+
const response = await this.selzy.createCampaign({
45+
subject: this.subject,
46+
sender_name: this.senderName,
47+
sender_email: this.senderEmail,
48+
message_content: this.messageContent,
49+
list_id: this.listId,
50+
});
51+
52+
$.export("$summary", `Successfully created email campaign with ID: ${response.campaign_id}`);
53+
return response;
54+
},
55+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import selzy from "../../selzy.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "selzy-create-email-message",
6+
name: "Create Email Message",
7+
description: "Adds a new email message. [See the documentation](https://selzy.com/en/support/category/api/messages/)",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
selzy,
12+
senderName: {
13+
propDefinition: [
14+
selzy,
15+
"senderName",
16+
],
17+
},
18+
senderEmail: {
19+
propDefinition: [
20+
selzy,
21+
"senderEmail",
22+
],
23+
},
24+
subject: {
25+
propDefinition: [
26+
selzy,
27+
"subject",
28+
],
29+
},
30+
body: {
31+
propDefinition: [
32+
selzy,
33+
"body",
34+
],
35+
},
36+
listId: {
37+
propDefinition: [
38+
selzy,
39+
"listId",
40+
],
41+
},
42+
name: {
43+
type: "string",
44+
label: "Name",
45+
description: "The optional name for the email message",
46+
optional: true,
47+
},
48+
customFields: {
49+
type: "object",
50+
label: "Custom Fields",
51+
description: "Optional custom fields for the email message",
52+
optional: true,
53+
},
54+
},
55+
async run({ $ }) {
56+
const emailMessageData = {
57+
sender_name: this.senderName,
58+
sender_email: this.senderEmail,
59+
subject: this.subject,
60+
body: this.body,
61+
list_id: this.listId,
62+
};
63+
64+
if (this.name) {
65+
emailMessageData.name = this.name;
66+
}
67+
68+
if (this.customFields) {
69+
Object.assign(emailMessageData, this.customFields);
70+
}
71+
72+
const response = await this.selzy.createEmailMessage(emailMessageData);
73+
74+
$.export("$summary", `Email message created successfully with ID ${response.message_id}.`);
75+
return response;
76+
},
77+
};

components/selzy/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
"publishConfig": {
1313
"access": "public"
1414
}
15-
}
15+
}

components/selzy/selzy.app.mjs

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,129 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "selzy",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
listId: {
8+
type: "string",
9+
label: "List ID",
10+
description: "Select or enter the List ID to monitor or target",
11+
async options() {
12+
const lists = await this.getLists();
13+
return lists.map((list) => ({
14+
label: list.name,
15+
value: list.id,
16+
}));
17+
},
18+
},
19+
campaignId: {
20+
type: "string",
21+
label: "Campaign ID",
22+
description: "Select or enter the Campaign ID to monitor",
23+
async options() {
24+
const campaigns = await this.getCampaigns();
25+
return campaigns.map((campaign) => ({
26+
label: campaign.name,
27+
value: campaign.id,
28+
}));
29+
},
30+
},
31+
senderEmail: {
32+
type: "string",
33+
label: "Sender Email",
34+
description: "The sender's email address",
35+
},
36+
senderName: {
37+
type: "string",
38+
label: "Sender Name",
39+
description: "The sender's name",
40+
},
41+
subject: {
42+
type: "string",
43+
label: "Subject",
44+
description: "The subject of the email",
45+
},
46+
body: {
47+
type: "string",
48+
label: "Body",
49+
description: "The HTML body of the email",
50+
},
51+
messageContent: {
52+
type: "string",
53+
label: "Message Content",
54+
description: "Content of the email message",
55+
},
56+
},
557
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
58+
_baseUrl() {
59+
return "https://api.selzy.com/en/api";
60+
},
61+
async _makeRequest(opts = {}) {
62+
const {
63+
$ = this,
64+
method = "GET",
65+
path = "/",
66+
headers,
67+
...otherOpts
68+
} = opts;
69+
return axios($, {
70+
...otherOpts,
71+
method,
72+
url: `${this._baseUrl()}${path}`,
73+
headers: {
74+
...headers,
75+
api_key: this.$auth.api_key,
76+
},
77+
});
78+
},
79+
async getLists(opts = {}) {
80+
return this._makeRequest({
81+
path: "/lists",
82+
...opts,
83+
});
84+
},
85+
async getCampaigns(opts = {}) {
86+
return this._makeRequest({
87+
path: "/campaigns",
88+
...opts,
89+
});
90+
},
91+
async createEmailMessage(opts = {}) {
92+
return this._makeRequest({
93+
method: "POST",
94+
path: "/createEmailMessage",
95+
data: {
96+
sender_name: this.senderName,
97+
sender_email: this.senderEmail,
98+
subject: this.subject,
99+
body: this.body,
100+
list_id: this.listId,
101+
...opts,
102+
},
103+
});
104+
},
105+
async createCampaign(opts = {}) {
106+
return this._makeRequest({
107+
method: "POST",
108+
path: "/createCampaign",
109+
data: {
110+
subject: this.subject,
111+
sender_name: this.senderName,
112+
sender_email: this.senderEmail,
113+
message_content: this.messageContent,
114+
list_id: this.listId,
115+
...opts,
116+
},
117+
});
118+
},
119+
async subscribeToNewContact(listId) {
120+
// Logic for webhook or polling to emit new event on new contact subscription
121+
},
122+
async subscribeToNewCampaign() {
123+
// Logic for webhook or polling to emit new event when a new campaign is created
124+
},
125+
async subscribeToCampaignStatusChange(campaignId) {
126+
// Logic for webhook or polling to emit new event on campaign status change
9127
},
10128
},
11-
};
129+
};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import selzy from "../../selzy.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
import crypto from "crypto";
4+
5+
export default {
6+
key: "selzy-new-campaign-instant",
7+
name: "New Campaign Created",
8+
description: "Emit new event when a new email campaign is created. Useful for monitoring campaign creation activity. [See the documentation](https://selzy.com/en/support/category/api/)",
9+
version: "0.0.{{ts}}",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
selzy,
14+
http: {
15+
type: "$.interface.http",
16+
customResponse: true,
17+
},
18+
db: "$.service.db",
19+
},
20+
methods: {
21+
_getWebhookId() {
22+
return this.db.get("webhookId");
23+
},
24+
_setWebhookId(id) {
25+
this.db.set("webhookId", id);
26+
},
27+
},
28+
hooks: {
29+
async deploy() {
30+
const campaigns = await this.selzy.getCampaigns({
31+
paginate: true,
32+
max: 50,
33+
});
34+
for (const campaign of campaigns) {
35+
this.$emit(campaign, {
36+
id: campaign.id,
37+
summary: `New campaign created: ${campaign.name}`,
38+
ts: Date.parse(campaign.createdAt),
39+
});
40+
}
41+
},
42+
async activate() {
43+
const response = await this.selzy._makeRequest({
44+
method: "POST",
45+
path: "/setHook",
46+
data: {
47+
api_key: this.selzy.$auth.api_key,
48+
hook_url: this.http.endpoint,
49+
event_format: "json_post",
50+
events: [
51+
"campaign_status",
52+
],
53+
status: "active",
54+
},
55+
});
56+
this._setWebhookId(response.hook_id);
57+
},
58+
async deactivate() {
59+
const webhookId = this._getWebhookId();
60+
if (webhookId) {
61+
await this.selzy._makeRequest({
62+
method: "POST",
63+
path: "/removeHook",
64+
data: {
65+
hook_id: webhookId,
66+
},
67+
});
68+
}
69+
},
70+
},
71+
async run(event) {
72+
const signature = event.headers["x-selzy-signature"];
73+
const secret = this.selzy.$auth.api_key;
74+
const computedSignature = crypto.createHmac("sha256", secret)
75+
.update(event.body)
76+
.digest("base64");
77+
78+
if (computedSignature !== signature) {
79+
this.http.respond({
80+
status: 401,
81+
body: "Unauthorized",
82+
});
83+
return;
84+
}
85+
86+
const { events_by_user } = event.body;
87+
for (const userEvents of events_by_user) {
88+
for (const eventData of userEvents.Events) {
89+
if (eventData.event_name === "campaign_status") {
90+
this.$emit(eventData, {
91+
id: eventData.event_data.campaign_id,
92+
summary: `New campaign status: ${eventData.event_data.status}`,
93+
ts: Date.parse(eventData.event_time),
94+
});
95+
}
96+
}
97+
}
98+
99+
this.http.respond({
100+
status: 200,
101+
body: "OK",
102+
});
103+
},
104+
};

0 commit comments

Comments
 (0)