Skip to content

Commit 4834933

Browse files
committed
[Components] syncmate_by_assitro #16832
Actions - Send Message - Send Bulk Messages
1 parent 6b3a082 commit 4834933

File tree

6 files changed

+193
-124
lines changed

6 files changed

+193
-124
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import fs from "fs";
3+
import { checkTmp } from "../../common/utils.mjs";
4+
import syncmateByAssitro from "../../syncmate_by_assitro.app.mjs";
5+
6+
export default {
7+
props: {
8+
syncmateByAssitro,
9+
number: {
10+
propDefinition: [
11+
syncmateByAssitro,
12+
"number",
13+
],
14+
},
15+
message: {
16+
propDefinition: [
17+
syncmateByAssitro,
18+
"message",
19+
],
20+
},
21+
media: {
22+
propDefinition: [
23+
syncmateByAssitro,
24+
"media",
25+
],
26+
},
27+
fileName: {
28+
propDefinition: [
29+
syncmateByAssitro,
30+
"fileName",
31+
],
32+
optional: true,
33+
},
34+
},
35+
async run({ $ }) {
36+
if (this.media && !this.fileName) {
37+
throw new ConfigurationError("You must provide the file name.");
38+
}
39+
const file = fs.readFileSync(checkTmp(this.media), {
40+
encoding: "base64",
41+
});
42+
const response = await this.syncmateByAssitro.sendSingleMessage({
43+
$,
44+
data: {
45+
msgs: [
46+
{
47+
number: this.number,
48+
message: this.message,
49+
media: [
50+
{
51+
media_base64: file,
52+
file_name: this.fileName,
53+
},
54+
],
55+
},
56+
],
57+
},
58+
});
59+
60+
$.export("$summary", `Successfully sent message to ${this.number}`);
61+
return response;
62+
},
63+
};
Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,79 @@
1+
import fs from "fs";
2+
import { checkTmp } from "../../common/utils.mjs";
13
import syncmateByAssitro from "../../syncmate_by_assitro.app.mjs";
2-
import { axios } from "@pipedream/platform";
34

45
export default {
56
key: "syncmate_by_assitro-send-bulk-messages",
67
name: "Send Bulk Messages",
78
description: "Send multiple WhatsApp messages in bulk. [See the documentation](https://assistro.co/user-guide/bulk-messaging-at-a-scheduled-time-using-syncmate-2/)",
8-
version: "0.0.{{ts}}",
9+
version: "0.0.1",
910
type: "action",
1011
props: {
1112
syncmateByAssitro,
12-
numberOfMessages: {
13-
propDefinition: [
14-
syncmateByAssitro,
15-
"numberOfMessages",
16-
],
17-
},
18-
number: {
19-
propDefinition: [
20-
syncmateByAssitro,
21-
"number",
22-
],
23-
},
24-
message: {
25-
propDefinition: [
26-
syncmateByAssitro,
27-
"message",
28-
],
29-
},
30-
media: {
31-
propDefinition: [
32-
syncmateByAssitro,
33-
"media",
34-
],
35-
optional: true,
13+
messagesNumber: {
14+
type: "string",
15+
label: "Messages Number",
16+
description: "The quantity of messages you want to send.",
17+
reloadProps: true,
3618
},
3719
},
20+
async additionalProps() {
21+
const props = {};
22+
for (let i = 1; i <= this.messagesNumber; i++) {
23+
props[`number${i}`] = {
24+
type: "string",
25+
label: `Number ${i}`,
26+
description: "WhatsApp number with country code",
27+
};
28+
props[`message${i}`] = {
29+
type: "string",
30+
label: `Message ${i}`,
31+
description: "The text message to be sent",
32+
};
33+
props[`media${i}`] = {
34+
type: "string",
35+
label: `Media ${i}`,
36+
description: "Base64 encoded media files",
37+
optional: true,
38+
};
39+
props[`fileName${i}`] = {
40+
type: "string",
41+
label: `File Name ${i}`,
42+
description: "The name of the file.",
43+
optional: true,
44+
};
45+
}
46+
47+
return props;
48+
},
3849
async run({ $ }) {
50+
const msgs = [];
51+
52+
for (let i = 1; i <= this.messagesNumber; i++) {
53+
const file = fs.readFileSync(checkTmp(this[`media${i}`]), {
54+
encoding: "base64",
55+
});
56+
57+
msgs.push({
58+
number: this[`number${i}`],
59+
message: this[`message${i}`],
60+
media: [
61+
{
62+
media_base64: file,
63+
file_name: this[`fileName${i}`],
64+
},
65+
],
66+
});
67+
}
68+
3969
const response = await this.syncmateByAssitro.sendBulkMessages({
40-
numberOfMessages: this.numberOfMessages,
41-
number: this.number,
42-
message: this.message,
43-
media: this.media,
70+
$,
71+
data: {
72+
msgs,
73+
},
4474
});
4575

46-
$.export("$summary", `Successfully sent ${this.numberOfMessages} messages to ${this.number}`);
76+
$.export("$summary", `Successfully sent ${this.messagesNumber} messages.`);
4777
return response;
4878
},
4979
};

components/syncmate_by_assitro/actions/send-message/send-message.mjs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,62 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import fs from "fs";
3+
import { checkTmp } from "../../common/utils.mjs";
14
import syncmateByAssitro from "../../syncmate_by_assitro.app.mjs";
2-
import { axios } from "@pipedream/platform";
35

46
export default {
57
key: "syncmate_by_assitro-send-message",
68
name: "Send WhatsApp Message",
79
description: "Send a single WhatsApp message using SyncMate by Assistro. [See the documentation](https://assistro.co/user-guide/connect-your-custom-app-with-syncmate/)",
8-
version: "0.0.{{ts}}",
10+
version: "0.0.1",
911
type: "action",
1012
props: {
1113
syncmateByAssitro,
1214
number: {
13-
propDefinition: [
14-
syncmateByAssitro,
15-
"number",
16-
],
15+
type: "string",
16+
label: "Number",
17+
description: "WhatsApp number with country code",
1718
},
1819
message: {
19-
propDefinition: [
20-
syncmateByAssitro,
21-
"message",
22-
],
20+
type: "string",
21+
label: "Message",
22+
description: "The text message to be sent",
2323
},
2424
media: {
25-
propDefinition: [
26-
syncmateByAssitro,
27-
"media",
28-
],
25+
type: "string",
26+
label: "Media",
27+
description: "Base64 encoded media files",
28+
optional: true,
29+
},
30+
fileName: {
31+
type: "string",
32+
label: "File Name",
33+
description: "The name of the file.",
34+
optional: true,
2935
},
3036
},
3137
async run({ $ }) {
38+
if (this.media && !this.fileName) {
39+
throw new ConfigurationError("You must provide the file name.");
40+
}
41+
const file = fs.readFileSync(checkTmp(this.media), {
42+
encoding: "base64",
43+
});
3244
const response = await this.syncmateByAssitro.sendSingleMessage({
33-
number: this.number,
34-
message: this.message,
35-
media: this.media,
45+
$,
46+
data: {
47+
msgs: [
48+
{
49+
number: this.number,
50+
message: this.message,
51+
media: [
52+
{
53+
media_base64: file,
54+
file_name: this.fileName,
55+
},
56+
],
57+
},
58+
],
59+
},
3660
});
3761

3862
$.export("$summary", `Successfully sent message to ${this.number}`);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const checkTmp = (filename) => {
2+
if (!filename.startsWith("/tmp")) {
3+
return `/tmp/${filename}`;
4+
}
5+
return filename;
6+
};

components/syncmate_by_assitro/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/syncmate_by_assitro",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream SyncMate by Assitro Components",
55
"main": "syncmate_by_assitro.app.mjs",
66
"keywords": [
@@ -11,5 +11,9 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"fs": "^0.0.1-security"
1418
}
1519
}

components/syncmate_by_assitro/syncmate_by_assitro.app.mjs

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,95 +3,37 @@ import { axios } from "@pipedream/platform";
33
export default {
44
type: "app",
55
app: "syncmate_by_assitro",
6-
propDefinitions: {
7-
number: {
8-
type: "string",
9-
label: "Number",
10-
description: "WhatsApp number with country code",
11-
},
12-
message: {
13-
type: "string",
14-
label: "Message",
15-
description: "The text message to be sent",
16-
},
17-
media: {
18-
type: "string[]",
19-
label: "Media",
20-
description: "Base64 encoded media files",
21-
optional: true,
22-
default: [],
23-
},
24-
numberOfMessages: {
25-
type: "integer",
26-
label: "Number of Messages",
27-
description: "The number of messages to send in bulk",
28-
},
29-
},
306
methods: {
31-
authKeys() {
32-
console.log(Object.keys(this.$auth));
33-
},
347
_baseUrl() {
358
return "https://app.assistro.co/api/v1/wapushplus";
369
},
37-
async _makeRequest(opts = {}) {
38-
const {
39-
$ = this,
40-
method = "POST",
41-
path = "/",
42-
headers,
43-
...otherOpts
44-
} = opts;
10+
_headers() {
11+
return {
12+
"Authorization": `Bearer ${this.$auth.api_key}`,
13+
"Content-Type": "application/json",
14+
};
15+
},
16+
_makeRequest({
17+
$ = this, path, ...opts
18+
}) {
4519
return axios($, {
46-
...otherOpts,
47-
method,
4820
url: this._baseUrl() + path,
49-
headers: {
50-
"Authorization": `Bearer ${this.$auth.api_key}`,
51-
"Content-Type": "application/json",
52-
...headers,
53-
},
21+
headers: this._headers(),
22+
...opts,
5423
});
5524
},
56-
async sendSingleMessage({
57-
number, message, media = [],
58-
}) {
59-
const data = {
60-
msgs: [
61-
{
62-
number,
63-
message,
64-
media: media.map((m) => ({
65-
media_base64: m,
66-
file_name: `file_${Date.now()}`,
67-
})),
68-
},
69-
],
70-
};
25+
sendSingleMessage(opts = {}) {
7126
return this._makeRequest({
27+
method: "POST",
7228
path: "/single/message",
73-
data,
29+
...opts,
7430
});
7531
},
76-
async sendBulkMessages({
77-
numberOfMessages, number, message, media = [],
78-
}) {
79-
const msgs = Array.from({
80-
length: numberOfMessages,
81-
}, () => ({
82-
number,
83-
message,
84-
media: media.map((m) => ({
85-
media_base64: m,
86-
file_name: `file_${Date.now()}`,
87-
})),
88-
}));
89-
const data = {
90-
msgs,
91-
};
32+
sendBulkMessages(opts = {}) {
9233
return this._makeRequest({
34+
method: "POST",
9335
path: "/bulk/message",
94-
data,
36+
...opts,
9537
});
9638
},
9739
},

0 commit comments

Comments
 (0)