Skip to content

[Components] z_api #10837 #16555

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

Merged
merged 3 commits into from
May 6, 2025
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
35 changes: 35 additions & 0 deletions components/z_api/actions/get-contacts/get-contacts.mjs
Original file line number Diff line number Diff line change
@@ -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,
pageNum: {
propDefinition: [
app,
"pageNum",
],
},
pageSize: {
propDefinition: [
app,
"pageSize",
],
},
},
async run({ $ }) {
const response = await this.app.getContacts({
$,
params: {
page: this.pageNum,
pageSize: this.pageSize,
},
});
$.export("$summary", `Successfully retrieved ${response.length} contacts`);
return response;
},
};
44 changes: 44 additions & 0 deletions components/z_api/actions/modify-chat/modify-chat.mjs
Original file line number Diff line number Diff line change
@@ -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;
},
};
35 changes: 35 additions & 0 deletions components/z_api/actions/send-text/send-text.mjs
Original file line number Diff line number Diff line change
@@ -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;
},
};
36 changes: 36 additions & 0 deletions components/z_api/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -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",
},
],
};
5 changes: 4 additions & 1 deletion components/z_api/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3"
}
}
104 changes: 99 additions & 5 deletions components/z_api/z_api.app.mjs
Original file line number Diff line number Diff line change
@@ -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",
},
Comment on lines +18 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

pageNum / pageSize should be numeric to prevent accidental string concatenation

Both props are declared as string, yet they’re passed to the API as query params where numeric semantics are expected.
Changing the type to "integer" avoids surprises (e.g., "1" + 1 === "11").

-    pageNum: {
-      type: "string",
+    pageNum: {
+      type: "integer",
...
-    pageSize: {
-      type: "string",
+    pageSize: {
+      type: "integer",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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",
},
action: {
type: "string",
label: "Action",
description: "The action to be performed on the chat",
options: constants.ACTION_OPTIONS,
},
pageNum: {
type: "integer",
label: "Page",
description: "Used to paginate the results",
},
pageSize: {
type: "integer",
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,
});
},
},
};
};
12 changes: 7 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading