Skip to content

Upload files to channel #54

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
81 changes: 81 additions & 0 deletions integration-test/end-to-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,82 @@ const testUpdateMessage = async (channel, token) => {
return result;
};

const testUploadFile = async (channel, token) => {
// WIP!!!
// 1. Get this test to work
// 2. Move the code to the src/ section
// 3. Add integration test

return new Promise((resolve, reject) => {
const https = require("https");
const fs = require("fs");

let options = {
method: "POST",
hostname: "slack.com",
path: "/api/files.upload",
headers: {
Authorization: "Bearer " + token,
},
};

const req = https.request(options, (res) => {
let chunks = [];

res.on("data", (chunk) => {
chunks.push(chunk);
});

res.on("end", (chunk) => {
let body = Buffer.concat(chunks);
console.log(body.toString());
});

res.on("error", (error) => {
console.error(error);
});
});

/*let postData = `
----------------------------650979185122712208150351
Content-Disposition: form-data; name="file"; filename="one-does-not-simply.jpg"
Content-Type: image/jpeg

${fs.readFileSync(__dirname + "/one-does-not-simply.jpg").toString("ascii")}
----------------------------650979185122712208150351
Content-Disposition: form-data; name="initial_comment"

Wisdom of the Masters
----------------------------650979185122712208150351
Content-Disposition: form-data; name="channels"

CPPUV5KU0
----------------------------650979185122712208150351--
`;

fs.writeFileSync("temp-ascii.txt", postData);

req.setHeader(
"content-type",
"multipart/form-data; boundary=--------------------------650979185122712208150351"
);*/

let postData =
'------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="file"; filename="one-does-not-simply.jpg"\r\nContent-Type: "image/jpeg"\r\n\r\n' +
fs.readFileSync(__dirname + "/one-does-not-simply.jpg") +
'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="initial_comment"\r\n\r\nWisdom of the Masters\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="channels"\r\n\r\nCPPUV5KU0\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--';

req.setHeader(
"content-type",
"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
);

req.write(postData);

req.end();
});
};

(async () => {
if (!process.env.BOT_USER_OAUTH_ACCESS_TOKEN || !process.env.CHANNEL) {
console.error("Missing env values");
Expand Down Expand Up @@ -105,4 +181,9 @@ const testUpdateMessage = async (channel, token) => {
process.env.CHANNEL,
process.env.BOT_USER_OAUTH_ACCESS_TOKEN
);

await testUploadFile(
process.env.CHANNEL,
process.env.BOT_USER_OAUTH_ACCESS_TOKEN
);
})();
Binary file added integration-test/one-does-not-simply.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.