Jan 14, 2023. 5 min

How to Create a Slack App to Send Notifications

Overview

In this blog, you will learn how to create a Slack App and send notifications into a different workspace.

What is a Slack App?

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.

How to Create a Slack App?

To create a Slack app, you will need to sign in to your Slack account and follow these steps:

  • Go to the Slack API website ( https://api.slack.com/apps ).
  • Click the "Start Building" button in the top right corner of the page.
  • Please enter a name for your app and select the workspace where you want to develop it.
  • Click the "Create App" button.
  • You will be taken to the basic information page for your app. Here, you can enter a description and logo for your app, as well as set the app's primary color.
  • To add functionality to your app, you will need to create a "bot user" that can be used to interact with users in Slack. To do this, click the "Add a Bot User" button.
  • On the "Add a Bot User" page, enter a name for your bot user and click the "Add Bot User" button.
  • Once your bot user has been created, you can start building your app by adding the Slack API functionality.
After creating the app you will get a CLIENT_ID and CLIENT_SECRET, which you can grab from this screen.

This APP ID, CLIENT ID and CLIENT SECRET will be required in the later phase of this tutorial.

Set up OAuth and Permissions

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.

How To get OAuth Tokens from other Workspace Users

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.

Curious about AIOps?

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

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!