An alternative way to integrate with Slack using Node-RED

Do you want to make an integration with Slack?
Do you already know Node-RED but the solutions that appear in the documentation and the tutorials are not working?

I’ve been there and I passed through that.

Table of contents

Context
Pros and…


This content originally appeared on DEV Community and was authored by Ignacio Mattos

Do you want to make an integration with Slack?
Do you already know Node-RED but the solutions that appear in the documentation and the tutorials are not working?

I’ve been there and I passed through that.

Table of contents

  1. Context
  2. Pros and cons using Node-RED
  3. Using a simpler node
  4. Conclusion

Context

A few weeks ago I started working in a Slack chatbot which should answer some questions. The idea was good and the tool that was suggested to me to solve this - we talk about Node-RED - has a lot of examples on the web where it actually works.
But for some reason they didn’t work for me. I don’t know if the solutions proposed on the web are outdated or if I’ve been missing something important (of course this is something probable). The thing here is that, as developers, we are supposed to either reuse or to create new solutions.

And after a while, I finally managed to connect with the Slack API, but not in the way it was supposed to according to the doc.

In this post I’m going to show you how I solved this out hoping this might be reusable for other similar scenarios.

Pros and cons using Node-RED

Something really helpful about Node-RED is that it’s really worth it as it saves a lot of time that you would spend writing down every function you need. There’s a pile of useful nodes that facilitate the integration with external APIs.

The problem is that once you let the app manage your code, you’ll start wondering what is actually occurring and how things happen.
Leaving aside the annoying or not this could be, the big problem will show as the bugs appear and you don’t know what the node function is doing, so it is double tedious to fix them.
I mean, it is a problem to don’t know if you are missing a semicolon or if the auth token is wrong.

That is exactly what happened and what pushed me to find an alternative solution to slack-rm node or the slackbot listen, which were the ones proposed everywhere I searched for.

Using a simpler node

There’s a node called http in which listens to API calls. The advantage of using this one is that we can directly test the endpoint services using either curl or postman. This lets you know if you have an error in the request, I mean, before adding some logic.

Let me show you how to set it up:

Alt Text

Now we can test this from outside the app with the debug node, using curl -X POST http://localhost:1880/postmessage

The next step here is using a function node, in which we’ll set the headers and body of the request:

const BOT_TOKEN = "xoxb-...";
const CHANNEL_ID = "your_channel_id";

msg.headers = {
    "Authorization": `Bearer ${BOT_TOKEN}`,
    "Content-Type": "application/json; charset=utf-8"
};
msg.payload = {
    "channel": CHANNEL_ID,
    "text": "This is finally working"
};

return msg;

You’ll find the BOT_TOKEN in Slack apps settings. But the CHANNEL_ID is something that you’ll be able to obtain after reading this post ;)

After setting this up, we can send a request to the Slack API. If you didn’t do this yet, remember that you must set your bot’s permissions to let it use the API methods. Otherwise, no method will work.

For this use the http request node:

Alt Text

Finally, we add the http response node so we can have the response in the console.
The complete flow should seem like this:

Alt Text

After calling the endpoint we should receive the response in the bot chat as a formatted “This is finally working” message. To change that, you only need to modify the text at the request body, either in the app or in the call.

Conclusion

This is just a very first step in the general task, but I wanted to share it because I think that Node-RED is a good tool to make integrations with external APIs, but also it can get confusing when tuning it in at first.

I think that something useful after this example is that, most of the time, I guess I’d rather have smaller functions that make it clear to see what I am doing instead of using those in which we can’t see the code in the back but should solve the problems at once.

I'd like to thank to @lucasota and @navarroaxel for the tips and the reviews which have been helping me to improve my posts.


This content originally appeared on DEV Community and was authored by Ignacio Mattos


Print Share Comment Cite Upload Translate Updates
APA

Ignacio Mattos | Sciencx (2021-05-27T20:04:38+00:00) An alternative way to integrate with Slack using Node-RED. Retrieved from https://www.scien.cx/2021/05/27/an-alternative-way-to-integrate-with-slack-using-node-red/

MLA
" » An alternative way to integrate with Slack using Node-RED." Ignacio Mattos | Sciencx - Thursday May 27, 2021, https://www.scien.cx/2021/05/27/an-alternative-way-to-integrate-with-slack-using-node-red/
HARVARD
Ignacio Mattos | Sciencx Thursday May 27, 2021 » An alternative way to integrate with Slack using Node-RED., viewed ,<https://www.scien.cx/2021/05/27/an-alternative-way-to-integrate-with-slack-using-node-red/>
VANCOUVER
Ignacio Mattos | Sciencx - » An alternative way to integrate with Slack using Node-RED. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/27/an-alternative-way-to-integrate-with-slack-using-node-red/
CHICAGO
" » An alternative way to integrate with Slack using Node-RED." Ignacio Mattos | Sciencx - Accessed . https://www.scien.cx/2021/05/27/an-alternative-way-to-integrate-with-slack-using-node-red/
IEEE
" » An alternative way to integrate with Slack using Node-RED." Ignacio Mattos | Sciencx [Online]. Available: https://www.scien.cx/2021/05/27/an-alternative-way-to-integrate-with-slack-using-node-red/. [Accessed: ]
rf:citation
» An alternative way to integrate with Slack using Node-RED | Ignacio Mattos | Sciencx | https://www.scien.cx/2021/05/27/an-alternative-way-to-integrate-with-slack-using-node-red/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.