-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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]>
- Loading branch information
1 parent
7b02b2f
commit dd0bef4
Showing
13 changed files
with
522 additions
and
21 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |
63 changes: 63 additions & 0 deletions
63
components/wati/actions/send-template-message/send-template-message.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |
47 changes: 47 additions & 0 deletions
47
components/wati/actions/update-contact-attribute/update-contact-attribute.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> (https://pipedream.com/)", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@pipedream/platform": "^3.0.3" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
}; |
33 changes: 33 additions & 0 deletions
33
components/wati/sources/new-contact-created/new-contact-created.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
components/wati/sources/new-contact-created/test-event.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
52 changes: 52 additions & 0 deletions
52
components/wati/sources/new-incoming-message/new-incoming-message.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Oops, something went wrong.