Is it possible to post files to Slack using the incoming Webhook?
Asked Answered
M

3

61

I am trying out the Slack's API using the incoming webhook feature, posting messages works flawlessly, but it doesn't seem to allow any file attachments.

Looking through I understand I have to use a completely different OAuth based API, but creating more tokens just for the purpose of uploading a file seems odd when posting messages works well, is there no way to upload files to slack with the incoming webhook?

Manion answered 2/3, 2016 at 9:11 Comment(0)
H
57

No, its is not possible to upload files through an incoming Webhook. But you can attach image URLs to your attachments with the image_url tag.

To upload files you need to use the Slack Web API and the files.upload method. Yes, it requires a different authentication, but its not that complicated if you just use a test token for all API calls.

Hortatory answered 29/3, 2016 at 14:44 Comment(7)
Yes but the test token is intended for testing and development only, while a webhook could be used in a production environment. That's unfortunate that webhook do not support sending files :(Hasheem
I understand your point, but that is not how webhooks and test tokens are used in Slack. Webhooks give limited access to Slack, mainly to send and receive messages. Tokens give access to the Slack API, which has a much broader scope of functions, including sending messages and uploading files. If you want a simple url for uploading files I would recommend creating a small php script for that. You can use it as an "webhook" and the script can handle the actual file uploading to Slack through the API.Hortatory
A script does not make things much simplier than OAuth... But I agree and quoting you Webhooks give limited access to Slack, mainly to send and receive messages. For me, sending a message may mean also sending file. There are the message "attachments" supported in the API, but these attachments are usually too basic for complex formatting.Hasheem
I was in the same situation. I think Slack should slow down their rate of innovation - on one hand they have this scare that Tokens are obsolete, and on the other hand - such basic thing as attaching one binary file is not there yet on the new side, the webhook / slack app. If they'd bring new feature to 100% before urging people to abandon ship on the previous gen tech, that would be much appreciated.Melindamelinde
@Jun Sato. None of your claims make any sense as far as I can see. 1) webhooks are actually the older technology and where never supposed to support files 2) Tokens are not becoming obsolete, its only the legacy token - an older variant - that is no longer recommended due to its inherent security risk. The user token and bot tokens are default these days.Hortatory
@ErikKalkoken okay, fair enough, but none of that's apparent through documentation (mostly because the new and the old are mixed). So simply put, what do I do these days if I wanted to post files to slack the right way? That's the information I'm looking for.Melindamelinde
Agreed that the mixture of new and old in the documentation can be very confusing. To your question: To upload files use the API method: files.upload. Please create a new question if you have specific questions about how it works. Here is an example in CURL: #53229631Hortatory
V
0

As per the answer above you need to handle that through the API call: to files.upload

But the test API keys have been deprecated and cannot be generated anymore.

To get the Key you need to:

  1. Go to Your Apps in Slack API documentation.
  2. Create a new app.
  3. Go to the app and go to OAuth & Permissions section
  4. There you will find Bot User OAuth Token. It should start with xoxb-...

For posting the files you need to add files:write permission to the Bot Token Scopes section. Note that if you do that on existing bot, when changing the scope the bot needs to be reinstalled in channels.

Once that's configured you can try your connection:

curl -F [email protected] -F "initial_comment=Shakes the cat" -F channels=C024BE91L -H "Authorization: Bearer xoxb-xxxxxxxxx-xxxx" https://slack.com/api/files.upload

You can find the channel ID in Slack in Channel details.

Vindication answered 12/3 at 10:13 Comment(0)
S
-1

You can see in the Slack API document that it's easy to add an attachment to the POST message to your webhook. Here is a simple example of sending a text message with an attachment in NodeJS:

import fetch from "node-fetch";

const webhook_url = "https://hooks.slack.com/services/xxxx/xxxx/xxxxxxxx"
const url = "https://1.bp.blogspot.com/-ld1w-xCN0nA/UDB2HIY55WI/AAAAAAAAPdA/ho23L6J3TBA/s1600/Cute+Kitten+13.jpg"

await fetch(webhook_url, {
  method: "POST",
  body: JSON.stringify({
    type: "mrkdwn",
    text: "Example text",
    attachments: [
      {
        title_link: url,
        text: "Your document: <file name>"
      },
    ],
  }),
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
  },
});
Studer answered 22/7, 2022 at 0:5 Comment(1)
here an "attachment" is not a file as asked by OPBugbane

© 2022 - 2024 — McMap. All rights reserved.