Intial Commit
This commit is contained in:
68
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/bot-config/bot-config.html
generated
vendored
Normal file
68
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/bot-config/bot-config.html
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType("telegrambot-config",{
|
||||
category: "config",
|
||||
defaults: {
|
||||
botname: { value: "", required: true },
|
||||
usernames: { value: "", required: false },
|
||||
chatIds: { value: "", required: false },
|
||||
pollInterval: { value: 300, required: false, validate:RED.validators.number() }
|
||||
},
|
||||
credentials: {
|
||||
token: { type: "text" }
|
||||
},
|
||||
label: function() {
|
||||
return this.botname;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="telegrambot-config">
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-botname"><i class="fa fa-telegram"></i> Bot-Name</label>
|
||||
<input type="text" id="node-config-input-botname" placeholder="(e.g. MyTelegramBot)">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-token"><i class="fa fa-key"></i> Token</label>
|
||||
<input type="text" id="node-config-input-token" placeholder="(e.g. 12345:ABCdef-FDAfds)">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-usernames"><i class="fa fa-user"></i> Users</label>
|
||||
<input type="text" id="node-config-input-usernames" placeholder="(e.g. hugo,sepp,egon)">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-chatIds"><i class="fa fa-comment"></i> Chat IDs</label>
|
||||
<input type="text" id="node-config-input-chatIds" placeholder="(e.g. -1234567,2345678,-3456789)">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-pollInterval"><i class="fa fa-clock-o"></i> Polling Interval</label>
|
||||
<input type="text" id="node-config-input-pollInterval" placeholder="(Optional poll interval in milliseconds. The default is 300.)">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="telegrambot-config">
|
||||
<p>Telegram bot configuration</p>
|
||||
|
||||
<h3>Config</h3>
|
||||
<dl class="message-properties">
|
||||
<dt>Bot-Name <span class="property-type">string</span></dt>
|
||||
<dd>Label for this configuration. Name it anything you would like for easy reference.</dd>
|
||||
|
||||
<dt>Token <span class="property-type">string</span></dt>
|
||||
<dd>Token provided by botfather for connecting to Telegram</dd>
|
||||
|
||||
<dt class="optional">Users <span class="property-type">list</span></dt>
|
||||
<dd>Optional. Usernames the bot is permitted access. Enter values in comma separated format. Leave blank to allow everyone.</dd>
|
||||
|
||||
<dt class="optional">Chat IDs <span class="property-type">list</span></dt>
|
||||
<dd>Optional. Chat IDs the bot is permitted access. Enter values in comma separated format. Leave blank to allow all.</dd>
|
||||
|
||||
<dt class="optional">Polling Interval <span class="property-type">number</span></dt>
|
||||
<dd>How frequently the telegram API should be polled in seconds. Default of 300, or 5 minutes.</dd>
|
||||
</dl>
|
||||
|
||||
<h3>Details</h3>
|
||||
<p>Every node requires a configuration attached to define how to connect to Telegram. That is this config node's primary purpose.</p>
|
||||
|
||||
<h3>References</h3>
|
||||
<p>To setup your bot, you can send a message to BotFather. Further details are available <a href="https://core.telegram.org/bots#6-botfather" target="_blank" rel="noopener noreferrer">here</a>.</p>
|
||||
</script>
|
||||
144
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/bot-config/bot-config.js
generated
vendored
Normal file
144
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/bot-config/bot-config.js
generated
vendored
Normal file
@@ -0,0 +1,144 @@
|
||||
module.exports = function(RED) {
|
||||
var telegramBot = require("node-telegram-bot-api");
|
||||
|
||||
function BotConfigNode(n) {
|
||||
RED.nodes.createNode(this, n);
|
||||
var node = this;
|
||||
|
||||
this.botname = n.botname;
|
||||
this.status = "disconnected";
|
||||
this.usernames = (n.usernames) ? n.usernames.split(",").map(function(u){ return u.trim(); }) : [];
|
||||
this.chatIds = (n.chatIds) ? n.chatIds.split(",").map(function(id){ return parseInt(id); }) : [];
|
||||
this.pollInterval = parseInt(n.pollInterval);
|
||||
this.nodes = [];
|
||||
|
||||
if (isNaN(this.pollInterval)) {
|
||||
this.pollInterval = 300;
|
||||
}
|
||||
|
||||
this.getTelegramBot = function () {
|
||||
if (!this.telegramBot) {
|
||||
if (this.credentials) {
|
||||
this.token = this.credentials.token;
|
||||
|
||||
if (this.token) {
|
||||
this.token = this.token.trim();
|
||||
|
||||
var polling = {
|
||||
autoStart: true,
|
||||
interval: this.pollInterval
|
||||
};
|
||||
|
||||
var options = {
|
||||
polling: polling
|
||||
};
|
||||
|
||||
this.telegramBot = new telegramBot(this.token, options);
|
||||
node.status = "connected";
|
||||
|
||||
this.telegramBot.on("error", function(error){
|
||||
node.warn(error.message);
|
||||
|
||||
node.abortBot(error.message, function(){
|
||||
node.warn("Bot stopped: fatal error");
|
||||
});
|
||||
});
|
||||
|
||||
this.telegramBot.on("polling_error", function(error){
|
||||
node.warn(error.message);
|
||||
|
||||
var stopPolling = false;
|
||||
var hint;
|
||||
|
||||
if (error.message == "ETELEGRAM: 401 Unauthorized") {
|
||||
hint = `Please check that your bot token is valid: ${node.token}`;
|
||||
stopPolling = true;
|
||||
} else if (error.message.startsWith("EFATAL: Error: connect ETIMEDOUT")) {
|
||||
hint = "Timeout connecting to server. Trying again.";
|
||||
} else if (error.message.startsWith("EFATAL: Error: read ECONNRESET")) {
|
||||
hint = "Network connection may be down. Trying again.";
|
||||
} else if (error.message.startsWith("EFATAL: Error: getaddrinfo ENOTFOUND")) {
|
||||
hint = "Network connection may be down. Trying again.";
|
||||
} else {
|
||||
hint = "Unknown error. Trying again.";
|
||||
}
|
||||
|
||||
if (stopPolling) {
|
||||
node.abortBot(error.message, function(){
|
||||
node.warn(`Bot stopped: ${hint}`);
|
||||
});
|
||||
} else {
|
||||
node.warn(hint);
|
||||
}
|
||||
});
|
||||
|
||||
this.telegramBot.on("webhook_error", function(error){
|
||||
node.warn(error.message);
|
||||
|
||||
node.abortBot(error.message, function() {
|
||||
node.warn("Bot stopped: webhook error");
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.telegramBot;
|
||||
};
|
||||
|
||||
this.on("close", function(done){
|
||||
node.abortBot("closing", done);
|
||||
});
|
||||
|
||||
this.abortBot = function(hint, done){
|
||||
if (node.telegramBot !== null && node.telegramBot._polling) {
|
||||
node.telegramBot.stopPolling()
|
||||
.then(function(){
|
||||
node.telegramBot = null;
|
||||
node.status = "disconnected";
|
||||
node.setNodeStatus({ fill: "red", shape: "ring", text: `bot stopped (${hint})`});
|
||||
done();
|
||||
});
|
||||
} else {
|
||||
node.status = "disconnected";
|
||||
node.setNodeStatus({ fill: "red", shape: "ring", text: `bot stopped (${hint})`});
|
||||
done();
|
||||
}
|
||||
};
|
||||
|
||||
this.isAuthorizedUser = function(user) {
|
||||
return (node.usernames.length === 0) || (node.usernames.indexOf(user) >= 0);
|
||||
};
|
||||
|
||||
this.isAuthorizedChat = function(chatId) {
|
||||
return (node.chatIds.length === 0) || (node.chatIds.indexOf(chatId) >= 0);
|
||||
};
|
||||
|
||||
this.isAuthorized = function(chatId, username) {
|
||||
var isAuthorizedUser = node.isAuthorizedUser(username);
|
||||
var isAuthroizedChat = node.isAuthorizedChat(chatId);
|
||||
|
||||
return isAuthorizedUser && isAuthroizedChat;
|
||||
};
|
||||
|
||||
this.register = function(n) {
|
||||
if (node.nodes.indexOf(n) === -1) {
|
||||
node.nodes.push(n);
|
||||
} else {
|
||||
node.warn(`Node ${n.id} registered more than once at the configuration node. Ignoring.`);
|
||||
}
|
||||
};
|
||||
|
||||
this.setNodeStatus = function(status) {
|
||||
node.nodes.forEach(function(node){
|
||||
node.status(status);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
RED.nodes.registerType("telegrambot-config", BotConfigNode, {
|
||||
credentials: {
|
||||
token: { type: "text" }
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user