Skip to content

Commit

Permalink
fixed #253 again
Browse files Browse the repository at this point in the history
  • Loading branch information
windka committed Oct 21, 2022
1 parent bfd07fc commit 3bb055f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

# [14.8.6] - 2022-10-19
### reworked offline detection in control node - [#253](https://github.com/windkh/node-red-contrib-telegrambot/issues/253)

# [14.8.5] - 2022-10-19
### added getfile - [#252](https://github.com/windkh/node-red-contrib-telegrambot/issues/252)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-telegrambot",
"version": "14.8.5",
"version": "14.8.6",
"description": "Telegram bot nodes for Node-RED",
"dependencies": {
"bluebird": "^3.7.2",
Expand Down
80 changes: 39 additions & 41 deletions telegrambot/99-telegrambot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2973,13 +2973,6 @@ module.exports = function (RED) {
node.send(msg);
});

// start supervisor
if (checkconnection) {
node.checkConnectionTimer = setInterval(function () {
node.checkConnection();
}, interval);
}

node.status({
fill: 'green',
shape: 'ring',
Expand All @@ -3002,11 +2995,6 @@ module.exports = function (RED) {
telegramBot.off('getUpdates_end');
}

if (node.checkConnectionTimer) {
clearTimeout(node.checkConnectionTimer);
node.checkConnectionTimer = null;
}

node.status({
fill: 'red',
shape: 'ring',
Expand All @@ -3015,36 +3003,33 @@ 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);
}
);
let effectiveUrl = node.config.baseApiUrl || 'https://api.telegram.org';
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([null, msg]);
},
function (err) {
let msg = {
payload: {
isOnline: false,
error: err,
},
};
node.send([null, msg]);
}
);
};

this.isHostReachable = function (host, port, timeout) {
Expand Down Expand Up @@ -3084,6 +3069,13 @@ module.exports = function (RED) {
node.config.addListener('status', node.onStatusChanged);

node.start();

// start supervisor
if (checkconnection) {
node.checkConnectionTimer = setInterval(function () {
node.checkConnection();
}, interval);
}
} else {
node.warn('config node failed to initialize.');
node.status({
Expand Down Expand Up @@ -3136,6 +3128,12 @@ module.exports = function (RED) {
});

this.on('close', function (removed, done) {
// Stop supervisor
if (node.checkConnectionTimer) {
clearTimeout(node.checkConnectionTimer);
node.checkConnectionTimer = null;
}

node.stop();

if (node.onStatusChanged) {
Expand Down

0 comments on commit 3bb055f

Please sign in to comment.