Search code examples
slackslack-api

How to upload a file to a slack channel using a bot


I have a slack bot and the token starting with xoxb is used to upload a file to a channel.

I am using below format

curl -F token="${SLACK_TOKEN}" -F file=e2e.sh -F channel="${SLACK_CHANNEL}" -F  as_user=true https://slack.com/api/files.upload

This throws

{"ok":false,"error":"no_file_data"}

Solution

  • You are missing the @ in your file=e2e.sh argument to let curl know you want to transmit a file. The following should do the trick:

    curl \
      -F token="${SLACK_TOKEN}" \
      -F [email protected] \
      -F channel="${SLACK_CHANNEL}" \
      -F as_user=true \
      https://slack.com/api/files.upload
    

    p.s. Breaking a long curl into multiple lines can help you see things more clearly ;)