Skip to content

Commit 4372e80

Browse files
committed
[Components] wati #13213
Actions - Add Contact - Send Template Message - Update Contact Attribute Source - New Contact Created - New Incoming Message
1 parent 1591b71 commit 4372e80

File tree

14 files changed

+410
-291
lines changed

14 files changed

+410
-291
lines changed

components/wati/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { ConfigurationError } from "@pipedream/platform";
12
import wati from "../../wati.app.mjs";
2-
import { axios } from "@pipedream/platform";
33

44
export default {
55
key: "wati-add-contact",
@@ -9,16 +9,45 @@ export default {
99
type: "action",
1010
props: {
1111
wati,
12-
contactDetails: {
12+
whatsappNumber: {
1313
propDefinition: [
1414
wati,
15-
"contactDetails",
15+
"whatsappNumber",
1616
],
1717
},
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+
},
1831
},
1932
async run({ $ }) {
20-
const response = await this.wati.addContact(this.contactDetails);
21-
$.export("$summary", `Successfully added contact with name: ${this.contactDetails.name}`);
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}`);
2251
return response;
2352
},
2453
};
Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,63 @@
1+
import { ConfigurationError } from "@pipedream/platform";
12
import wati from "../../wati.app.mjs";
2-
import { axios } from "@pipedream/platform";
33

44
export default {
55
key: "wati-send-template-message",
66
name: "Send WhatsApp Template Message",
77
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.{{ts}}",
8+
version: "0.0.1",
99
type: "action",
1010
props: {
1111
wati,
12-
contactDetails: {
12+
whatsappNumber: {
1313
propDefinition: [
1414
wati,
15-
"contactDetails",
15+
"whatsappNumber",
1616
],
1717
},
18-
templateDetails: {
18+
customParams: {
1919
propDefinition: [
2020
wati,
21-
"templateDetails",
21+
"customParams",
2222
],
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.",
2336
},
2437
},
2538
async run({ $ }) {
2639
const response = await this.wati.sendTemplateMessage({
27-
contactDetails: this.contactDetails,
28-
templateDetails: this.templateDetails,
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+
},
2955
});
56+
if (!response.result) {
57+
throw new ConfigurationError(response.info);
58+
}
3059

31-
$.export("$summary", `Successfully sent template message to ${this.contactDetails.name || this.contactDetails.number}`);
60+
$.export("$summary", `Successfully sent template message to ${this.whatsappNumber}`);
3261
return response;
3362
},
3463
};
Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
1+
import { ConfigurationError } from "@pipedream/platform";
12
import wati from "../../wati.app.mjs";
2-
import { axios } from "@pipedream/platform";
33

44
export default {
55
key: "wati-update-contact-attribute",
66
name: "Update Contact Attribute",
77
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.{{ts}}",
8+
version: "0.0.1",
99
type: "action",
1010
props: {
1111
wati,
12-
contactDetails: {
12+
whatsappNumber: {
1313
propDefinition: [
1414
wati,
15-
"contactDetails",
15+
"whatsappNumber",
1616
],
1717
},
18-
attributeDetails: {
18+
customParams: {
1919
propDefinition: [
2020
wati,
21-
"attributeDetails",
21+
"customParams",
2222
],
23+
optional: true,
2324
},
2425
},
2526
async run({ $ }) {
2627
const response = await this.wati.updateContactAttributes({
27-
contactDetails: this.contactDetails,
28-
attributeDetails: this.attributeDetails,
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+
},
2939
});
40+
if (!response.result) {
41+
throw new ConfigurationError(response.info);
42+
}
3043

31-
$.export("$summary", `Successfully updated attributes for contact ${this.contactDetails.name}`);
44+
$.export("$summary", `Successfully updated attributes for contact ${this.whatsappNumber}`);
3245
return response;
3346
},
3447
};

components/wati/app/wati.app.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

components/wati/package.json

Lines changed: 5 additions & 3 deletions
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
}
1618
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
getOpts() {
17+
return {};
18+
},
19+
filterItems() {
20+
() => true;
21+
},
22+
_getLastDate() {
23+
return this.db.get("lastDate") || 0;
24+
},
25+
_setLastDate(lastDate) {
26+
this.db.set("lastDate", lastDate);
27+
},
28+
async emitEvent(maxResults = false) {
29+
const lastDate = this._getLastDate();
30+
const dateField = this.getDateField();
31+
32+
const response = this.wati.paginate({
33+
fn: this.getFunction(),
34+
...this.getOpts(),
35+
itemsField: this.getItemsField(),
36+
maxResults,
37+
});
38+
39+
let responseArray = [];
40+
for await (const item of response) {
41+
if (this.checkBreak(item, lastDate)) break;
42+
responseArray.push(item);
43+
}
44+
45+
responseArray = responseArray.filter(this.filterItems);
46+
47+
if (responseArray.length) {
48+
this._setLastDate(responseArray[0][dateField]);
49+
}
50+
51+
for (const item of responseArray.reverse()) {
52+
this.$emit(item, {
53+
id: item.id,
54+
summary: this.getSummary(item),
55+
ts: Date.parse(item[dateField]),
56+
});
57+
}
58+
},
59+
},
60+
hooks: {
61+
async deploy() {
62+
await this.emitEvent(25);
63+
},
64+
},
65+
async run() {
66+
await this.emitEvent();
67+
},
68+
};

components/wati/sources/new-contact-created-instant/new-contact-created-instant.mjs

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
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+
getDateField() {
15+
return "created";
16+
},
17+
getItemsField() {
18+
return [
19+
"result",
20+
];
21+
},
22+
checkBreak(item, lastDate) {
23+
return Date.parse(item.created) < lastDate;
24+
},
25+
getFunction() {
26+
return this.wati.listContacts;
27+
},
28+
getSummary(item) {
29+
return `New contact created: ${item.wAid}`;
30+
},
31+
},
32+
sampleEmit,
33+
};

0 commit comments

Comments
 (0)