AI-powered Kubernetes
Jan 27, 2022. 12 min
In this blog, you will learn how to create a Slack App and send notifications into a different workspace.
In Slack, an app is a custom integration that you can build to bring your own functionality and features into Slack. Apps can be used to automate tasks, provide custom functionality, or allow users to interact with external services or tools from within Slack.
Apps can be installed in individual Slack workspaces, and they can perform a variety of different tasks, such as sending messages, reading data from external sources, and reacting to events that happen within Slack. Slack provides a set of APIs (Application Programming Interfaces) that you can use to build and customize your own apps.
To create a Slack app, you will need to sign in to your Slack account and follow these steps:
This APP ID, CLIENT ID and CLIENT SECRET will be required in the later phase of this tutorial.
In the previous steps, you created a Slack App. Now you need to set up some OAuth permissions. To give OAuth permission you need to go to the details of the app and then from the left navigation menu click on “OAuth and Permissions”.
In this screen, add some OAuth scopes. Like the basic ones of “incoming-webhook”. Once you have added the required scores, please add a redirect URL and save the changes. Remember that, Slack will use your redirect URL to redirect the user with the OAuth token added as a query parameter.
The next step is to go into the “Manage Distribution” menu from the left navigation panel and “Activate Public Distribution” for your app. Before you activate the public distribution, you need to tick the check box that you have removed any hard-coded information from the “OAuth permissions” and Redirect URL.
Once the Public Distribution is activated for the app, then you need to collect the shareable URL from the screen below.
Remember that we have taken a sharable URL in the previous screen from the “Manage Distribution” menu, so now we are going to use that sharable URL to grab OAuth tokens from other Workspaces / Users.
So the basic idea is, to have a button that says “Authorize” and clicking that button will redirect the user to the authorization screen, which is something like this.
There is an “Allow” button on this screen which allows our app into this workspace, which can be used to send messages to a channel that which user has selected in the dropdown. After clicking the “Allow” button, Slack will redirect the user to the redirect URL which we defined earlier. Slack will also append the OAuth token as a query parameter in the redirect URL.
We need to use that OAuth token to grab the webhook, which we will use to push messages to the Slack channel.
A sample code is given below to get the webhook URL and then use that URL to push messages
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
try:
code = "OAuth token received in the redirect URL"
# An empty string is a valid token for this request
client = WebClient(token="")
CLIENT_ID = "{CLIENTID}"
CLIENT_SECRET ="{CLIENTSECRET}"
# Request the auth tokens from Slack
response = client.oauth_v2_access(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
code=code
)
print(response['incoming_webhook']['url'])
#print(response.incoming_webhook)
except SlackApiError as e:
print("Error sending message: {}".format(e))
The output of this script will give a webhook URL, which you need to store in the database for further use./p>
The next script actually usages the webhook URL which is generated previously and pushes a message to the Slack channel.
import json
import requests
webhook_url = "{webhook url received in the previous script }"
# Set the request payload
payload = {
"text": "alert notification testing with slack "
}
# Send the request to the webhook URL
response = requests.post(webhook_url, data=json.dumps(payload))
# Print the response status code
print(response.status_code)
If the script runs without any error that means you have successfully pushed a message to the related slack channel.
In conclusion, creating a Slack App and sending notifications to a different workspace is a straightforward process that involves setting up a new Slack App, configuring the app's permissions, and using the Slack API to send messages to a specified channel in the target workspace. By following the steps outlined in this article, you can effectively communicate with users in different workspaces and enhance your team's productivity and collaboration.
Solve problems in seconds with a comprehensive AI-powered observability solution.
Did you know that CloudAEye supports Slack, Email, SMS, webhook, and SNS for notifications? Request a free demo today!
Atiqur Rahman works as a Pricipal Engineer at CloudAEye. He graduated from BUET, the top engineering university in Dhaka, Bangladesh. He is an AWS certified solutions architect. He has successfully achieved 4 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. He has more than 12 years of working experience with designing complex SaaS applications. His YouTube channel has over 3K subscribers!