Skip to content

Commit

Permalink
fixed #272
Browse files Browse the repository at this point in the history
  • Loading branch information
windka committed Sep 20, 2022
1 parent 804da30 commit a4d6aec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

# [14.3.0] - 2022-09-20
### improved deuplicate token usage detection - [#272](https://github.com/windkh/node-red-contrib-telegrambot/issues/272)

# [14.2.0] - 2022-09-19
### MAde node more robust when initialization is aborted due to duplicate token usage - [#272](https://github.com/windkh/node-red-contrib-telegrambot/issues/272)
### Made node more robust when initialization is aborted due to duplicate token usage - [#272](https://github.com/windkh/node-red-contrib-telegrambot/issues/272)

# [14.1.0] - 2022-09-01
### Added web app data support - [#264](https://github.com/windkh/node-red-contrib-telegrambot/issues/264)
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.2.0",
"version": "14.3.0",
"description": "Telegram bot nodes for Node-RED",
"dependencies": {
"bluebird": "^3.7.2",
Expand Down
57 changes: 38 additions & 19 deletions telegrambot/99-telegrambot.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,21 @@ module.exports = function (RED) {
return null;
};

this.tokenRegistered = false;

// first of all check if the token is used twice: in this case we abort
this.token = this.credentials.token;
let tokenUser = botsByToken[this.token];
if (tokenUser === undefined) {
botsByToken[this.token] = n;
let configNodeId = botsByToken[this.token];
if (configNodeId === undefined) {
botsByToken[self.token] = n.id;
this.tokenRegistered = true;
} else {
if (tokenUser != n) {
self.error('Aborting: Token of ' + n.botname + ' is already in use by ' + tokenUser.botname + ': ' + self.token);
if (configNodeId == n.id) {
this.tokenRegistered = true;
} else {
this.tokenRegistered = false;
let conflictingConfigNode = RED.nodes.getNode(configNodeId);
self.error('Aborting: Token of ' + n.botname + ' is already in use by ' + conflictingConfigNode.botname + ': ' + self.token);
return;
}
}
Expand Down Expand Up @@ -235,7 +242,7 @@ module.exports = function (RED) {
self.status = 'connected';
} else {
self.abortBot('Failed to set webhook ' + botUrl, function () {
self.warn('Bot stopped: Webhook not set.');
self.error('Bot stopped: Webhook not set.');
});
}
});
Expand Down Expand Up @@ -301,11 +308,14 @@ module.exports = function (RED) {
});
// We reset the polling status after the 80% of the timeout
setTimeout(function () {
self.setStatus({
fill: 'green',
shape: 'ring',
text: 'polling',
});
// check if abort was called in the meantime.
if (self.telegramBot) {
self.setStatus({
fill: 'green',
shape: 'ring',
text: 'polling',
});
}
}, self.pollInterval * 0.8);

if (self.verbose) {
Expand Down Expand Up @@ -338,7 +348,7 @@ module.exports = function (RED) {

if (stopPolling) {
self.abortBot(error.message, function () {
self.warn('Bot stopped: ' + hint);
self.error('Bot ' + self.botname + ' stopped: ' + hint);
});
} else {
// here we simply ignore the bug and try to reestablish polling.
Expand Down Expand Up @@ -485,9 +495,13 @@ module.exports = function (RED) {

RED.events.on('flows:started', this.onStarted);

this.on('close', function (done) {
this.on('close', function (removed, done) {
RED.events.removeListener('flows:started', this.onStarted);
delete botsByToken[self.token];
if (removed) {
if (self.tokenRegistered) {
delete botsByToken[self.token];
}
}
self.abortBot('closing', done);
});

Expand Down Expand Up @@ -1153,14 +1167,15 @@ module.exports = function (RED) {
});
}

this.on('close', function () {
this.on('close', function (removed, done) {
node.telegramBot.off('message');

if (node.onStatusChanged) {
node.config.removeListener('status', node.onStatusChanged);
}

node.status({});
done();
});
}
RED.nodes.registerType('telegram receiver', TelegramInNode);
Expand Down Expand Up @@ -1376,7 +1391,7 @@ module.exports = function (RED) {
});
}

this.on('close', function () {
this.on('close', function (removed, done) {
node.telegramBot.off('message');

if (node.onStatusChanged) {
Expand All @@ -1385,6 +1400,7 @@ module.exports = function (RED) {

node.config.unregisterCommand(node.id);
node.status({});
done();
});
}
RED.nodes.registerType('telegram command', TelegramCommandNode);
Expand Down Expand Up @@ -1765,14 +1781,15 @@ module.exports = function (RED) {
});
}

this.on('close', function () {
this.on('close', function (removed, done) {
node.telegramBot.off(this.event);

if (node.onStatusChanged) {
node.config.removeListener('status', node.onStatusChanged);
}

node.status({});
done();
});
}
RED.nodes.registerType('telegram event', TelegramEventNode);
Expand Down Expand Up @@ -2520,12 +2537,13 @@ module.exports = function (RED) {
}
});

this.on('close', function () {
this.on('close', function (removed, done) {
if (node.onStatusChanged) {
node.config.removeListener('status', node.onStatusChanged);
}

node.status({});
done();
});
}
RED.nodes.registerType('telegram sender', TelegramOutNode);
Expand Down Expand Up @@ -2622,12 +2640,13 @@ module.exports = function (RED) {
}
});

this.on('close', function () {
this.on('close', function (removed, done) {
if (node.onStatusChanged) {
node.config.removeListener('status', node.onStatusChanged);
}

node.status({});
done();
});
}
RED.nodes.registerType('telegram reply', TelegramReplyNode);
Expand Down

0 comments on commit a4d6aec

Please sign in to comment.