์ง๋ ๋ฒ์ ์๊ฐ์ด ์์ด์ slack file upload๋ฅผ ๊ตฌํํ์ง ๋ชปํ๋๋ฐ ์ฐจ๋ถํ ํ๋๊น ๊ธ๋ฐฉ ํ๊ฒ๋์๋ค. ๋ํ ๊ตฌ๊ธ์๋ ์ ๋ณด๊ฐ ๋ง์์ ๋์์ด ๋ง์ด ๋์๋ค. ์ญ์ ์ง์์ ์์นํ๋๋ฐ๋ ๊ตฌ๊ธ๋งํ๊ฒ ์๋ ๊ฒ ๊ฐ๋ค.
๊ตฌ๊ธ ์คํ๋ ๋์ํธ PDF๋ก ๋ณํํ์ฌ ๋ฉ์ผ ๋ณด๋ด๊ธฐ
๊ตฌ๊ธ ์คํ๋ ๋์ํธ๋ฅผ PDF๋ก ๋ณํํ์ฌ ๋ฐ๋ก Slack ์ฑ๋์ ์ ์กํ๊ธฐ
๋งํฌ : https://api.slack.com/methods/files.upload
function sendSlackFile() {
/** ๊ตฌ๊ธ ์คํ๋ ๋์ํธ pdf ๋ณํ*/
var theurl = '์ง๋๋ฒ์ ์คํ๋ ๋ ์ํธ pdf๋ก ๋ณํํ ๊ธ ์ฐพ์๋ณด๊ธฐ!'
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
/** ์ฌ๋ fileupload */
var metadata = {
token: "slack API ํ ํฐ",
channels: "์ฑ๋ ID",
filename: "ํ์ผ์ด๋ฆ.ํ์ฅ์",
title: "ํ์ผ์ ๋ชฉ",
};
var url = "https://slack.com/api/files.upload";
var boundary = "xxxxxxxxxx";
var data = "";
for (var i in metadata) {
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"" + i + "\"; \r\n\r\n" + metadata[i] + "\r\n";
}
data += "--" + boundary + "\r\n";
/** value1 */
data += "Content-Disposition: form-data; name=\"file\"; filename=\""+"ํ์ผ์ด๋ฆ"+"\"\r\n";
data += "Content-Type:application/ํ์ผํ์ฅ์\r\n\r\n";
/** value2 , file ์ฌ์ด์ฆ ๋งํผ */
var payload = Utilities.newBlob(data).getBytes()
.concat(docurl.getBlob().getBytes())
.concat(Utilities.newBlob("\r\n--" + boundary + "--").getBytes());
var options = {
method : "post",
contentType : "multipart/form-data; boundary=" + boundary,
payload : payload,
muteHttpExceptions: true,
};
var res = UrlFetchApp.fetch(url, options).getContentText();
Logger.log(res);
}
#์์ค ํด์ํ๊ธฐ
- ์คํ๋ ๋์ํธ๋ ํ๋ฒ pdf๋ก ๋ณํํ๊ธฐ UrlFetchApp์ ์ฌ์ฉํด์ผ ํ๋ค. UrlFetchApp : ๋ฆฌ์์ค๋ฅผ ๊ฐ์ ธ์ค๊ณ ์ธํฐ๋ท์ ํตํด ๋ค๋ฅธ ํธ์คํธ์ ํต์ ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
/** ๊ตฌ๊ธ ์คํ๋ ๋์ํธ pdf ๋ณํ*/
var theurl = '์ง๋๋ฒ์ ์คํ๋ ๋ ์ํธ pdf๋ก ๋ณํํ ๊ธ ์ฐพ์๋ณด๊ธฐ!'
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
- slack์ my app์ผ๋ก ๋ค์ด๊ฐ๋ฉด api token์ ํ์ธํ ์ ์๋ค. ์ฌ๋ ์ฑ๋ ID ๋ณด๋ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ๋ค.
- ์์ฑํ ์์ค์ฝ๋ ์์์ด๋ค.
/** ์ฌ๋ fileupload */
var metadata = {
token: "slack API ํ ํฐ",
channels: "์ฑ๋ ID",
filename: "ํ์ผ์ด๋ฆ.ํ์ฅ์",
title: "ํ์ผ์ ๋ชฉ",
};
var url = "https://slack.com/api/files.upload";
var boundary = "xxxxxxxxxx";
var data = "";
for (var i in metadata) {
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"" + i + "\"; \r\n\r\n" + metadata[i] + "\r\n";
}
data += "--" + boundary + "\r\n";
/** value1 */
data += "Content-Disposition: form-data; name=\"file\"; filename=\""+"ํ์ผ์ด๋ฆ"+"\"\r\n";
data += "Content-Type:application/ํ์ผํ์ฅ์\r\n\r\n";
/** value2 , file ์ฌ์ด์ฆ ๋งํผ */
var payload = Utilities.newBlob(data).getBytes()
.concat(docurl.getBlob().getBytes())
.concat(Utilities.newBlob("\r\n--" + boundary + "--").getBytes());
var options = {
method : "post",
contentType : "multipart/form-data; boundary=" + boundary,
payload : payload,
muteHttpExceptions: true,
};
ํ์๊ฐ ์์ ์์ ์ ํด๋ด์ ์ ๋ง ๋คํ์ด์๋ค๊ณ ์๊ฐํ๋ค. ๊ถ๊ธํ์ ์ฌํญ์ ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์๋ฉด ๋ต๋ณ์ ๋ฌ๊ฒ ์ต๋๋ค. ์ด ๋ค์์ ๋ ธ์ ์ฐ๋์ ๊ฐ์ง๊ณ ์ฌ ์์ ์ ๋๋ค.
๋๊ธ