Skip to content

Commit dd0bef4

Browse files
New Components - wati (#14276)
* wati init * [Components] wati #13213 Actions - Add Contact - Send Template Message - Update Contact Attribute Source - New Contact Created - New Incoming Message * pnpm update * Update components/wati/wati.app.mjs Co-authored-by: michelle0927 <[email protected]> * some adjusts --------- Co-authored-by: michelle0927 <[email protected]>
1 parent 7b02b2f commit dd0bef4

File tree

13 files changed

+522
-21
lines changed

13 files changed

+522
-21
lines changed

components/wati/.gitignore

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import wati from "../../wati.app.mjs";
3+
4+
export default {
5+
key: "wati-add-contact",
6+
name: "Add Contact",
7+
description: "Adds a new contact on the WATI platform. [See the documentation](https://docs.wati.io/reference/post_api-v1-addcontact-whatsappnumber)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
wati,
12+
whatsappNumber: {
13+
propDefinition: [
14+
wati,
15+
"whatsappNumber",
16+
],
17+
},
18+
name: {
19+
type: "string",
20+
label: "Name",
21+
description: "The name of the contact",
22+
optional: true,
23+
},
24+
customParams: {
25+
propDefinition: [
26+
wati,
27+
"customParams",
28+
],
29+
optional: true,
30+
},
31+
},
32+
async run({ $ }) {
33+
const response = await this.wati.addContact({
34+
$,
35+
whatsappNumber: this.whatsappNumber,
36+
data: {
37+
name: this.name,
38+
customParams: this.customParams && Object.entries(this.customParams).map(([
39+
key,
40+
value,
41+
]) => ({
42+
name: key,
43+
value,
44+
})),
45+
},
46+
});
47+
if (!response.result) {
48+
throw new ConfigurationError(response.info);
49+
}
50+
$.export("$summary", `Successfully added contact with phone number: ${this.whatsappNumber}`);
51+
return response;
52+
},
53+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import wati from "../../wati.app.mjs";
3+
4+
export default {
5+
key: "wati-send-template-message",
6+
name: "Send WhatsApp Template Message",
7+
description: "Enables sending of WhatsApp messages using a pre-approved template. [See the documentation](https://docs.wati.io/reference/post_api-v2-sendtemplatemessage)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
wati,
12+
whatsappNumber: {
13+
propDefinition: [
14+
wati,
15+
"whatsappNumber",
16+
],
17+
},
18+
customParams: {
19+
propDefinition: [
20+
wati,
21+
"customParams",
22+
],
23+
label: "Parameters",
24+
description: "An object with template's custom params.",
25+
},
26+
templateName: {
27+
propDefinition: [
28+
wati,
29+
"templateName",
30+
],
31+
},
32+
broadcastName: {
33+
type: "string",
34+
label: "Broadcast Name",
35+
description: "The name of broadcast.",
36+
},
37+
},
38+
async run({ $ }) {
39+
const response = await this.wati.sendTemplateMessage({
40+
$,
41+
params: {
42+
whatsappNumber: this.whatsappNumber,
43+
},
44+
data: {
45+
parameters: this.customParams && Object.entries(this.customParams).map(([
46+
key,
47+
value,
48+
]) => ({
49+
name: key,
50+
value,
51+
})),
52+
template_name: this.templateName,
53+
broadcast_name: this.broadcastName,
54+
},
55+
});
56+
if (!response.result) {
57+
throw new ConfigurationError(response.info);
58+
}
59+
60+
$.export("$summary", `Successfully sent template message to ${this.whatsappNumber}`);
61+
return response;
62+
},
63+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import wati from "../../wati.app.mjs";
3+
4+
export default {
5+
key: "wati-update-contact-attribute",
6+
name: "Update Contact Attribute",
7+
description: "Allows updating attributes/tags related to an existing contact. [See the documentation](https://docs.wati.io/reference/post_api-v1-updatecontactattributes-whatsappnumber)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
wati,
12+
whatsappNumber: {
13+
propDefinition: [
14+
wati,
15+
"whatsappNumber",
16+
],
17+
},
18+
customParams: {
19+
propDefinition: [
20+
wati,
21+
"customParams",
22+
],
23+
optional: true,
24+
},
25+
},
26+
async run({ $ }) {
27+
const response = await this.wati.updateContactAttributes({
28+
$,
29+
whatsappNumber: this.whatsappNumber,
30+
data: {
31+
customParams: this.customParams && Object.entries(this.customParams).map(([
32+
key,
33+
value,
34+
]) => ({
35+
name: key,
36+
value,
37+
})),
38+
},
39+
});
40+
if (!response.result) {
41+
throw new ConfigurationError(response.info);
42+
}
43+
44+
$.export("$summary", `Successfully updated attributes for contact ${this.whatsappNumber}`);
45+
return response;
46+
},
47+
};

components/wati/app/wati.app.ts

-13
This file was deleted.

components/wati/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
{
22
"name": "@pipedream/wati",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream WATI Components",
5-
"main": "dist/app/wati.app.mjs",
5+
"main": "wati.app.mjs",
66
"keywords": [
77
"pipedream",
88
"wati"
99
],
10-
"files": ["dist"],
1110
"homepage": "https://pipedream.com/apps/wati",
1211
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1312
"publishConfig": {
1413
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1517
}
16-
}
18+
}
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import wati from "../../wati.app.mjs";
3+
4+
export default {
5+
props: {
6+
wati,
7+
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
14+
},
15+
methods: {
16+
prepareData(data) {
17+
return data;
18+
},
19+
_getLastDate() {
20+
return this.db.get("lastDate") || 0;
21+
},
22+
_setLastDate(lastDate) {
23+
this.db.set("lastDate", lastDate);
24+
},
25+
async emitEvent(maxResults = false) {
26+
const lastDate = this._getLastDate();
27+
const dateField = this.getDateField();
28+
29+
const response = this.wati.paginate(
30+
this.getPaginateOpts(maxResults),
31+
);
32+
33+
let responseArray = [];
34+
for await (const item of response) {
35+
responseArray.push(item);
36+
}
37+
38+
responseArray = this.prepareData(responseArray, lastDate, maxResults);
39+
40+
if (responseArray.length) {
41+
this._setLastDate(responseArray[0][dateField]);
42+
}
43+
44+
for (const item of responseArray.reverse()) {
45+
this.$emit(item, {
46+
id: item.id,
47+
summary: this.getSummary(item),
48+
ts: Date.parse(item[dateField]),
49+
});
50+
}
51+
},
52+
},
53+
hooks: {
54+
async deploy() {
55+
await this.emitEvent(25);
56+
},
57+
},
58+
async run() {
59+
await this.emitEvent();
60+
},
61+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "wati-new-contact-created",
7+
name: "New Contact Created",
8+
description: "Emit new event when a contact is created from an incoming WhatsApp message.",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...common.methods,
14+
getPaginateOpts(maxResults) {
15+
return {
16+
fn: this.wati.listContacts,
17+
itemsField: "result",
18+
optsField: "data",
19+
maxResults,
20+
};
21+
},
22+
getDateField() {
23+
return "created";
24+
},
25+
checkBreak(item, lastDate) {
26+
return Date.parse(item.created) < lastDate;
27+
},
28+
getSummary(item) {
29+
return `New contact created: ${item.wAid}`;
30+
},
31+
},
32+
sampleEmit,
33+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export default {
2+
"id": "670934c1d464c11dd46c3b7f",
3+
"wAid": "17759865200",
4+
"firstName": "+17759865200",
5+
"fullName": "+17759865200",
6+
"phone": "17759865200",
7+
"source": null,
8+
"contactStatus": "VALID",
9+
"photo": null,
10+
"created": "Oct-11-2024",
11+
"customParams": [
12+
{
13+
"name": "name",
14+
"value": "+17759865200"
15+
},
16+
{
17+
"name": "phone",
18+
"value": "17759865200"
19+
}
20+
],
21+
"optedIn": false,
22+
"isDeleted": false,
23+
"lastUpdated": "2024-10-11T15:09:36.047Z",
24+
"allowBroadcast": true,
25+
"allowSMS": true,
26+
"teamIds": [
27+
"6708393ad464c11dd46b3d73"
28+
],
29+
"isInFlow": false,
30+
"lastFlowId": null,
31+
"currentFlowNodeId": null,
32+
"selectedHubspotId": null
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "wati-new-incoming-message",
7+
name: "New Incoming Message",
8+
description: "Emit new event when there is an incoming message on your number.",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
...common.props,
14+
contactId: {
15+
propDefinition: [
16+
common.props.wati,
17+
"contactId",
18+
],
19+
},
20+
},
21+
methods: {
22+
...common.methods,
23+
getPaginateOpts() {
24+
return {
25+
fn: this.wati.listContactMessages,
26+
whatsappNumber: `+${this.contactId}`,
27+
itemsField: [
28+
"messages",
29+
],
30+
optsField: "params",
31+
};
32+
},
33+
getDateField() {
34+
return "timestamp";
35+
},
36+
prepareData(data, lastDate, maxResults) {
37+
data = data
38+
.filter((item) => item.statusString === "SENT" && Date.parse(item.created) > lastDate)
39+
.sort((a, b) => Date.parse(b.created) - Date.parse(a.created));
40+
41+
if (maxResults && data.length > maxResults) data.length = maxResults;
42+
return data;
43+
},
44+
checkBreak(item, lastDate) {
45+
return Date.parse(item.timestamp) < lastDate;
46+
},
47+
getSummary(item) {
48+
return `New message: ${item.text || "No content"}`;
49+
},
50+
},
51+
sampleEmit,
52+
};

0 commit comments

Comments
 (0)