Skip to content

Commit 1e8db4a

Browse files
Merging pull request #16555
* Added actions * Improve action summary * Added actions --------- Co-authored-by: Leo Vu <[email protected]>
1 parent aae9f1c commit 1e8db4a

File tree

7 files changed

+258
-7
lines changed

7 files changed

+258
-7
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import app from "../../z_api.app.mjs";
2+
3+
export default {
4+
key: "z_api-get-contacts",
5+
name: "Get Contacts",
6+
description: "Get a list of all your WhatsApp contacts. [See the documentation](https://developer.z-api.io/en/contacts/get-contacts)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
pageNum: {
12+
propDefinition: [
13+
app,
14+
"pageNum",
15+
],
16+
},
17+
pageSize: {
18+
propDefinition: [
19+
app,
20+
"pageSize",
21+
],
22+
},
23+
},
24+
async run({ $ }) {
25+
const response = await this.app.getContacts({
26+
$,
27+
params: {
28+
page: this.pageNum,
29+
pageSize: this.pageSize,
30+
},
31+
});
32+
$.export("$summary", `Successfully retrieved ${response.length} contacts`);
33+
return response;
34+
},
35+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import app from "../../z_api.app.mjs";
2+
3+
export default {
4+
key: "z_api-modify-chat",
5+
name: "Modify Chat",
6+
description: "Modify the specified chat. [See the documentation](https://developer.z-api.io/en/chats/delete-chat)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
pageNum: {
12+
propDefinition: [
13+
app,
14+
"pageNum",
15+
],
16+
},
17+
chat: {
18+
propDefinition: [
19+
app,
20+
"chat",
21+
(c) => ({
22+
pageNum: c.pageNum,
23+
}),
24+
],
25+
},
26+
action: {
27+
propDefinition: [
28+
app,
29+
"action",
30+
],
31+
},
32+
},
33+
async run({ $ }) {
34+
const response = await this.app.modifyChat({
35+
$,
36+
data: {
37+
phone: this.chat,
38+
action: this.action,
39+
},
40+
});
41+
$.export("$summary", "Successfully modified chat with number: " + this.chat);
42+
return response;
43+
},
44+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import app from "../../z_api.app.mjs";
2+
3+
export default {
4+
key: "z_api-send-text",
5+
name: "Send Text",
6+
description: "Send a text to the specified phone. [See the documentation](https://developer.z-api.io/en/message/send-message-text)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
phone: {
12+
propDefinition: [
13+
app,
14+
"phone",
15+
],
16+
},
17+
message: {
18+
propDefinition: [
19+
app,
20+
"message",
21+
],
22+
},
23+
},
24+
async run({ $ }) {
25+
const response = await this.app.sendText({
26+
$,
27+
data: {
28+
phone: this.phone,
29+
message: this.message,
30+
},
31+
});
32+
$.export("$summary", "Successfully sent text to" + this.phone);
33+
return response;
34+
},
35+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export default {
2+
ACTION_OPTIONS: [
3+
{
4+
label: "Delete",
5+
value: "delete",
6+
},
7+
{
8+
label: "Clear",
9+
value: "clear",
10+
},
11+
{
12+
label: "Mute",
13+
value: "mute",
14+
},
15+
{
16+
label: "Unmute",
17+
value: "unmute",
18+
},
19+
{
20+
label: "Pin",
21+
value: "pin",
22+
},
23+
{
24+
label: "Unpin",
25+
value: "unpin",
26+
},
27+
{
28+
label: "Archive",
29+
value: "archive",
30+
},
31+
{
32+
label: "Unarchive",
33+
value: "unarchive",
34+
},
35+
],
36+
};

components/z_api/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/z_api",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Z-API Components",
55
"main": "z_api.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
1518
}

components/z_api/z_api.app.mjs

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,105 @@
1+
import { axios } from "@pipedream/platform";
2+
import constants from "./common/constants.mjs";
3+
14
export default {
25
type: "app",
36
app: "z_api",
4-
propDefinitions: {},
7+
propDefinitions: {
8+
phone: {
9+
type: "string",
10+
label: "Phone",
11+
description: "Telephone number of the contact the message will be sent to, i.e.: `551199999999`",
12+
},
13+
message: {
14+
type: "string",
15+
label: "Message",
16+
description: "The message to be sent",
17+
},
18+
action: {
19+
type: "string",
20+
label: "Action",
21+
description: "The action to be performed on the chat",
22+
options: constants.ACTION_OPTIONS,
23+
},
24+
pageNum: {
25+
type: "string",
26+
label: "Page",
27+
description: "Used to paginate the results",
28+
},
29+
pageSize: {
30+
type: "string",
31+
label: "Page Size",
32+
description: "The number of chats to be retrieved",
33+
},
34+
chat: {
35+
type: "string",
36+
label: "Chat",
37+
description: "The chat to be modified",
38+
async options({ pageNum }) {
39+
const response = await this.getChats({
40+
pageNum,
41+
});
42+
return response.map(({
43+
name, phone,
44+
}) => ({
45+
label: name || phone,
46+
value: phone,
47+
}));
48+
},
49+
},
50+
},
551
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
52+
_baseUrl() {
53+
return `https://api.z-api.io/instances/${this.$auth.instance_id}/token/${this.$auth.token_id}`;
54+
},
55+
async _makeRequest(opts = {}) {
56+
const {
57+
$ = this,
58+
path,
59+
headers,
60+
...otherOpts
61+
} = opts;
62+
return axios($, {
63+
...otherOpts,
64+
url: this._baseUrl() + path,
65+
headers: {
66+
"Client-Token": `${this.$auth.account_security_token}`,
67+
...headers,
68+
},
69+
});
70+
},
71+
async getContacts(args = {}) {
72+
return this._makeRequest({
73+
path: "/contacts",
74+
...args,
75+
});
76+
},
77+
async sendText(args = {}) {
78+
return this._makeRequest({
79+
path: "/send-text",
80+
method: "post",
81+
...args,
82+
});
83+
},
84+
async modifyChat(args = {}) {
85+
return this._makeRequest({
86+
path: "/modify-chat",
87+
method: "post",
88+
...args,
89+
});
90+
},
91+
async getChats({
92+
pageNum,
93+
...args
94+
}) {
95+
return this._makeRequest({
96+
path: "/chats",
97+
params: {
98+
page: pageNum,
99+
pageSize: 50,
100+
},
101+
...args,
102+
});
9103
},
10104
},
11-
};
105+
};

pnpm-lock.yaml

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)