The Things Network - New community gateways
This flow tracks newly registered gateways to the The Things Network and sends the data to a Telegram chat.
Usage
After copying the flow to a clean tab in your Node-RED instance, you will have to configure several bits:
Zone requests. Two example requests are inicluded: a zone-based (latitude, longitude and radius) and a country request. Change them depending on your needs.
Change the file where EUIs are persisted in the
read
andsave
nodes. This will depend on your OS.The flow uses the Open Caged Data geocoding API based on OpenStreetMaps. You will need to create an account to get an access key. It is free up to 2500 requests per day. Use your key in the
geocoding url
node. You can also change the output language there using the 2-char ISO code.Create a telegram bot (using the @BotFather) and configure the token in the telegram configuration node.
Edit the
message
node and change the two chatId codes to target your chat. To get the chatId use another bot like @my_id_bot.
[{"id":"92371713.376e88","type":"comment","z":"940a15af.1fc478","name":"Community Gateway Monitoring","info":"","x":170,"y":60,"wires":[]},{"id":"6956fc3a.390e84","type":"http request","z":"940a15af.1fc478","name":"request","method":"GET","ret":"obj","url":"https://www.thethingsnetwork.org/gateway-data/location?latitude=41.3870734&longitude=2.1700660&distance=50000","tls":"","x":300,"y":120,"wires":[["8419982e.f23018"]]},{"id":"20e9288c.f04ad8","type":"inject","z":"940a15af.1fc478","name":"Espanya","topic":"espanya","payload":"","payloadType":"date","repeat":"900","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":160,"wires":[["f25c2b85.384508"]]},{"id":"f25c2b85.384508","type":"http request","z":"940a15af.1fc478","name":"request","method":"GET","ret":"obj","url":"https://www.thethingsnetwork.org/gateway-data/country/es","tls":"","x":300,"y":160,"wires":[[]]},{"id":"12d707f8.04a918","type":"yield","z":"940a15af.1fc478","x":630,"y":140,"wires":[["e672612c.d64e1"]]},{"id":"e7c3ee3f.fe499","type":"http request","z":"940a15af.1fc478","name":"request","method":"GET","ret":"obj","url":"","tls":"","x":820,"y":200,"wires":[["fd7d8565.e8b6f8"]]},{"id":"e672612c.d64e1","type":"template","z":"940a15af.1fc478","name":"geocode url","field":"url","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"https://api.opencagedata.com/geocode/v1/json?key=<access-key>&q={{ data.location.latitude }},{{ data.location.longitude }}&pretty=1&no_annotations=1&language=en","output":"str","x":650,"y":200,"wires":[["e7c3ee3f.fe499"]]},{"id":"19fab538.b911fb","type":"telegram sender","z":"940a15af.1fc478","name":"Telegram Bot","bot":"2d48dea9.66b672","x":950,"y":260,"wires":[[]]},{"id":"fd7d8565.e8b6f8","type":"template","z":"940a15af.1fc478","name":"message","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"[\n{ \n \"payload\": {\n \"chatId\": -220000000,\n \"type\": \"message\",\n \"content\": \"New gateway in {{ scope }}!!\\nOwner: {{ data.owner }}\\nAddress: {{ payload.results.0.formatted }}\\nDescription: {{ data.description }}\\nEUI: {{ data.id }}\"\n }\n},\n{\n \"payload\": {\n \"chatId\": -220000000,\n \"type\": \"location\",\n \"content\": {\n \"latitude\" : {{ data.location.latitude }},\n \"longitude\" : {{ data.location.longitude }}\n },\n \"options\": {\n \"live_period\": \"{{ data.last_seen }}\"\n }\n }\n}\n]\n","output":"json","x":640,"y":260,"wires":[["9b70908d.0060a"]]},{"id":"9b70908d.0060a","type":"yield","z":"940a15af.1fc478","x":790,"y":260,"wires":[["19fab538.b911fb"]]},{"id":"9a81561d.960cb8","type":"inject","z":"940a15af.1fc478","name":"Barcelona","topic":"barcelona","payload":"","payloadType":"date","repeat":"900","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":120,"wires":[["6956fc3a.390e84"]]},{"id":"8419982e.f23018","type":"function","z":"940a15af.1fc478","name":"compare","func":"// Set the zone scope\nmsg.scope = msg.topic.charAt(0).toUpperCase() + msg.topic.slice(1);\n\n// Current payload holds an object with data for the\n// currently registered gateways, object keys are EUIs\n\n// Build EUI filename based on the scope\nvar filename = \"euis_\" + msg.topic;\n\n// Previous EUIs are saved in a persistent flow variable\nvar previous = flow.get(filename, \"storeInFile\") || [];\n\n// Store current list of EUIs (only keys)\nflow.set(filename, Object.keys(msg.payload), \"storeInFile\");\n\n// Stop if first run\nif (0 === previous.length) return null;\n\n// We calculate the difference between\n// current and previous EUIs\nvar diff = Array();\nfor (var eui in msg.payload) {\n if (previous.indexOf(eui) < 0) {\n diff.push({\n scope: msg.scope,\n data: msg.payload[eui]\n });\n }\n}\n\n// Stop if no differences\nif (0 === diff.length) return null;\n\n// Return an array of new EUIs\nmsg.payload = diff;\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":140,"wires":[["12d707f8.04a918"]]},{"id":"398711d7.3b4f2e","type":"http request","z":"940a15af.1fc478","name":"request","method":"GET","ret":"obj","url":"https://www.thethingsnetwork.org/gateway-data/country/mx","tls":"","x":300,"y":200,"wires":[[]]},{"id":"40064f2d.aeb17","type":"inject","z":"940a15af.1fc478","name":"México","topic":"mexico","payload":"","payloadType":"date","repeat":"900","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":200,"wires":[["398711d7.3b4f2e"]]},{"id":"2d48dea9.66b672","type":"telegram bot","z":"","botname":"Telegram Bot","usernames":"","chatids":"","baseapiurl":"","pollinterval":"300"}]