Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Components - wati #14276

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
});
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved
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,
})),
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved
},
});
if (!response.result) {
throw new ConfigurationError(response.info);
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved
}

$.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",
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved
"keywords": [
"pipedream",
"wati"
],
"files": ["dist"],
"homepage": "https://pipedream.com/apps/wati",
"author": "Pipedream <[email protected]> (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;
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved
},
getSummary(item) {
return `New contact created: ${item.wAid}`;
},
},
sampleEmit,
};
luancazarine marked this conversation as resolved.
Show resolved Hide resolved
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",
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved
"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
}
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved
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}`,
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved
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;
},
luancazarine marked this conversation as resolved.
Show resolved Hide resolved
checkBreak(item, lastDate) {
return Date.parse(item.timestamp) < lastDate;
michelle0927 marked this conversation as resolved.
Show resolved Hide resolved
},
getSummary(item) {
return `New message: ${item.text || "No content"}`;
},
},
sampleEmit,
};
Loading
Loading