Skip to content

Instantly share code, notes, and snippets.

Created September 7, 2017 03:55
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Uploading Image Files to Slack Using Incoming Webhooks by Google Apps Script

Uploading Image Files to Slack Using Incoming Webhooks by Google Apps Script

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.

Script :

var fileId = "#####"; // File ID of image file
var url = ""; // 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",
      "image_url": "" + fileId
  muteHttpExceptions: true
file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.NONE);

Note :

  1. Modify permission of the image file to Access.ANYONE_WITH_LINK and Permission.VIEW.
  2. Make Slack download the image file from Google Drive by post request.
  3. Wait for 3 seconds until the download is finished.
  4. Undo the permission of the image file to Access.PRIVATE and Permission.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.

Copy link

Hi Kanshi. Thanks for sharing this. It is very useful for me. Am trying to get it to work.

The download of the image from Google Drive does not seem to work. I have tried longer waiting times to allow download to finish.

Has been a couple of years since this post. Not sure if anything has changed or if I don't understand concept fully.

Interesting, the preview image shows on the slack channel but when I click on the image I get the attached image.

Any guidance is much appreciated. Sorry to bother on this.


no image

Copy link

Would you mind clarifying whether this uploads the image file to Slack or posts a message with a link to the image URL?

Copy link

I think its uploading image url

"attachments":[{ "image_url": "" + fileId }]

which is against the title of this thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment