DevSecOps | Slack Integration | Sagar Kakkala's World













Highly Recommend using repo here -Day15 to practise this project instead of code snippets, In case of confusion, Please do watch video that is explained in English, the code here in the blog is not changed to keep screenshots intact

Why is slack mostly preferred?

slack is preferred mostly for its ease of integration across other tools, for ease communication, for channels and communication made simpler

How do we send files from Linux server to Slack channel?

We will use slack bots for this purpose, we will be able to easily send alerts, files, and many more to slack channels

Create a Slack Channel

login to your slack channel and create an App



Now, name channel any name , you want



Create a Slack Bot

Now since slack channel is created, let us create a slack bot

click on your WorkSpace > Tools and Settings > Manage Apps


 Click on Build

click on create-new-app



click on "From Scratch"


Give any name of your choice and select workspace


Once created, you will be able to see the App this way


Now, we need to add Oauth permissions and in features tab on left hand side bar




Scroll down to Scope and click on "Add an OAuth Scope"


search for files and select files:write


Scroll up to OAuth tokens and you will find the button that says to Install to "your workspace name"


Once you click on install, you will be able to see Bot token, this bot token is important for us to authenticate from our linux server



Integrate Slack Channel with Slack Bot

Since we now have our Slack channel and Slack Bot ready, let us integrate them both

Go to your slack channel, and click on top of slack channel



Once opened, go to integrations tab and click on "Add an App"


You will be redirected to a Page, click on Add on the Slack Bot you created, in this case , it is test-alerts




once App is installed, you will now see this in Integrations Tab



Configuring Slack Alerts

Now before we configure slack alerts, please do make note of important data like Bot token which was generated at OAuth token

in our case it is xoxb-7895581536487-7913673982083-TShvbrM4nma2KmtTPk1ha78E

and channel id of slack which you will get once you go to about section and scroll down


so, for us channel_id is C07T8B8NWPK


Let us upload a Dummy data for testing now, 

echo "hello, this is a test" > test.txt



Deprecated Api - files.upload:

Slack used to have files.upload api which is widely popular and can send files directly from server to slack channel

https://slack.com/api/files.upload



Let us give a try using this Api

curl -F file=@test.txt -F channels=C07T8B8NWPK -H "Authorization: Bearer xoxb-7895581536487-7913673982083-TShvbrM4nma2KmtTPk1ha78E" https://slack.com/api/files.upload

Note: Replace Bot token and channel_id with your generated tokens


As you could see in above screenshot, it shows deprecated

Alternate API's used for slack file upload

We need to use two API's now to send files to slack channels

Step-1


we will be using API files.getUploadURLExternal to get an Upload URL and file ID

and also point to note that this new_api has some mandatory arguments , you can read more at files.getUploadURLExternal 




Now our command with new api updates this way

curl -X POST -H "Authorization: Bearer xoxb-7895581536487-7913673982083-TShvbrM4nma2KmtTPk1ha78E" -H "Content-Type: application/x-www-form-urlencoded" --data "filename=test.txt&length=$(stat -c%s test.txt)" https://slack.com/api/files.getUploadURLExternal

note: stat -c%s test.txt , tells the size of file in bytes

note: please do update with your bot token and channel_id


As you could see from above screenshot, that command generated an UploadUrl and fileId which we will be using in Step2 and Step3 respectively

Step-2

Now we need to upload our file in the Upload_url given by slack

curl -F file=@<file_path> -F "filename=<file_name>" <upload_url>

from above command, file_path is the file path in our linux server , file_name is the name of file that should show in slack server, upload_url is the url generated in previous command

Now our command would be modified this way

curl -F file=@test.txt -F "filename=test.txt" https://files.slack.com/upload/v1/CwABAAAAXAoAAcm4_GZ2AtHDCgACGACTAKXqDUgMAAMLAAEAAAALVDA3U0JIM0ZTRUILAAIAAAALVTA3U1ZLVFVXMkYLAAMAAAALRjA3U1QxU0w2NzcACgAEAAAAAAAAABYACwACAAAAFLlFFn-NPUpip_AW8unXV3Z9Vc8YAA



As you could see from Screenshot, it said OK which means our file is uploaded successfully

Step-3

Now our final step is to send the uploaded file to our slack channel, for this we use different slack api files.completeUploadExternal

We will use the file id generated in step-1 here for this Api

curl -X POST -H "Authorization: Bearer xoxb-7895581536487-7913673982083-TShvbrM4nma2KmtTPk1ha78E" -H "Content-Type: application/json" -d '{"files": [{"id":"F07ST1SL677", "title":"slack-test"}], "channel_id": "C07T8B8NWPK"}' https://slack.com/api/files.completeUploadExternal



Once, step-3 is complete, you will be able to see your files in Slack channel





In Summary, new process works this way, Generate Upload URL > Upload files in URL > Send Uploaded URL to Slack channel

Automate files upload of Slack using Bash

Since it is a 3 step process, and we cant manually check every upload URL and file ID for each file, best possible way is to automate this, please find bash script below


#!/bin/bash

#set the variables according to your tokens and ID's
SLACK_TOKEN="xoxb-7895581536487-7913673982083-TShvbrM4nma2KmtTPk1ha78E"
FILENAME="test.txt"
CHANNEL_ID="C07T8B8NWPK"

# Step 1: Get upload URL and file ID
response=$(curl -s -X POST -H "Authorization: Bearer $SLACK_TOKEN" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data "filename=$FILENAME&length=$(stat -c%s $FILENAME)" \
  https://slack.com/api/files.getUploadURLExternal)

# Parse upload_url and file_id from the response
upload_url=$(echo $response | jq -r '.upload_url')
file_id=$(echo $response | jq -r '.file_id')

# Check if both values are successfully retrieved
if [[ -z "$upload_url" || -z "$file_id" ]]; then
  echo "Error: Failed to retrieve upload URL or file ID."
  exit 1
fi

# Step 2: Upload the file using the upload_url
curl -F file=@$FILENAME -F "filename=$FILENAME" "$upload_url"

# Step 3: Complete the upload using the file ID and channel ID
curl -X POST -H "Authorization: Bearer $SLACK_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"files\": [{\"id\":\"$file_id\", \"title\":\"slack-test\"}], \"channel_id\": \"$CHANNEL_ID\"}" \
  https://slack.com/api/files.completeUploadExternal

echo "File upload completed successfully!"
This concludes our blog.


🔹 Important NoteAlso, before proceeding to the next session, please do the homework to understand the session better - DevOps Homework



I Post most of my content in Telugu related to contrafactums(changing lyrics to original songs),fun vlogs, Travel stories and much more to explore, You can use this link as single point of link to access - Sagar Kakkala One Stop

🖊feedback, queries and suggestions about blog are welcome in the comments.

Comments