Posting messages into a Slack channel from Node-RED

Hi there,

This is a safety feature for a science lab,
I have a flow set up that monitors water flow from a tap (for water cooling chemistry experiments), it is read as a frequency. The whole purpose of it is if the water is accidentally turned off, or water is shut off to the building it then turns off the power to the heating mantles via a tp-link switch (using the Kasa node).

I have that part of the flow all working correctly and tidy. I would now like to send a message to a Slack channel to tell all users the water has been turned off. We use slack as our work messaging system, so this is very integrated into my workplace.

Could anyone explain how to get Node-RED using nodes to trigger a message in a specific Slack channel? The easiest method is best, I can use IFTTT or Slack API, just need some guidance.

I am not a coder or computer engineer, and I have been getting rather confused with all the new terminology I have come across, (Webhooks, OAuth Tokens, Slack API, Scopes etc)

Thanks in advance for any help or clarity on the issue
Bryce

Nothing in the flows site that would help?

Library - Node-RED (nodered.org)

Hi Bryce,

I'm using the slack-web-out node from this pacakge node-red-contrib-slack (node) - Node-RED

The node itself is simple enough to setup you to send in the msg.topic as the channel name you want to post to eg #general (with the #) and then msg.payload is the text of the message.

The auth is a little more complex as Slack have been through a number of ways of doing things over the years but I've just tested this method and its probabbly the simplest for your use case.

(You will need to be an admin on the slack workspace you want to setup against) goto https://api.slack.com
You'll need to create a new app, then install that app onto your workspace and then in the oAuth section you should be able to create a bot user and that user will have a token. That token is what you need to setup the config in the slack node.

Hi Sam,

Thank you so much, that has helped.
I have been promoted to admin of the Slack workspace, where I have created a channel called lab_notifications.

Do I then use a function node with the following code?

if (msg.payload == 0) {
msg.payload = "Water Flow has stopped, Power has been turned off";
}
msg.topic = "#lab_notifications";
return msg;

If not, what do I use to inject the text I would like to appear in the Slack channel into the slack-web-out node

  1. I've built an app in the Slack API, and under the "Bot Token Scopes"
    I have added:
    -chat:write
    -incoming-webhook as its two OAuth Scopes.

  2. I installed it to a workspace, and gave it permission to post in the #lab_notifications channel, which gave me a Bot User OAuth Token, which I copy and pasted into the slack-web-out node in the Token field

Do I need to do anything with a Webhood URL?

I still can't get it to post, if possible could you give me more instructions and steps to follow, this is all new for me and very confusing

Thanks heaps for helping me
Bryce

I think I have figured it out. It took a small amount of code in a function node for it to send through to my Slack channel.

If anyone else find this thread or needs the solution that worked for me, then this is the code I used:

msg.topic = "chat.postMessage";
msg.payload = {
channel: "#lab_notifications",
text: "Water Flow to the LARGE soxhlets (Metals Lab) has stopped. Power to its heating element has been turned off"
}

return msg;

Using the Bot OAuth Token from the App I made, it all seems to work well. Thanks for pointing me in the right direction. Much appreciated !!

Glad you've worked it out, according to the help a simple message posting should work with just the channel in the topic field, (thats what I have) but this will also do it, I wonder if it makes a difference if the bot is added to the channel you're posting too.

You don't have to use a function node to setup your message either, you could do this with a change node or (for testing) with an inject node and set the properties within that node.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.