Trigger a Power Automate Flow from a Slack Channel Message
Lance Raeper
Insurance Operations Lead | Power Platform Consultant | Accredited Advisor in Insurance | Tech and AI Driven Process Improvement
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
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.
{"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.
领英推荐
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!
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 ?
Knowledge and Engagement Manager at Elsevier
3 个月This article is really helpful. Thank you for posting it.
Dynamics 365 Programme Manager
5 个月Great article - exactly what I needed and couldn't find elsewhere. Worked the first time. ??
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!
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