Skip to content

Commit

Permalink
Added second output to control node
Browse files Browse the repository at this point in the history
  • Loading branch information
windka committed Oct 17, 2022
1 parent 39e5510 commit 159c5b8
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 5 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.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

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.7.0",
"version": "14.8.0",
"description": "Telegram bot nodes for Node-RED",
"dependencies": {
"bluebird": "^3.7.2",
Expand Down
51 changes: 47 additions & 4 deletions telegrambot/99-telegrambot.html
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,13 @@ <h3>Details</h3>
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,
Expand All @@ -740,6 +746,19 @@ <h3>Details</h3>
},
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);
}
});
</script>
Expand All @@ -755,16 +774,37 @@ <h3>Details</h3>
</div>

<hr align="middle"/>

<div class="form-row">
<label for="node-input-checkconnection"><i class="fa fa-paper-plane"></i> Check network connection.</label>
<input type="checkbox" id="node-input-checkconnection" style="display: inline-block; width: auto; vertical-align: top;">
</div>

<div class="form-row hidden" id="checkconnectionoptions" style="background: #fbfbfb">
<label style="width: auto"><i class="fa fa-cogs"></i> Check Connection Options:</label>

<div class="form-row" style="margin-left: 20px">
<div class="form-row">
<label for="node-config-input-hostname"><i class="fa fa-server"></i> Hostname</label>
<input type="text" id="node-config-input-hostname" placeholder="(Optional: default: https://api.telegram.org:80")">
</div>
<div class="form-row">
<label for="node-config-input-interval"><i class="fa clock-o"></i> Interval (s)</label>
<input type="text" id="node-config-input-interval" placeholder="10">
</div>
<div class="form-row">
<label for="node-config-input-timeout"><i class="fa clock-o"></i> Timeout (s)</label>
<input type="text" id="node-config-input-timeout" placeholder="1">
</div>
</div>
</div>
</script>

<script type="text/x-red" data-help-name="telegram control">
<p>A telegram node that control the configuration node.</p>

<h3>Inputs</h3>
<p>1. Standard Input: receives a control command.</p>

<h3>Outputs</h3>
<p>1. The result of the operation.</p>
<h3>Details</h3>
<p>The <code>msg.payload</code> must be an object that contains a complete command,
at a minimum these should contain:
Expand All @@ -773,6 +813,9 @@ <h3>Details</h3>
<li><code>delay</code> The optional delay in milliseconds between stop and start for command restart</li>
</ul>

<h3>Outputs</h3>
<p>1. The polling cycle info.</p>
<p>2. The check connection result.</p>
</script>
<!-- ------------------------------------------------------------------------------------------ -->

72 changes: 72 additions & 0 deletions telegrambot/99-telegrambot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand All @@ -2972,13 +2988,69 @@ module.exports = function (RED) {
telegramBot.off('getUpdates_end');
}

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

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

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' });
Expand Down

0 comments on commit 159c5b8

Please sign in to comment.