This sample script is for uploading image files to Slack using Incoming Webhooks by Google Apps Script.
When users try to upload image files to Slack using Incoming Webhooks, it has been known that although the access token is required to directly upload them, Incoming Webhooks can upload them by using the tag of image_url
. In this sample script, it uploads image files (BMP, GIF, JPEG and PNG) on Google Drive to Slack using Incoming Webhooks. The script is written by Google Apps Script.
In this sample, It supposes that there are image files on Google Drive.
var fileId = "#####"; // File ID of image file
var url = "https://hooks.slack.com/services/#####"; // Incoming Webhooks URL retrieved at Slack
var file = DriveApp.getFileById(fileId);
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
var res = UrlFetchApp.fetch(url, {
method: "post",
payload: JSON.stringify({
"channel":"#####", // Channel name or channel ID
"username":"Sample Image",
"text":"Sample Text",
"attachments":[{
"image_url": "http://drive.google.com/uc?export=download&id=" + fileId
}]
}),
muteHttpExceptions: true
});
Utilities.sleep(3000);
file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.NONE);
- Modify permission of the image file to
Access.ANYONE_WITH_LINK
andPermission.VIEW
. - Make Slack download the image file from Google Drive by post request.
- Wait for 3 seconds until the download is finished.
- Undo the permission of the image file to
Access.PRIVATE
andPermission.NONE
.
About uploading image file to Slack, when the upload was finished, even if the image file on Google Drive is removed, the image file on Slack is not removed.
not work for me. not send with image