Slack: Send Attachments Using Python WebClient
In this article, we shall see how to Send attachments in Slack Channel using python.
First let’s see Important steps in a summarized way after that we shall discuss these things in a detail way. So let’s start.
- Login and Create a new workspace and Channel in Slack
- Create a new app
- Add scope/permission to newly created app
- Install App to Workspace
- Add App in Channel
- Get Token
- Use Slack Web Client to send attachments
Now, Let’s see each step in detail.
- Login and Create a new workspace and channel in Slack
Create workspace and then provide name of your team as required below.
Then provide your teammates email to include/invite them to work with you. You can also skip this step for now.
Now provide project name that will work as channel known in Slack.
Finally, a new channel named as #projectname will be created in newly created slack workspace and following interface will be opened.
2. Create a new app
After creating Channel in workspace, create a new app. For that open app directory that can be seen in under Browse Slack dropdown’s Apps. You can see App Directory hyperlink in right corner.
When app directory is opened then following page can be seen.
In the above image your Team name(Demo Team) can be seen. If you have multiple teams you can select accordingly from dropdown.
From here click on Build button, it will open following page.
Now click on Create App button. Select From Scratch option and provide name of your app and also make sure that select workspace is the same where you want to create that app. (See below images)
Hurrah! You App has been Created.
3. Add scope/permission to newly created app.
This is the most important step to send messages and attachments using python (or in general we can say using bot).
When app is created then following page will be opened. Open Permissions section and two scopes as shown below.
Please notice I have added two scopes: chat:write (for sending messages), files:write (for Upload, edit and delete files).
4. Install App to Workspace
We have successfully created app and added scope permissions. After saving following page can be seen.
Open Your Apps and you can see all of your created apps listed here. Open you respective app and install that App to your workspace.
Click on Install to Workspace button. It will ask for permission as below, Allow it.
5. Add App in Channel
Open your channel and click on channel name as shown below.
When you click on highlighted #projectname it will open a box containing integrations option (See below).
Now Click on Add an App button to add app in the channel. This will open a page where you can search for your app to add (See below).
Click on Add button to add app in channel and you have successfully added your app to channel.
6. Get Token:
Open Your apps page again and search for Oauth and Permissions tab, open it and you can see Bot User OAuth Token that is the token to be used in you python code so note it down and keep it private.
7. Use Slack Web Client to send attachments
Now let’s come to the code part.
Following is the snippet of Code to send attachment to Slack using python using Web Client. You can get code on Github here. If you have no slack-sdk library then install it using pip install slack-sdk
One thing important here is that you have to get your token from Slack(that we got from previous step).
Let the code run and see the results. Code is self explanatory so I am not explaining that part.
Thanks for Reading! Happy Learning!