An alternative way to integrate with Slack using Node-RED
Tags: nodered, javascript, slack, chatbot
Published: 2021-05-27 20:04:38

Do you want to make an integration with *Slack*?
Do you already know [*Node-RED*](https://nodered.org/) 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](#context)
2. [Pros and cons using *Node-RED*](#p&c)
3. [Using a simpler node](#simplerNode)
4. [Conclusion](#conclusion)

<h2 id="context">Context</h2>

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.

<h2 id="p&c">Pros and cons using <em>Node-RED</em></h2>

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](https://flows.nodered.org/node/node-red-contrib-slack) or the [*slackbot listen*](https://flows.nodered.org/node/node-red-contrib-slackbot), which were the ones proposed everywhere I searched for.

<h2 id="simplerNode">Using a simpler node</h2>

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypeif5lb6gpy8w9m9yuo.png)

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:

```javascript
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](https://api.slack.com/apps). 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](https://api.slack.com/methods). 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0zu628h5v12z4avq31n.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nc6gpzz3aexxatpqnint.png)

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.

<h2 id="conclusion">Conclusion</h2>

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.
<hr/>

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