Skip to content

Commit 44871be

Browse files
luancazarinemichelle0927GTFalcao
authored
New Components - whautomate (#13119)
* whautomate init * [Components] whautomate #13095 Sources - New Appointment Cancelled (Instant) - New Appointment Scheduled (Instant) - New Client Created (Instant) Actions - Assign Tags Contact - Create Contact - Send Whatsapp Template Message * pnpm update * Update components/whautomate/sources/common/base.mjs Co-authored-by: michelle0927 <[email protected]> * some adjusts * fix tags in data request --------- Co-authored-by: michelle0927 <[email protected]> Co-authored-by: Guilherme Falcão <[email protected]>
1 parent 4e2f97b commit 44871be

File tree

15 files changed

+652
-8
lines changed

15 files changed

+652
-8
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { parseObject } from "../../common/utils.mjs";
2+
import whautomate from "../../whautomate.app.mjs";
3+
4+
export default {
5+
key: "whautomate-assign-tags-contact",
6+
name: "Assign Tags to Contact",
7+
description: "Assign one or more tags to an existing contact. [See the documentation](https://help.whautomate.com/product-guides/whautomate-rest-api/contacts#/v1-contacts-contactid-1)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
whautomate,
12+
contactId: {
13+
propDefinition: [
14+
whautomate,
15+
"contactId",
16+
],
17+
},
18+
contactTags: {
19+
propDefinition: [
20+
whautomate,
21+
"contactTags",
22+
],
23+
},
24+
},
25+
async run({ $ }) {
26+
const contact = await this.whautomate.getContact(this.contactId);
27+
const response = await this.whautomate.updateContact({
28+
$,
29+
contactId: this.contactId,
30+
data: {
31+
...contact,
32+
tags: [
33+
...new Set([
34+
...(contact.tags
35+
? contact.tags
36+
: []),
37+
...parseObject(this.contactTags),
38+
]),
39+
],
40+
},
41+
});
42+
$.export("$summary", `Successfully assigned tags to contact ${this.contactId}`);
43+
return response;
44+
},
45+
};
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { STAGE_OPTIONS } from "../../common/constants.mjs";
2+
import { parseObject } from "../../common/utils.mjs";
3+
import whautomate from "../../whautomate.app.mjs";
4+
5+
export default {
6+
key: "whautomate-create-contact",
7+
name: "Create Contact",
8+
description: "Create a new contact associated with a WhatsApp number. [See the documentation](https://help.whautomate.com/product-guides/whautomate-rest-api/contacts#/v1-contacts-1)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
whautomate,
13+
name: {
14+
type: "string",
15+
label: "Name",
16+
description: "The name of the contact",
17+
},
18+
phoneNumber: {
19+
type: "string",
20+
label: "Phone Number",
21+
description: "The WhatsApp phone number of the contact. Format: +15555555555",
22+
},
23+
locationId: {
24+
propDefinition: [
25+
whautomate,
26+
"locationId",
27+
],
28+
},
29+
stage: {
30+
type: "string",
31+
label: "Stage",
32+
description: "The Contact Stage",
33+
optional: true,
34+
options: STAGE_OPTIONS,
35+
},
36+
tags: {
37+
propDefinition: [
38+
whautomate,
39+
"contactTags",
40+
],
41+
optional: true,
42+
},
43+
customFields: {
44+
type: "object",
45+
label: "Custom Fields",
46+
description: "The contact custom fields.",
47+
optional: true,
48+
},
49+
notes: {
50+
type: "string",
51+
label: "Notes",
52+
description: "The contact notes.",
53+
optional: true,
54+
},
55+
},
56+
async run({ $ }) {
57+
const {
58+
whautomate,
59+
locationId,
60+
tags,
61+
customFields,
62+
...data
63+
} = this;
64+
65+
const response = await whautomate.createContact({
66+
$,
67+
data: {
68+
...data,
69+
location: {
70+
id: locationId,
71+
},
72+
tags: parseObject(tags),
73+
customFields: parseObject(customFields),
74+
},
75+
});
76+
77+
$.export("$summary", `Successfully created contact with ID ${response.id}`);
78+
return response;
79+
},
80+
};
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import { parseObject } from "../../common/utils.mjs";
3+
import whautomate from "../../whautomate.app.mjs";
4+
5+
export default {
6+
key: "whautomate-send-whatsapp-template-message",
7+
name: "Send WhatsApp Template Message",
8+
description: "Send a pre-defined WhatsApp message template to a contact. [See the documentation](https://help.whautomate.com/product-guides/whautomate-rest-api/messages)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
whautomate,
13+
contactId: {
14+
propDefinition: [
15+
whautomate,
16+
"contactId",
17+
],
18+
},
19+
templateName: {
20+
type: "string",
21+
label: "Template Name",
22+
description: "The WhatsApp Template from your Whautomate Account.",
23+
},
24+
templateLanguage: {
25+
type: "string",
26+
label: "Template Language",
27+
description: "The language of the WhatsApp Template.",
28+
},
29+
locationId: {
30+
propDefinition: [
31+
whautomate,
32+
"locationId",
33+
],
34+
},
35+
headerMediaUrl: {
36+
type: "string",
37+
label: "Header Media URL",
38+
description: "The URL of the header media.",
39+
optional: true,
40+
},
41+
headerTextParameters: {
42+
type: "string[]",
43+
label: "Header Text Parameters",
44+
description: "The variables used in the header of your WhatsApp Template.",
45+
optional: true,
46+
},
47+
bodyTextParameters: {
48+
type: "string[]",
49+
label: "Body Text Parameters",
50+
description: "The variables used in the body of your WhatsApp Template.",
51+
optional: true,
52+
},
53+
buttonUrlParameters: {
54+
type: "string[]",
55+
label: "Button URL Parameters",
56+
description: "The placeholders used in the buttons of your WhatsApp Template.",
57+
optional: true,
58+
},
59+
},
60+
async run({ $ }) {
61+
const response = await this.whautomate.sendWhatsAppMessageTemplate({
62+
$,
63+
data: {
64+
contact: {
65+
id: this.contactId,
66+
},
67+
template: {
68+
name: this.templateName,
69+
language: this.templateLanguage,
70+
},
71+
location: {
72+
id: this.locationId,
73+
},
74+
headerMediaUrl: this.headerMediaUrl,
75+
headerTextParameters: parseObject(this.headerTextParameters),
76+
bodyTextParameters: parseObject(this.bodyTextParameters),
77+
buttonUrlParameters: parseObject(this.buttonUrlParameters),
78+
},
79+
});
80+
81+
if (response.error) throw new ConfigurationError(response.error);
82+
83+
$.export("$summary", `Successfully sent template message to ${this.locationId}`);
84+
return response;
85+
},
86+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const STAGE_OPTIONS = [
2+
"Subscriber",
3+
"Lead",
4+
"Marketing qualified lead (MQL)",
5+
"Sales qualified lead (SQL)",
6+
"Opportunity",
7+
"Customer",
8+
"Evangelist",
9+
"Other",
10+
];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (Array.isArray(obj)) {
5+
return obj.map((item) => {
6+
if (typeof item === "string") {
7+
try {
8+
return JSON.parse(item);
9+
} catch (e) {
10+
return item;
11+
}
12+
}
13+
return item;
14+
});
15+
}
16+
if (typeof obj === "string") {
17+
try {
18+
return JSON.parse(obj);
19+
} catch (e) {
20+
return obj;
21+
}
22+
}
23+
return obj;
24+
};

components/whautomate/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/whautomate",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Whautomate Components",
55
"main": "whautomate.app.mjs",
66
"keywords": [
@@ -11,5 +11,9 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.0"
1417
}
15-
}
18+
}
19+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import whautomate from "../../whautomate.app.mjs";
2+
3+
export default {
4+
props: {
5+
whautomate,
6+
http: {
7+
type: "$.interface.http",
8+
customResponse: false,
9+
},
10+
db: "$.service.db",
11+
name: {
12+
type: "string",
13+
label: "Name",
14+
description: "The name of the webhook.",
15+
},
16+
},
17+
methods: {
18+
_setWehookId(webhookId) {
19+
this.db.set("webhookId", webhookId);
20+
},
21+
_getWebhookId() {
22+
return this.db.get("webhookId");
23+
},
24+
},
25+
hooks: {
26+
async activate() {
27+
const data = await this.whautomate.createWebhook({
28+
data: {
29+
webhookHeaders: [
30+
{
31+
"key": "Content-Type",
32+
"value": "application/json",
33+
},
34+
],
35+
events: this.getEvent(),
36+
name: this.name,
37+
serverUrl: this.http.endpoint,
38+
active: true,
39+
},
40+
});
41+
this._setWehookId(data.id);
42+
},
43+
async deactivate() {
44+
const webhookId = this._getWebhookId();
45+
await this.whautomate.deleteWebhook(webhookId);
46+
},
47+
},
48+
async run({ body }) {
49+
if (body.event) {
50+
const ts = Date.parse(body.event.timeStamp);
51+
this.$emit(body, {
52+
id: body.event.id,
53+
summary: this.getSummary(body),
54+
ts,
55+
});
56+
}
57+
},
58+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "whautomate-new-appointment-cancelled-instant",
7+
name: "New Appointment Cancelled (Instant)",
8+
description: "Emit new event when an appointment is cancelled in Whautomate.",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...common.methods,
14+
getEvent() {
15+
return [
16+
"appointment_cancelled",
17+
];
18+
},
19+
getSummary(body) {
20+
return `Appointment cancelled: ${body.appointment.id}`;
21+
},
22+
},
23+
sampleEmit,
24+
};

0 commit comments

Comments
 (0)