Skip to content

Commit

Permalink
New Components - wati (#14276)
Browse files Browse the repository at this point in the history
* 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 <michelle0927@users.noreply.github.com>

* some adjusts

---------

Co-authored-by: michelle0927 <michelle0927@users.noreply.github.com>
  • Loading branch information
luancazarine and michelle0927 authored Oct 16, 2024
1 parent 7b02b2f commit dd0bef4
Show file tree
Hide file tree
Showing 13 changed files with 522 additions and 21 deletions.
3 changes: 0 additions & 3 deletions components/wati/.gitignore

This file was deleted.

53 changes: 53 additions & 0 deletions components/wati/actions/add-contact/add-contact.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ConfigurationError } from "@pipedream/platform";
import wati from "../../wati.app.mjs";

export default {
key: "wati-add-contact",
name: "Add Contact",
description: "Adds a new contact on the WATI platform. [See the documentation](https://docs.wati.io/reference/post_api-v1-addcontact-whatsappnumber)",
version: "0.0.1",
type: "action",
props: {
wati,
whatsappNumber: {
propDefinition: [
wati,
"whatsappNumber",
],
},
name: {
type: "string",
label: "Name",
description: "The name of the contact",
optional: true,
},
customParams: {
propDefinition: [
wati,
"customParams",
],
optional: true,
},
},
async run({ $ }) {
const response = await this.wati.addContact({
$,
whatsappNumber: this.whatsappNumber,
data: {
name: this.name,
customParams: this.customParams && Object.entries(this.customParams).map(([
key,
value,
]) => ({
name: key,
value,
})),
},
});
if (!response.result) {
throw new ConfigurationError(response.info);
}
$.export("$summary", `Successfully added contact with phone number: ${this.whatsappNumber}`);
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ConfigurationError } from "@pipedream/platform";
import wati from "../../wati.app.mjs";

export default {
key: "wati-send-template-message",
name: "Send WhatsApp Template Message",
description: "Enables sending of WhatsApp messages using a pre-approved template. [See the documentation](https://docs.wati.io/reference/post_api-v2-sendtemplatemessage)",
version: "0.0.1",
type: "action",
props: {
wati,
whatsappNumber: {
propDefinition: [
wati,
"whatsappNumber",
],
},
customParams: {
propDefinition: [
wati,
"customParams",
],
label: "Parameters",
description: "An object with template's custom params.",
},
templateName: {
propDefinition: [
wati,
"templateName",
],
},
broadcastName: {
type: "string",
label: "Broadcast Name",
description: "The name of broadcast.",
},
},
async run({ $ }) {
const response = await this.wati.sendTemplateMessage({
$,
params: {
whatsappNumber: this.whatsappNumber,
},
data: {
parameters: this.customParams && Object.entries(this.customParams).map(([
key,
value,
]) => ({
name: key,
value,
})),
template_name: this.templateName,
broadcast_name: this.broadcastName,
},
});
if (!response.result) {
throw new ConfigurationError(response.info);
}

$.export("$summary", `Successfully sent template message to ${this.whatsappNumber}`);
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ConfigurationError } from "@pipedream/platform";
import wati from "../../wati.app.mjs";

export default {
key: "wati-update-contact-attribute",
name: "Update Contact Attribute",
description: "Allows updating attributes/tags related to an existing contact. [See the documentation](https://docs.wati.io/reference/post_api-v1-updatecontactattributes-whatsappnumber)",
version: "0.0.1",
type: "action",
props: {
wati,
whatsappNumber: {
propDefinition: [
wati,
"whatsappNumber",
],
},
customParams: {
propDefinition: [
wati,
"customParams",
],
optional: true,
},
},
async run({ $ }) {
const response = await this.wati.updateContactAttributes({
$,
whatsappNumber: this.whatsappNumber,
data: {
customParams: this.customParams && Object.entries(this.customParams).map(([
key,
value,
]) => ({
name: key,
value,
})),
},
});
if (!response.result) {
throw new ConfigurationError(response.info);
}

$.export("$summary", `Successfully updated attributes for contact ${this.whatsappNumber}`);
return response;
},
};
13 changes: 0 additions & 13 deletions components/wati/app/wati.app.ts

This file was deleted.

10 changes: 6 additions & 4 deletions components/wati/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"name": "@pipedream/wati",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream WATI Components",
"main": "dist/app/wati.app.mjs",
"main": "wati.app.mjs",
"keywords": [
"pipedream",
"wati"
],
"files": ["dist"],
"homepage": "https://pipedream.com/apps/wati",
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3"
}
}
}
61 changes: 61 additions & 0 deletions components/wati/sources/common/base.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
import wati from "../../wati.app.mjs";

export default {
props: {
wati,
db: "$.service.db",
timer: {
type: "$.interface.timer",
default: {
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},
},
methods: {
prepareData(data) {
return data;
},
_getLastDate() {
return this.db.get("lastDate") || 0;
},
_setLastDate(lastDate) {
this.db.set("lastDate", lastDate);
},
async emitEvent(maxResults = false) {
const lastDate = this._getLastDate();
const dateField = this.getDateField();

const response = this.wati.paginate(
this.getPaginateOpts(maxResults),
);

let responseArray = [];
for await (const item of response) {
responseArray.push(item);
}

responseArray = this.prepareData(responseArray, lastDate, maxResults);

if (responseArray.length) {
this._setLastDate(responseArray[0][dateField]);
}

for (const item of responseArray.reverse()) {
this.$emit(item, {
id: item.id,
summary: this.getSummary(item),
ts: Date.parse(item[dateField]),
});
}
},
},
hooks: {
async deploy() {
await this.emitEvent(25);
},
},
async run() {
await this.emitEvent();
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import common from "../common/base.mjs";
import sampleEmit from "./test-event.mjs";

export default {
...common,
key: "wati-new-contact-created",
name: "New Contact Created",
description: "Emit new event when a contact is created from an incoming WhatsApp message.",
version: "0.0.1",
type: "source",
dedupe: "unique",
methods: {
...common.methods,
getPaginateOpts(maxResults) {
return {
fn: this.wati.listContacts,
itemsField: "result",
optsField: "data",
maxResults,
};
},
getDateField() {
return "created";
},
checkBreak(item, lastDate) {
return Date.parse(item.created) < lastDate;
},
getSummary(item) {
return `New contact created: ${item.wAid}`;
},
},
sampleEmit,
};
33 changes: 33 additions & 0 deletions components/wati/sources/new-contact-created/test-event.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default {
"id": "670934c1d464c11dd46c3b7f",
"wAid": "17759865200",
"firstName": "+17759865200",
"fullName": "+17759865200",
"phone": "17759865200",
"source": null,
"contactStatus": "VALID",
"photo": null,
"created": "Oct-11-2024",
"customParams": [
{
"name": "name",
"value": "+17759865200"
},
{
"name": "phone",
"value": "17759865200"
}
],
"optedIn": false,
"isDeleted": false,
"lastUpdated": "2024-10-11T15:09:36.047Z",
"allowBroadcast": true,
"allowSMS": true,
"teamIds": [
"6708393ad464c11dd46b3d73"
],
"isInFlow": false,
"lastFlowId": null,
"currentFlowNodeId": null,
"selectedHubspotId": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import common from "../common/base.mjs";
import sampleEmit from "./test-event.mjs";

export default {
...common,
key: "wati-new-incoming-message",
name: "New Incoming Message",
description: "Emit new event when there is an incoming message on your number.",
version: "0.0.1",
type: "source",
dedupe: "unique",
props: {
...common.props,
contactId: {
propDefinition: [
common.props.wati,
"contactId",
],
},
},
methods: {
...common.methods,
getPaginateOpts() {
return {
fn: this.wati.listContactMessages,
whatsappNumber: `+${this.contactId}`,
itemsField: [
"messages",
],
optsField: "params",
};
},
getDateField() {
return "timestamp";
},
prepareData(data, lastDate, maxResults) {
data = data
.filter((item) => item.statusString === "SENT" && Date.parse(item.created) > lastDate)
.sort((a, b) => Date.parse(b.created) - Date.parse(a.created));

if (maxResults && data.length > maxResults) data.length = maxResults;
return data;
},
checkBreak(item, lastDate) {
return Date.parse(item.timestamp) < lastDate;
},
getSummary(item) {
return `New message: ${item.text || "No content"}`;
},
},
sampleEmit,
};
Loading

0 comments on commit dd0bef4

Please sign in to comment.