From 2d55f49d6397b1671936b03a30026fcc33aba15a Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Sun, 4 May 2025 16:38:10 -0300 Subject: [PATCH 1/3] Added actions --- .../actions/get-contacts/get-contacts.mjs | 35 ++++++ .../z_api/actions/modify-chat/modify-chat.mjs | 44 ++++++++ .../z_api/actions/send-text/send-text.mjs | 35 ++++++ components/z_api/common/constants.mjs | 36 ++++++ components/z_api/package.json | 5 +- components/z_api/z_api.app.mjs | 104 +++++++++++++++++- pnpm-lock.yaml | 12 +- 7 files changed, 260 insertions(+), 11 deletions(-) create mode 100644 components/z_api/actions/get-contacts/get-contacts.mjs create mode 100644 components/z_api/actions/modify-chat/modify-chat.mjs create mode 100644 components/z_api/actions/send-text/send-text.mjs create mode 100644 components/z_api/common/constants.mjs diff --git a/components/z_api/actions/get-contacts/get-contacts.mjs b/components/z_api/actions/get-contacts/get-contacts.mjs new file mode 100644 index 0000000000000..d41052c8de325 --- /dev/null +++ b/components/z_api/actions/get-contacts/get-contacts.mjs @@ -0,0 +1,35 @@ +import app from "../../z_api.app.mjs"; + +export default { + key: "z_api-get-contacts", + name: "Get Contacts", + description: "Get a list of all your WhatsApp contacts. [See the documentation](https://developer.z-api.io/en/contacts/get-contacts)", + version: "0.0.1", + type: "action", + props: { + app, + page: { + propDefinition: [ + app, + "page", + ], + }, + pageSize: { + propDefinition: [ + app, + "pageSize", + ], + }, + }, + async run({ $ }) { + const response = await this.app.getContacts({ + $, + params: { + page: this.page, + pageSize: this.pageSize, + }, + }); + $.export("$summary", `Successfully retrieved ${response.length} contacts`); + return response; + }, +}; diff --git a/components/z_api/actions/modify-chat/modify-chat.mjs b/components/z_api/actions/modify-chat/modify-chat.mjs new file mode 100644 index 0000000000000..e5125e02e745b --- /dev/null +++ b/components/z_api/actions/modify-chat/modify-chat.mjs @@ -0,0 +1,44 @@ +import app from "../../z_api.app.mjs"; + +export default { + key: "z_api-modify-chat", + name: "Modify Chat", + description: "Modify the specified chat. [See the documentation](https://developer.z-api.io/en/chats/delete-chat)", + version: "0.0.1", + type: "action", + props: { + app, + pageNum: { + propDefinition: [ + app, + "pageNum", + ], + }, + chat: { + propDefinition: [ + app, + "chat", + (c) => ({ + pageNum: c.pageNum, + }), + ], + }, + action: { + propDefinition: [ + app, + "action", + ], + }, + }, + async run({ $ }) { + const response = await this.app.modifyChat({ + $, + data: { + phone: this.chat, + action: this.action, + }, + }); + $.export("$summary", "Successfully modified chat with number: " + this.chat); + return response; + }, +}; diff --git a/components/z_api/actions/send-text/send-text.mjs b/components/z_api/actions/send-text/send-text.mjs new file mode 100644 index 0000000000000..a78e2e1181d07 --- /dev/null +++ b/components/z_api/actions/send-text/send-text.mjs @@ -0,0 +1,35 @@ +import app from "../../z_api.app.mjs"; + +export default { + key: "z_api-send-text", + name: "Send Text", + description: "Send a text to the specified phone. [See the documentation](https://developer.z-api.io/en/message/send-message-text)", + version: "0.0.1", + type: "action", + props: { + app, + phone: { + propDefinition: [ + app, + "phone", + ], + }, + message: { + propDefinition: [ + app, + "message", + ], + }, + }, + async run({ $ }) { + const response = await this.app.sendText({ + $, + data: { + phone: this.phone, + message: this.message, + }, + }); + $.export("$summary", "Successfully sent text to" + this.phone); + return response; + }, +}; diff --git a/components/z_api/common/constants.mjs b/components/z_api/common/constants.mjs new file mode 100644 index 0000000000000..fb4051b48db5e --- /dev/null +++ b/components/z_api/common/constants.mjs @@ -0,0 +1,36 @@ +export default { + ACTION_OPTIONS: [ + { + label: "Delete", + value: "delete", + }, + { + label: "Clear", + value: "clear", + }, + { + label: "Mute", + value: "mute", + }, + { + label: "Unmute", + value: "unmute", + }, + { + label: "Pin", + value: "pin", + }, + { + label: "Unpin", + value: "unpin", + }, + { + label: "Archive", + value: "archive", + }, + { + label: "Unarchive", + value: "unarchive", + }, + ], +}; diff --git a/components/z_api/package.json b/components/z_api/package.json index fc6c56ed77e1e..0b784d557b095 100644 --- a/components/z_api/package.json +++ b/components/z_api/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/z_api", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Z-API Components", "main": "z_api.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } } diff --git a/components/z_api/z_api.app.mjs b/components/z_api/z_api.app.mjs index f588a73c6ba21..b14499bb3339b 100644 --- a/components/z_api/z_api.app.mjs +++ b/components/z_api/z_api.app.mjs @@ -1,11 +1,105 @@ +import { axios } from "@pipedream/platform"; +import constants from "./common/constants.mjs"; + export default { type: "app", app: "z_api", - propDefinitions: {}, + propDefinitions: { + phone: { + type: "string", + label: "Phone", + description: "Telephone number of the contact the message will be sent to, i.e.: `551199999999`", + }, + message: { + type: "string", + label: "Message", + description: "The message to be sent", + }, + action: { + type: "string", + label: "Action", + description: "The action to be performed on the chat", + options: constants.ACTION_OPTIONS, + }, + pageNum: { + type: "string", + label: "Page", + description: "Used to paginate the results", + }, + pageSize: { + type: "string", + label: "Page Size", + description: "The number of chats to be retrieved", + }, + chat: { + type: "string", + label: "Chat", + description: "The chat to be modified", + async options({ pageNum }) { + const response = await this.getChats({ + pageNum, + }); + return response.map(({ + name, phone, + }) => ({ + label: name || phone, + value: phone, + })); + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return `https://api.z-api.io/instances/${this.$auth.instance_id}/token/${this.$auth.token_id}`; + }, + async _makeRequest(opts = {}) { + const { + $ = this, + path, + headers, + ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + url: this._baseUrl() + path, + headers: { + "Client-Token": `${this.$auth.account_security_token}`, + ...headers, + }, + }); + }, + async getContacts(args = {}) { + return this._makeRequest({ + path: "/contacts", + ...args, + }); + }, + async sendText(args = {}) { + return this._makeRequest({ + path: "/send-text", + method: "post", + ...args, + }); + }, + async modifyChat(args = {}) { + return this._makeRequest({ + path: "/modify-chat", + method: "post", + ...args, + }); + }, + async getChats({ + pageNum, + ...args + }) { + return this._makeRequest({ + path: "/chats", + params: { + page: pageNum, + pageSize: 50, + }, + ...args, + }); }, }, -}; \ No newline at end of file +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1c60519e3d425..c1df2175b13fd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13059,11 +13059,9 @@ importers: components/test_apps_for_checking_something_001: {} - components/test_apps_for_checking_something_005: - specifiers: {} + components/test_apps_for_checking_something_005: {} - components/test_apps_for_checking_something_006: - specifiers: {} + components/test_apps_for_checking_something_006: {} components/test_apps_for_switching_appslug_009: {} @@ -14749,7 +14747,11 @@ importers: components/yumpu: {} - components/z_api: {} + components/z_api: + dependencies: + '@pipedream/platform': + specifier: ^3.0.3 + version: 3.0.3 components/zagomail: {} From dc1b1d5004bb7769f8dc74c9c97d4ba46d356874 Mon Sep 17 00:00:00 2001 From: Leo Vu Date: Mon, 5 May 2025 09:50:05 +0700 Subject: [PATCH 2/3] Improve action summary --- components/z_api/actions/send-text/send-text.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/z_api/actions/send-text/send-text.mjs b/components/z_api/actions/send-text/send-text.mjs index a78e2e1181d07..aa24dd95570b2 100644 --- a/components/z_api/actions/send-text/send-text.mjs +++ b/components/z_api/actions/send-text/send-text.mjs @@ -29,7 +29,7 @@ export default { message: this.message, }, }); - $.export("$summary", "Successfully sent text to" + this.phone); + $.export("$summary", "Successfully sent text to " + this.phone); return response; }, }; From 4bf9b8bdf66bb2ea0dd29e1768a0729908400d15 Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Mon, 5 May 2025 14:12:49 -0300 Subject: [PATCH 3/3] Added actions --- components/z_api/actions/get-contacts/get-contacts.mjs | 6 +++--- components/z_api/actions/send-text/send-text.mjs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/z_api/actions/get-contacts/get-contacts.mjs b/components/z_api/actions/get-contacts/get-contacts.mjs index d41052c8de325..c7fc2218dbb2d 100644 --- a/components/z_api/actions/get-contacts/get-contacts.mjs +++ b/components/z_api/actions/get-contacts/get-contacts.mjs @@ -8,10 +8,10 @@ export default { type: "action", props: { app, - page: { + pageNum: { propDefinition: [ app, - "page", + "pageNum", ], }, pageSize: { @@ -25,7 +25,7 @@ export default { const response = await this.app.getContacts({ $, params: { - page: this.page, + page: this.pageNum, pageSize: this.pageSize, }, }); diff --git a/components/z_api/actions/send-text/send-text.mjs b/components/z_api/actions/send-text/send-text.mjs index aa24dd95570b2..a78e2e1181d07 100644 --- a/components/z_api/actions/send-text/send-text.mjs +++ b/components/z_api/actions/send-text/send-text.mjs @@ -29,7 +29,7 @@ export default { message: this.message, }, }); - $.export("$summary", "Successfully sent text to " + this.phone); + $.export("$summary", "Successfully sent text to" + this.phone); return response; }, };