Logic App and Slack - Sending messages to slack channel | Logic app and slack integration | Connecting Logic App to Slack channel

{tocify} $title={Table of Contents}


Introduction

In most of the interfaces there is a need send some alert message to stake holders notifying about some errors or about some specific conditions.

Recently, in one of the interface the ask was to send alert messages(warning/issue/errors) to a slack channel where the business users were able to get notified instantly.

What is Slack Channel?

Slack is a messaging app for business that connects people to the information they need. (On same line as Microsoft Teams)

And Slack channel is a dedicated space for conversations that can be created for any project, topic, or team

 
So how do we integrate Logic app with Slack channel? Is there a out of box connector available for Slack?

Ways to connect to Slack channel


There are couple of ways to go about it, 

1. Using connector

Yes, there is a connector provided by Microsoft to connect to Slack.

Before we use connector, let's first create a workspace in Slack. Go to slack (https://slack.com/intl/en-in/get-started#/createnew)

Enter your email address and continue. Provide the code received over email and continue.

Create Slack workspace

On the first step, provide the name of your workspace 

create slack workspace profile

Provide your name and photo(optional)

create slack workspace add members

Add team members in the workspace

create slack workspace add channel

In the last step provide the name of channel. And we are ready to use it.

We will post message to this channel from Logic app using the slack connector

Let's create logic app

Create logic app

Create logic app, provide name, region and select plan type.

Add http trigger in logic app

For the sake of demo, creating a simple logic app which will be triggered by http request and will post to slack (same message received over http trigger).

Add new action and search for Slack. Select Post message (V2) from Actions

Add slack connector in logic app

The screen will ask you to Sign in , click on it

Allow logic app access to slack

Click on Allow to permit Logic App to interact with Slack workspace

Select the channel and message to post in Slack channel

Once connected to the workspace, Configure Post message slack action

Configure Post message slack action

select the Channel name(demoproject) where you want to post messages and provide value against Message Text(body of http request)

Save the workflow and that's it, ready for testing. 

Now run the logic app, with some data . And check the slack channel if message is reached.


message reached to Slack channel

As can be seen in above image, message reached to Slack channel which was sent by Logic App.


2. Using Http action (calling slack channel webhook)

Another option is to use http action in logic app and interact with slack channel, but for that we need to create an App in slack and webhook on top of slack channel.

Go to https://api.slack.com/apps

create slack workspace app

Click on Create an App to create slack workspace app

create an app from scratch

create an app from scratch

Provide name for App and select workspace

Provide name for App and select workspace for which you want to create app

Select Incoming webhooks

App is created, Select Incoming webhooks

Select Incoming webhooks and make it ON


Select Incoming webhooks and against Activate Incoming Webhooks - ON it
Select new webhook to workspace

Click on Add New Webhook to Workspace

Grant permission to the App

Grant permission to the App ,select the channel for which you want webhook to be created.

webhook url generated

Copy the webhook url and use it in Logic app to post messages

We will post message to this channel from Logic app over the webhook url

Let's create logic app


For the sake of demo, creating a simple logic app which will be triggered by http request and will post to slack channel over the webhook (same message received over http trigger).

Configure slack webhook url in logic app http action

Followed by Http action, with method as POST, configure slack webhook url against URI and against Body -

                                            {
                                                  "text": "@{triggerBody()}"
                                            }

So whatever is received over http request is pushed to slack channel.

Note: using text is mandatory and payload should be valid JSON, else it will not be accepted.

Save the logic app and test it.

Testing slack webhook url

If you see in above snap, it shows that message is posted by DemoProjectApp and not Logic app. 

It is because we had created the webhook for channel through this App, and the url generated is associated to this App.


Summary

We saw that we have two options to post messages to slack channel and off course this will raise a question - which one to use?

What I observed is that - with slack connector you have to manually authorize every time you deploy the logic app to another environment .

authorization every time you deploy



And that is not required in case of webhook url, as the auth code is already part of the URL .

So, my preferred option is go with Webhook way !!!

Learn More about Logic App


1 Comments

If you have any suggestions or questions or want to share something then please drop a comment

  1. Very well drafted article, it opens minds to many new possible scenarios. Will explore if similar things can be achieved via teams. Thanks for writing this and many others blogs, they are always very helpful and delight to read through.

    ReplyDelete
Previous Post Next Post