How to create an awesome slack approval workflow
Tags: slack, frontend, tutorial, productivity
Published: 2021-12-18 23:29:44

Slack is a very popular workplace place, to a point where now it’s become a verb — “**Slack me if you need something!**” — and there’s good reason for it. During these pandemic times, it’s so much more than just a chat app. 

Today, we’ll explore how we can do even more with **Slack Approvals**. We’ll bring in [Canonic](https://canonic.dev/?utm_source=dev.to&utm_medium=organic_article&utm_campaign=30daysofcanonic_uc_3&utm_term=header_canonic&utm_content=day_20) and use the power of slack approvals to approve or disapprove our message. We want the ability that whenever a message is added to the database, its respective slack approval message is sent, based on the response, the same gets updated in the database.

## Let’s dive in! 🚀

**Step 1: Create necessary slack app & generate tokens**

Let’s start with getting all the slack requirements in place. We have to [create an app with Slack](https://api.slack.com/apps) and generate its respective tokens.

For this step, we’ll follow the slack guides linked with the respective text as they’ll be the best source to get all these requirements.

![Step 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7tv7zmwvc1el1cne1gg.png)
 
**Step 2: Starting with a slack integration workflow**

Jump to Canonic and clone this [sample project](https://app.canonic.dev/projects/61be5cc3b6586d0026b16023/graph?utm_source=dev.to&utm_medium=organic_article&utm_campaign=30daysofcanonic_uc_3&utm_term=step2_sample&utm_content=day_20) which already has slack integration added, or create one for yourself and move to the API section to manually add the slack integration for your `createMessage` API.

![Step 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqrouqqxq3s06axdz6kv.png)
 
You will be asked to authenticate and add in a token (the one that we got in step 1) Once authorized you should have a green tick to reflect that.

![Step 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfu9p11rex8cq23yqpaj.png)
 

**Step 3: Slack payload creation**

Now that we have our integration set up complete and attached to our `createMessage` API. Whenever we trigger this API, an approval message with the trigger message should be sent to slack. 

For this, we need to **add our payload to slack**. For using this feature, we’ll use a slightly different payload than we usually do for a simple chat message. Our payload should look something like this:

```json
{
 	"text": "New approval request",
	"blocks": [
		{
			"type": "header",
			"text": {
				"type": "plain_text",
				"text": "New request",
				"emoji": true
			}
		},
		{
			"type": "section",
			"fields": [
				{
					"type": "mrkdwn",
					"text": {{ title }}
				},
			]
		},
		{
			"type": "actions",
			"elements": [
				{
					"type": "button",
					"text": {
						"type": "plain_text",
						"emoji": true,
						"text": "Approve"
					},
					"style": "primary",
					"url": "click_me_123"
				},
				{
					"type": "button",
					"text": {
						"type": "plain_text",
						"emoji": true,
						"text": "Reject"
					},
					"style": "danger",
					"url": "click_me_123"
				}
			]
		}
	]
}
```

Here, in place of `click_me_123`, we’ll add the URL for the respective API to get triggered when someone approves or disapproves it. For that, we’ll need to build a custom API. Let’s do that.

**Step 4: Create custom API for approving/disapproving messages**

We now create a custom API in Canonic to look for the query parameters and update the respective record. Create a new API by clicking on the `+` icon against the `Endpoints` tab.

Next, add details regarding the API.

![Step 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2cjkmbsxvod6uba3wn4.png)
 

Go to the Code tab and add the following code to find the id for the message and add in the approval or disapproval in the database. We use query parameters to fetch the details then do the processing.

```jsx
module.exports = async function endpoint({ query }) {
  const { id, approved } = query
  return Message.findOneAndUpdate({ _id: id }, { $set: { approved: approved }}, { upsert: true })
}
```

**Step 5: Update back the message**

We deploy this project to get the API URL. Click on the top right corner saying `deploy`, then go to the docs to get its URL.

We replace `**click_me_123**` to**`https://messages.can.canonic.dev/api/messages/approvalornot?id=1234&approved=false`**  in the danger button and replace the same with **`https://messages.can.canonic.dev/api/messages/approvalornot?id=1234&approved=true`.** 

Now whenever a user will approve or disapprove, the following URL will get triggered saving the approval of the message in the database. This can be used in place of any Micro-service which is built is to handle slack integration or become a slack server for that business.

Hope this guide helps you make your day a little bit more productive!
Check out the sample project [here](https://app.canonic.dev/projects/61be5cc3b6586d0026b16023/graph?utm_source=dev.to&utm_medium=organic_article&utm_campaign=30daysofcanonic_uc_3&utm_term=step5_here&utm_content=day_20)

## Conclusion

Thank you for reading this guide. If you happen to create any of these integrations, please remember to mention us on social media or drop us a line on our discord channel.

You can also check out our other guides [here](https://dev.to/canonic/).

Join us on [Discord](https://discord.gg/9dyytsM) to discuss or share with our community. Write to us for any support requests at [support@canonic.dev](https://support@canonic.dev/). Check out our [website](https://www.canonic.dev/30-days-of-canonic/?utm_source=dev.to&utm_medium=organic_article&utm_campaign=30daysofcanonic_uc_3&utm_term=conclusion_website&utm_content=day_20) to know more about Canonic.