Saifis
Saifis

Reputation: 2237

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

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?

Upvotes: 61

Views: 66455

Answers (3)

PiotrWolkowski
PiotrWolkowski

Reputation: 8782

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.

Upvotes: 0

Wesley Cheek
Wesley Cheek

Reputation: 1696

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",
  },
});

Upvotes: -1

Erik Kalkoken
Erik Kalkoken

Reputation: 32698

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.

Upvotes: 57

Related Questions