Skip to content

Commit 8eb0181

Browse files
committed
updates
1 parent d3c1a31 commit 8eb0181

File tree

7 files changed

+111
-28
lines changed

7 files changed

+111
-28
lines changed

components/slack/actions/delete-file/delete-file.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ export default {
88
type: "action",
99
props: {
1010
slack,
11+
conversation: {
12+
propDefinition: [
13+
slack,
14+
"conversation",
15+
],
16+
},
1117
file: {
1218
propDefinition: [
1319
slack,
1420
"file",
21+
(c) => ({
22+
channel: c.conversation,
23+
}),
1524
],
1625
},
1726
},

components/slack/actions/get-file/get-file.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ export default {
88
type: "action",
99
props: {
1010
slack,
11+
conversation: {
12+
propDefinition: [
13+
slack,
14+
"conversation",
15+
],
16+
},
1117
file: {
1218
propDefinition: [
1319
slack,
1420
"file",
21+
(c) => ({
22+
channel: c.conversation,
23+
}),
1524
],
1625
},
1726
},

components/slack/actions/invite-user-to-channel/invite-user-to-channel.mjs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,19 @@ export default {
2222
},
2323
},
2424
async run({ $ }) {
25-
const response = await this.slack.inviteToConversation({
26-
channel: this.conversation,
27-
users: this.user,
28-
});
29-
$.export("$summary", `Successfully invited user ${this.user} to channel with ID ${this.conversation}`);
30-
return response;
25+
try {
26+
const response = await this.slack.inviteToConversation({
27+
channel: this.conversation,
28+
users: this.user,
29+
});
30+
$.export("$summary", `Successfully invited user ${this.user} to channel with ID ${this.conversation}`);
31+
return response;
32+
} catch (error) {
33+
if (error.includes("already_in_channel")) {
34+
$.export("$summary", `The user ${this.user} is already in the channel`);
35+
return;
36+
}
37+
throw error;
38+
}
3139
},
3240
};

components/slack/actions/kick-user/kick-user.mjs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ export default {
3333
},
3434
},
3535
async run({ $ }) {
36-
const response = await this.slack.kickUserFromConversation({
37-
channel: this.conversation,
38-
user: this.user,
39-
});
40-
$.export("$summary", `Successfully kicked user ${this.user} from channel with ID ${this.conversation}`);
41-
return response;
36+
try {
37+
const response = await this.slack.kickUserFromConversation({
38+
channel: this.conversation,
39+
user: this.user,
40+
});
41+
$.export("$summary", `Successfully kicked user ${this.user} from channel with ID ${this.conversation}`);
42+
return response;
43+
} catch (error) {
44+
if (error.includes("not_in_channel")) {
45+
$.export("$summary", `The user ${this.user} is not in the channel`);
46+
return;
47+
}
48+
throw error;
49+
}
4250
},
4351
};

components/slack/actions/upload-file/upload-file.mjs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { ConfigurationError } from "@pipedream/platform";
1+
import {
2+
ConfigurationError, axios,
3+
} from "@pipedream/platform";
24
import fs from "fs";
5+
import FormData from "form-data";
36
import slack from "../../slack.app.mjs";
47

58
export default {
69
key: "slack-upload-file",
710
name: "Upload File",
8-
description: "Upload a file. [See the documentation](https://api.slack.com/methods/files.upload)",
11+
description: "Upload a file. [See the documentation](https://api.slack.com/messaging/files#uploading_files)",
912
version: "0.0.27",
1013
type: "action",
1114
props: {
@@ -35,13 +38,54 @@ export default {
3538
if (!fs.existsSync(this.content)) {
3639
throw new ConfigurationError(`\`${this.content}\` not found, a valid \`/tmp\` path is needed`);
3740
}
38-
const response = await this.slack.uploadFile({
39-
file: fs.createReadStream(this.content),
41+
42+
const filename = this.content.split("/").pop();
43+
44+
// Get an upload URL from Slack
45+
const getUploadUrlResponse = await this.slack.getUploadUrl({
46+
filename,
47+
length: fs.statSync(this.content).size,
48+
});
49+
50+
if (!getUploadUrlResponse.ok) {
51+
throw new ConfigurationError(`Error getting upload URL: ${JSON.stringify(getUploadUrlResponse)}`);
52+
}
53+
54+
const {
55+
upload_url: uploadUrl, file_id: fileId,
56+
} = getUploadUrlResponse;
57+
58+
// Upload the file to the provided URL
59+
const formData = new FormData();
60+
formData.append("file", fs.createReadStream(this.content));
61+
formData.append("filename", filename);
62+
63+
await axios($, {
64+
url: uploadUrl,
65+
data: formData,
66+
method: "POST",
67+
headers: {
68+
...formData.getHeaders(),
69+
Authorization: `Bearer ${this.slack.getToken()}`,
70+
},
71+
});
72+
73+
// Complete the file upload process in Slack
74+
const completeUploadResponse = await this.slack.completeUpload({
4075
channel_id: this.conversation,
4176
initial_comment: this.initialComment,
42-
filename: this.content.split("/").pop(),
77+
files: [
78+
{
79+
id: fileId,
80+
},
81+
],
4382
});
83+
84+
if (!completeUploadResponse.ok) {
85+
throw new Error(`Error completing upload: ${JSON.stringify(completeUploadResponse)}`);
86+
}
87+
4488
$.export("$summary", "Successfully uploaded file");
45-
return response;
89+
return completeUploadResponse;
4690
},
4791
};

components/slack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
},
1616
"dependencies": {
1717
"@pipedream/platform": "^3.0.0",
18-
"@slack/web-api": "^7.0.4"
18+
"@slack/web-api": "^7.9.0"
1919
}
2020
}

components/slack/slack.app.mjs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ export default {
483483
const sdk = props.reduce((reduction, prop) =>
484484
reduction[prop], this.sdk());
485485

486-
const response = await this._withRetries(() => sdk(args, throwRateLimitError));
486+
const response = await this._withRetries(() => sdk(args), throwRateLimitError);
487487

488488
if (!response.ok) {
489489
console.log(`Error in response with method ${method}`, response.error);
@@ -494,23 +494,22 @@ export default {
494494
async _withRetries(apiCall, throwRateLimitError = false) {
495495
const retryOpts = {
496496
retries: 3,
497-
factor: 2,
498-
minTimeout: 3000,
497+
minTimeout: 30000,
499498
};
500499
return retry(async (bail) => {
501500
try {
502501
return await apiCall();
503502
} catch (error) {
504503
const statusCode = get(error, "code");
505-
if (statusCode === 429) {
504+
if (statusCode === "slack_webapi_rate_limited_error") {
506505
if (throwRateLimitError) {
507506
bail(`Rate limit exceeded. ${error}`);
508507
} else {
509508
console.log(`Rate limit exceeded. Will retry in ${retryOpts.minTimeout / 1000} seconds`);
509+
throw error;
510510
}
511-
} else {
512-
throw error;
513511
}
512+
bail(`${error}`);
514513
}
515514
}, retryOpts);
516515
},
@@ -799,7 +798,7 @@ export default {
799798
});
800799
},
801800
listFiles(args = {}) {
802-
args.limit ||= constants.LIMIT;
801+
args.count ||= constants.LIMIT;
803802
return this.makeRequest({
804803
method: "files.list",
805804
...args,
@@ -938,9 +937,15 @@ export default {
938937
...args,
939938
});
940939
},
941-
uploadFile(args = {}) {
940+
getUploadUrl(args = {}) {
941+
return this.makeRequest({
942+
method: "files.getUploadURLExternal",
943+
...args,
944+
});
945+
},
946+
completeUpload(args = {}) {
942947
return this.makeRequest({
943-
method: "filesUploadV2",
948+
method: "files.completeUploadExternal",
944949
...args,
945950
});
946951
},

0 commit comments

Comments
 (0)