diff --git a/CHANGELOG.md b/CHANGELOG.md index f1ebf88..67b0c87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog All notable changes to this project will be documented in this file. +# [14.8.0] - 2022-10-17 +### control node has second output now + # [14.7.0] - 2022-10-16 ### control node sends a msg on every poll cycle diff --git a/package.json b/package.json index 1d41cd2..bed3b11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-telegrambot", - "version": "14.7.0", + "version": "14.8.0", "description": "Telegram bot nodes for Node-RED", "dependencies": { "bluebird": "^3.7.2", diff --git a/telegrambot/99-telegrambot.html b/telegrambot/99-telegrambot.html index dcc527a..ecb82b5 100644 --- a/telegrambot/99-telegrambot.html +++ b/telegrambot/99-telegrambot.html @@ -725,7 +725,13 @@

Details

defaults: { name: { value: "" }, bot: { value: "", type: "telegram bot", required: true }, - outputs: {value: 1} + outputs: {value: 1}, + + // only for checkconnection + checkconnection: { value: false, required: false }, + hostname: { value: "", required: false }, + interval: { value: 10, required: false, validate:function(v) { return ((v === "") || (RED.validators.number(v) && (v >= 0) && (v <= 2147483))) }}, + timeout: { value: 1, required: false, validate:function(v) { return ((v === "") || (RED.validators.number(v) && (v >= 0) && (v <= 2147483))) }}, }, inputs: 1, outputs: 1, @@ -740,6 +746,19 @@

Details

}, oneditprepare: function() { var that = this; + // checkconnection on / off + var checkconnection = function() { + var mode = $("#node-input-checkconnection").prop('checked'); + if (mode === true) { + $("#checkconnectionoptions").show(); + that.outputs = 2; + } else { + $("#checkconnectionoptions").hide(); + that.outputs = 1; + } + }; + checkconnection(); + $("#node-input-checkconnection").change(checkconnection); } }); @@ -755,6 +774,30 @@

Details


+ +
+ + +
+ + diff --git a/telegrambot/99-telegrambot.js b/telegrambot/99-telegrambot.js index 1f38d30..c4bd8f8 100644 --- a/telegrambot/99-telegrambot.js +++ b/telegrambot/99-telegrambot.js @@ -10,6 +10,7 @@ module.exports = function (RED) { const pump = require('pump'); const fs = require('fs'); + let net = require('net'); let Promise = require('bluebird'); Promise.config({ cancellation: true, @@ -2921,6 +2922,14 @@ module.exports = function (RED) { let node = this; this.bot = config.bot; + let checkconnection = config.checkconnection; + if (checkconnection === undefined) { + checkconnection = false; + } + let hostname = config.hostname; + let interval = (config.interval || 10) * 1000; + let connectionTimeout = (config.timeout || 10) * 1000; + this.start = function () { let telegramBot = node.config.getTelegramBot(); if (telegramBot) { @@ -2950,6 +2959,13 @@ module.exports = function (RED) { node.send(msg); }); + // start supervisor + if (checkconnection) { + node.checkConnectionTimer = setInterval(function () { + node.checkConnection(); + }, interval); + } + node.status({ fill: 'green', shape: 'ring', @@ -2972,6 +2988,11 @@ module.exports = function (RED) { telegramBot.off('getUpdates_end'); } + if (node.checkConnectionTimer) { + clearTimeout(node.checkConnectionTimer); + node.checkConnectionTimer = null; + } + node.status({ fill: 'red', shape: 'ring', @@ -2979,6 +3000,57 @@ module.exports = function (RED) { }); }; + this.checkConnection = function () { + let telegramBot = node.config.getTelegramBot(); + if (telegramBot) { + let effectiveUrl = telegramBot.options.baseApiUrl; + if (hostname !== '') { + effectiveUrl = hostname; + } + let url = new URL(effectiveUrl); + let host = url.hostname; + let port = url.port || 80; + let timeout = connectionTimeout; + node.isHostReachable(host, port, timeout).then( + function () { + let msg = { + payload: { + isOnline: true, + }, + }; + node.send(msg); + }, + function (err) { + let msg = { + payload: { + isOnline: false, + error: err, + }, + }; + node.send(msg); + } + ); + } + }; + + this.isHostReachable = function (host, port, timeout) { + return new Promise(function (resolve, reject) { + let timer = setTimeout(function () { + reject('timeout'); + socket.end(); + }, timeout); + let socket = net.createConnection(port, host, function () { + clearTimeout(timer); + resolve(); + socket.end(); + }); + socket.on('error', function (err) { + clearTimeout(timer); + reject(err); + }); + }); + }; + this.config = RED.nodes.getNode(this.bot); if (this.config) { node.status({ fill: 'red', shape: 'ring', text: 'not connected' });