Trigger a Power Automate Flow from a Slack Channel Message

Trigger a Power Automate Flow from a Slack Channel Message

I had a recent conversation with Subhramalya Ghosh on Power Automate's Slack connector, which highlighted the lack of a trigger for when new messages are posted. Microsoft may have intentionally excluded this, along with the lack of an action for dynamically searching users and sending direct messages, which I posted a solution to in this article.

Usually I can find relevant solutions in community forums or on the ‘I Love Automation’ Discord channel, but in this case I couldn't find a single answer. Hence I'm writing this post—to share my own solution and hopefully help others trying to get past the same limitation.

Requirements

  1. Power Automate Premium - HTTP actions use premium connectors
  2. Your own Slack instance with permission to set up new apps
  3. General knowledge of APIs and Power Automate HTTP connectors

Building the Process

In this example, we have a vendor who posts details of new sales to one of our Slack channels. The message format is structured and consistent. We want to set up a Power Automate flow that triggers whenever a new message is posted in this channel. The trigger should capture the message content so we can use it to feed other processes.

The structure of the Slack message will be:

"Company Name: ABC Corp LTD

Sale Value: $25,000.00

Commission %: 0.08

Address: 123 ABC Way, San Francisco, CA 99999

Policy Number: POL1234"


Building the Solution

1. Setting Up Your Slack App

First, you’ll need to create a Slack app that can listen for messages and send event notifications to Power Automate using Slack's Events API. This involves adding a 'Bot' to the channel you want to monitor. The bot requires the channels:history scope, allowing it to access the channel's message history and trigger actions when a new message is posted.

  • Create a Slack App: Go to the Slack API home page and create an app. Give it a name and associate it with your workspace. You can also update an existing app, but for this example we'll create one from scratch.

  • Enable Event Subscriptions: Navigate to 'Event Subscriptions' and toggle 'Enable Events' to 'On'. Under 'Subscribe to Bot Events', add the 'message.channels' event. This event triggers whenever a message is posted in a channel where your app is a member.

  • Creating the Request URL: Create a new Power Automate flow and set the trigger action to "When an HTTP request is received." Under "Who can trigger the flow?", select "Anyone." Add a "Parse JSON" action and use the example JSON provided below to generate the Schema. This step is important because Slack will first verify that you 'own' the endpoint by sending a request and expects a response with the 'challenge' value. This only needs to be done once.

{"token": “ABCDE1234", "challenge": "xV2595E9CY3gmdALWMmHkvFXO7sdfbAD8P", "type": "url_verification" }        

The final step is to add a 'Response' action as in the above image. Set the Status Code to 200, leave 'Headers' blank, and in the 'Body', insert the 'challenge' dynamic content from the 'Parse JSON' action. Save the flow to generate the HTTP POST URL. Then, return to Slack's Event Subscriptions page and paste this URL into the 'Request URL' field. Slack will send a verification request, and your flow’s response will complete the setup.

  • Install the App: navigate to the 'OAuth & Permissions' tab, and select 'Install to Workspace'

3. Adding the Slack App to the Channel

For your app to receive messages from a channel, it must first be added to that channel. Click on the channel name, select 'Integrations'. Search for the app you installed and add it.

4. Updating the HTTP Request Flow to Handle the Slack Payload

Now that the app is configured and listening for message events, we can update the Power Automate flow to receive the standard Slack message payload. Change your existing Parse JSON Schema using the below example from the message.channels documentation.

{
    "token": "one-long-verification-token",
    "team_id": "T123ABC456",
    "api_app_id": "A123ABC456",
    "event": {
        "type": "message",
        "channel": "C123ABC456",
        "user": "U123ABC456",
        "text": "Live long and prospect.",
        "ts": "1355517523.000005",
        "event_ts": "1355517523.000005",
        "channel_type": "channel"
    },
    "type": "event_callback",
    "authed_teams": [
        "T123ABC456"
    ],
    "event_id": "Ev123ABC456",
    "event_time": 1355517523
}        

The 'Response' action is no longer needed so we can delete it. At this point you can use the request data however you like, but we will continue with the example case. We now want to parse the data and capture all the 5 datapoints sent. After the 'Parse JSON' action, add three 'Compose' actions as follows. The final action in the below example for 'Company Name' can simply be repeated for each of the other datapoints.

Here are the expressions in text:

split(body('Parse_JSON')?['event']?['text'],outputs('New_Line'))
substring(outputs('Split_into_Lines')[0],14)        

5. Testing the Flow

Open up the Slack channel where we added our app/bot. Post a message in the format expected--see the example at the beginning of this article. This will trigger the flow, and we receive the data we needed. Below is output from my test.

5. Security and Other Considerations

This flow can be triggered by anyone with the URL, so it's not exactly secure. Additionally, it will pick up every message posted in the channel, which might not suit your needs. An option is to add Trigger Conditions in the trigger’s settings. For instance, to restrict the trigger to a specific bot or user, use:

@equals(triggerBody()['event']['user'], '[Member ID]')        

where [Member ID] is the user’s unique ID. For added security, require the correct token with:

@equals(triggerOutputs()?['body']['token'], '[your unique token]')        

The token is included in every event request header and is unique to your Slack app.


I hope you found this helpful! If you did, please leave a comment or reaction to let me know. Feel free to comment or message me if you have any questions. I’ll be sharing more solutions for scenarios where clear answers are hard to find, so be sure to follow me to stay updated!

Andy Hill

Lead Infrastructure Engineer at Companies House

1 个月

Hi, I followed all the instructions, but when I post a message into the channel where I have installed the app, the power automate flow does not get triggered... Any ideas ?

回复
Andy Spender

Knowledge and Engagement Manager at Elsevier

3 个月

This article is really helpful. Thank you for posting it.

回复
Matthew Arnott

Dynamics 365 Programme Manager

5 个月

Great article - exactly what I needed and couldn't find elsewhere. Worked the first time. ??

Tomas Suarez

Ingenieria de Datos y Devops | Inteligencia de Negocios | Computacion en la Nube ??

7 个月

The HTTP request action is amazing because it lets you connect to almost anything, enabling powerful integrations as you’ve shown here!

Subhramalya Ghosh

Power Platform /SharePoint Developer

7 个月

That's great! Thanks ?? These are standard functionality which must be included as standard connectors, I hope Microsoft implement this pretty soon

要查看或添加评论,请登录

Lance Raeper的更多文章

社区洞察

其他会员也浏览了