We’ll cover a simple example of sending a slack message every morning to get the day weather’s forecast: min-max temp and an emoji depicting what the sky will be like, using OpenWeatherMap free API.

todo todo

Intro

The goal is to create an automated bot that sends weather information every morning on a slack channel with 0 code (or maybe a maximum of 2 lines). To do so, we’re going to use Node-RED, an automation tool based on a visual interface to query APIs and trigger external tools. See it like a free, highly customizable and self-hosted IFTTT. Each action is a Node and is connected to another ones/ones with wires.

Install and run

Node red install doc

$ node -v # >=  8.x
$ sudo npm install -g --unsafe-perm node-red # install node red globally
$ node-red # will create a ~/.node-red folder to store config & data
Welcome to Node-RED
===================
22 Nov 21:53:17 - [info] Node-RED version: v0.19.5
22 Nov 21:53:17 - [info] Node.js  version: v8.12.0
22 Nov 21:53:23 - [info] User directory : /home/florian/.node-red
22 Nov 21:53:23 - [info] Server now running at http://127.0.0.1:1880/

Go ahead and open http://127.0.0.1:1880/ or http://your-server.com:1880/, you’ll have an empty flow open by default: todo

Discover

Drag n drop an inject, a debug, connect them together, hit Deploy, click on the blue square on the left of the inject node and open the debug console: todo

This is the very first step! This is what happened:

  • inject creates a global Javascrippt object msg with the key payload containing the actual int timestamp
  • inject sends it to the Debug
  • debug displays in the console the default configured output msg.payload

Double-clicking on the Debug node displays the configuration: todo

Build the weather bot

Connect to Open Weather Map

Create API key on Open Weather Map or use the default one for testing https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22

Look for the node http request, set the endpoint and change the return to be a JSON object: todo

Deploy and run! todo

And it took what, 5 min? Sweeet…

Format

Use the template node a little bit of moustache templating and here we are: todo todo

Send to Slack

Install the Node red slack module

npm install node-red-contrib-slack

Make sure you have a webhook configured on your slack, i.e. https://floriancourgey.slack.com/apps/manage/custom-integrations: todo

And use the slack node with the correct Webhook URL: todo

CRON it with Node RED

Think of using it on a daily basis? Node RED has a much easier system than Linux’s CRONs.

Open up the inject node and define the Regular setting. todo

Conclusion

You now have a fully working bot sending weather information every morning! Going further: display an emoji based on payload.weather[0].id (see https://openweathermap.org/weather-conditions), connect to your home IoT, your smart mirror