Skip to content

Commit

Permalink
fix #272
Browse files Browse the repository at this point in the history
  • Loading branch information
windka committed Sep 19, 2022
1 parent 81359a4 commit 804da30
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 40 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.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)

# [14.1.0] - 2022-09-01
### Added web app data support - [#264](https://github.com/windkh/node-red-contrib-telegrambot/issues/264)

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.1.0",
"version": "14.2.0",
"description": "Telegram bot nodes for Node-RED",
"dependencies": {
"bluebird": "^3.7.2",
Expand Down
93 changes: 56 additions & 37 deletions telegrambot/99-telegrambot.js
Original file line number Diff line number Diff line change
Expand Up @@ -1331,18 +1331,19 @@ module.exports = function (RED) {
language = undefined;
}

this.config.registerCommand(node.id, command, description, language, scope, registerCommand);
node.telegramBot = this.config.getTelegramBot();
if (node.telegramBot) {
this.config.registerCommand(node.id, command, description, language, scope, registerCommand);

node.status({ fill: 'red', shape: 'ring', text: 'not connected' });
node.status({ fill: 'red', shape: 'ring', text: 'not connected' });

node.onStatusChanged = function (status) {
node.status(status);
};
node.config.addListener('status', node.onStatusChanged);
node.onStatusChanged = function (status) {
node.status(status);
};
node.config.addListener('status', node.onStatusChanged);

node.botname = this.config.botname;

node.telegramBot = this.config.getTelegramBot();
node.botname = this.config.botname;
if (node.telegramBot) {
if (node.telegramBot._polling !== null || node.telegramBot._webHook !== null) {
node.status({
fill: 'green',
Expand Down Expand Up @@ -2492,18 +2493,27 @@ module.exports = function (RED) {
node.status({ fill: 'green', shape: 'ring', text: 'connected' });

if (msg.payload) {
if (!Array.isArray(msg.payload.chatId)) {
this.processMessage(msg.payload.chatId, msg, nodeSend, nodeDone);
} else {
let chatIds = msg.payload.chatId;
let length = chatIds.length;
for (let i = 0; i < length; i++) {
let chatId = chatIds[i];

let clonedMsg = RED.util.cloneMessage(msg);
clonedMsg.payload.chatId = chatId;
this.processMessage(chatId, clonedMsg, nodeSend, nodeDone);
if (node.telegramBot) {
if (!Array.isArray(msg.payload.chatId)) {
this.processMessage(msg.payload.chatId, msg, nodeSend, nodeDone);
} else {
let chatIds = msg.payload.chatId;
let length = chatIds.length;
for (let i = 0; i < length; i++) {
let chatId = chatIds[i];

let clonedMsg = RED.util.cloneMessage(msg);
clonedMsg.payload.chatId = chatId;
this.processMessage(chatId, clonedMsg, nodeSend, nodeDone);
}
}
} else {
node.warn('bot not initialized.');
node.status({
fill: 'red',
shape: 'ring',
text: 'bot not initialized',
});
}
} else {
node.warn('msg.payload is empty');
Expand Down Expand Up @@ -2576,27 +2586,36 @@ module.exports = function (RED) {
node.status({ fill: 'green', shape: 'ring', text: 'connected' });

if (msg.payload) {
if (msg.payload.chatId) {
if (msg.payload.sentMessageId) {
let chatId = msg.payload.chatId;
let messageId = msg.payload.sentMessageId;

node.telegramBot.onReplyToMessage(chatId, messageId, function (botMsg) {
let messageDetails = getMessageDetails(botMsg);
if (messageDetails) {
msg.payload = messageDetails;
msg.originalMessage = botMsg;
nodeSend(msg);
if (nodeDone) {
nodeDone();
if (node.telegramBot) {
if (msg.payload.chatId) {
if (msg.payload.sentMessageId) {
let chatId = msg.payload.chatId;
let messageId = msg.payload.sentMessageId;

node.telegramBot.onReplyToMessage(chatId, messageId, function (botMsg) {
let messageDetails = getMessageDetails(botMsg);
if (messageDetails) {
msg.payload = messageDetails;
msg.originalMessage = botMsg;
nodeSend(msg);
if (nodeDone) {
nodeDone();
}
}
}
});
});
} else {
node.warn('msg.payload.sentMessageId is empty');
}
} else {
node.warn('msg.payload.sentMessageId is empty');
node.warn('msg.payload.chatId is empty');
}
} else {
node.warn('msg.payload.chatId is empty');
node.warn('bot not initialized.');
node.status({
fill: 'red',
shape: 'ring',
text: 'bot not initialized',
});
}
} else {
node.warn('msg.payload is empty');
Expand Down

0 comments on commit 804da30

Please sign in to comment.