Authenticating Node-RED web sockets

While developing extension nodes for Node-RED is sometimes useful to re-use the built-in Web Sockets channel (/comms) to provide two ways communication between the extension and Node-RED.

Everything works fine until the moment we put everything in production and we secure the Node-RED server, the web socket connection remains pending. That's because the Web Socket connection is initiated through the http:// protocol (which requires authentication) and then upgraded to ws://.

I've found the solution after digging a little in the Node-RED code (since the lack of documentation on this topic)

import Sockette from 'sockette';
const ws = new Sockette(
  'http://localhost:1880/comms', // the Node-RED server  
  {
    // ...
    onopen: () => {
      const raw = localStorage.getItem('auth-tokens');
      let json;
      try {
        json = JSON.parse(raw)
      } catch(e) {
        // do nothing
      }
      if (json != null && !_.isEmpty(json.access_token)) {
        ws.send(`{"auth":"${json.access_token}"}`);
      }
    }
  }
);

Basically the trick is to use the same Node-RED authentication token to authenticate the web socket connection.

RedBot 0.19.4

Some recent changes in Slack API remove the bot scope from RTM API. That basically means the for all new applications it's not possible to use the RTM API and the old ones will stop working at the end of 2020.

The 0.19.4 will use the Event API which has the drawback of being not compatible with the Express stack of Node-RED, for this reason the web hook server is running in a separate Express app on a different port (that can be configured in the connection panel).

Follow the updated instruction here to upgrade to the new connection params.

Twilio connector for RedBot

Create an account on Twilio (you'll need to verify a mobile number to proceed)

In order to create a SMS service you have to buy a Twilio phone number (any country is fine).

Twilio sends notification of incoming messages with a callback, use ngrok to expose your development instance of Node-RED to a public address, open a Terminal window and

ngrok http localhost:1880

You should get something like this

`

The address https://*.nkrok.io is the public URL that

Setting up a dev chatbot for Facebok and RedBot

This is a quick tutorial to setup a very simple chatbot with Facebook Messenger and RedBot.

First of all install Node-RED

sudo npm install -g node-red

Then open the user data directory $HOME/.node-red and install the package

cd $HOME/.node-red npm install node-red-contrib-chatbot

Then run node-red.

Setting up a Facebook Messenger is quite tricky since Facebook API talks to Red-Node via a https callback (a self signed certificate is not enough). It also requires a verify token that makes

Store user response in RedBot

Working with chatbots it's sometimes useful to parse and store the user response somewhere.

In RedBot there's the chat context, it's a volatile memory space where it's possible to store variables related to the current chat user, for example consider this flow in which we ask the user to type in his email.

parse-email

The first Message node just sends out the question to the user "What is your email?" (always use a conversational tone with a chatbot and not cold

Logging conversations with RedBot

Running a chat bot requires some caring, in particular you're never done training it.

A good habit is to inspect the chat bot log searching for user sentences that were not recognized by the bot and then update the parsers Listen node, RiveScript node, etc) in order to support it.

Log filename

In RedBot every platform receiver can log the conversation to a file, it looks like

196520947 [Guido Bellomo] > Wed Oct 05 2016 12:08:45 GMT+0000

NetAtmo dashboard for Node-RED

There are few Node-RED nodes for the NetAtmo weather station, most of them return data of the sensors as time series.

I needed something simpler for my chatbot, a way to get fetch all current values from all sensors, something like this

{
  "temperature": 27.5,
  "co2": 246,
  "humidity": 60,
  "noise": 38,
  "pressure": 1009.5,
  "pressureTrend": "stable",
  "externalTemperature": 30,
  "externalHumidity": 51,
  "externalTemperatureTrend&

Setup a simple Telegram chatbot with RedBot

This is a quick tutorial to setup a very simple chatbot with Telegram and RedBot.

First of all install Node-RED

sudo npm install -g node-red

Then open the user data directory $HOME/.node-red and install the package

cd $HOME/.node-red npm install node-red-contrib-chatbot

Then run

node-red

The next step is to create a chat bot, I recommend to use Telegram since the setup it's easier ( Telegram allows polling to receive messages, Facebook requires a https callback and a valid https

RedBot, a chatbot framework for Node-RED

RedBot_logo

RedBot is a Node-RED framework to create chatbots for Telegram, Facebook and Slack. Almost no coding skills required.

Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways.

With RedBot it's possible to easily create chatbots just wiring together basic blocks like message, audio, location, etc. and interact with the Node-RED ecosystem. If you have particular need, you can do it by yourself in NodeJS, it's all open source!

RedBot is able

Middleware in NodeJS

Talking with a collegue of mine I came up with this solution about organizing the code in NodeJS+Express using middlewares.
Consider this example in which we have a simple page that performs two queries and shows the result in a template.
Since everything is asynchronous, we chain in two queries in this way

app.get('/mypage', function(req,res,next) { 
  sequelize.search(query1)
    .on('success', function(res_query1) { 
      sequelize.search(query2)
        .on('success', function(res_query2) { 
          res.render(

Tiny little JavaScript polling object

Few days I came across this situation where I had to check, at predefined interval, against a remote server for some conditions. So just for not having the setTimeout and setInterval ids hanging around my code, I wrapped everything in a small JavaScript object Poller.

It's pretty simple: just configure any number of intervals, a callback in case of success, a callback in case of failure, and the function that checks the server

var poller = new Poller();
poller.intervals([1000,