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" }
|
||||
}
|
||||
});
|
||||
};
|
||||
77
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/command/command.html
generated
vendored
Normal file
77
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/command/command.html
generated
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType("telegrambot-command", {
|
||||
category: "telegram",
|
||||
paletteLabel: "command",
|
||||
color: "#3babdd",
|
||||
icon: "telegram.png",
|
||||
align: "left",
|
||||
defaults: {
|
||||
name: { value: "" },
|
||||
bot: { value: "", type: "telegrambot-config", required: true },
|
||||
command: { value: "", required: true },
|
||||
commandType: { value: "str" },
|
||||
commandCase: { value: false }
|
||||
},
|
||||
inputs: 0,
|
||||
outputs: 1,
|
||||
label: function() {
|
||||
return this.name || "telegram command";
|
||||
},
|
||||
labelStyle: function() {
|
||||
return this.name ? "node_label_italic" : "";
|
||||
},
|
||||
oneditprepare: function() {
|
||||
$('#node-input-command').typedInput({
|
||||
default: this.commandType || "str",
|
||||
typeField: $("#node-input-commandType"),
|
||||
types: ["str", "re"]
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="telegrambot-command">
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-bot">Bot</label>
|
||||
<input type="text" id="node-input-bot" placeholder="Bot">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-command">Command</label>
|
||||
<input type="text" id="node-input-command" placeholder="(e.g. /help or /^Help$/i)" style="width: 70%">
|
||||
<input type="hidden" id="node-input-commandType">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-command">Case Sensitive</label>
|
||||
<input type="checkbox" id="node-input-commandCase" style="width:auto; vertical-align:top;">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="telegrambot-command">
|
||||
<p>Outputs message payload once a specific command is said in chat.</p>
|
||||
|
||||
<h3>Details</h3>
|
||||
<p>When a certain command or phrase is said in the specified chat, a message is output containing the Telegram API's message payload.</p>
|
||||
|
||||
<dl class="message-properties">
|
||||
<dt>Bot <span class="property-type">string</span></dt>
|
||||
<dd>Telegram bot configuration for sending the message</dd>
|
||||
|
||||
<dt>Chat ID <span class="property-type">int</span></dt>
|
||||
<dd>Telegram chat ID to send the message</dd>
|
||||
|
||||
<dt>Command <span class="property-type">string</span></dt>
|
||||
<dd>The specific command or phrase that must be said in chat to initiate the flow</dd>
|
||||
|
||||
<dt>Case Sensitive <span class="property-type">bool</span></dt>
|
||||
<dd>When enabled, matching command and message will be done without comparing case.</dd>
|
||||
</dl>
|
||||
|
||||
<h3>Outputs</h3>
|
||||
<p>The message sent by the user is passed as <code>msg.payload</code>.</p>
|
||||
|
||||
<p>The original Telegram message object is passed as <code>msg.telegram</code>.</p>
|
||||
</script>
|
||||
70
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/command/command.js
generated
vendored
Normal file
70
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/command/command.js
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
var utils = require('../../lib/utils.js');
|
||||
|
||||
module.exports = function(RED) {
|
||||
function CommandNode(config) {
|
||||
RED.nodes.createNode(this, config);
|
||||
var node = this;
|
||||
|
||||
// Get base configuration
|
||||
this.bot = RED.nodes.getNode(config.bot);
|
||||
this.command = { type: config.commandType || "str", value: config.command, case: config.commandCase };
|
||||
|
||||
// Initialize bot
|
||||
utils.initializeBot(node);
|
||||
|
||||
// Verify inputs
|
||||
if (isEmpty(this.command.value)) {
|
||||
utils.updateNodeStatusFailed(node, "command is not provided");
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.command.type === "re") {
|
||||
try {
|
||||
this.command.value = new RegExp(this.command.value, this.command.case ? "" : "i");
|
||||
} catch(ex) {
|
||||
utils.updateNodeStatusFailed(node, "command is not valid regexp");
|
||||
node.warn(ex.message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (node.telegramBot) {
|
||||
node.telegramBot.on('message', function(botMsg){
|
||||
var msg = { payload: botMsg.text, telegram: botMsg };
|
||||
var chatId = botMsg.chat.id;
|
||||
var username = botMsg.from.username;
|
||||
|
||||
if (node.bot.isAuthorized(chatId, username)) {
|
||||
if (matchedCommand(node.command, botMsg.text)) {
|
||||
node.send(msg);
|
||||
}
|
||||
} else {
|
||||
node.warn(`received unauthorized message in ${chatId} from '${username}'`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.on("close", function(){
|
||||
node.telegramBot.off("message");
|
||||
node.status({});
|
||||
});
|
||||
}
|
||||
|
||||
function matchedCommand(command, message) {
|
||||
if (command.type === "str" && !command.case) {
|
||||
return command.value.localeCompare(message, undefined, { sensitivity: "base" }) === 0;
|
||||
} else if (command.type === "str" && command.case) {
|
||||
return command.value === message;
|
||||
} else if (command.type === "re") {
|
||||
return command.value.test(message);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function isEmpty(str) {
|
||||
return (!str || /^\s*$/.test(str));
|
||||
}
|
||||
|
||||
RED.nodes.registerType("telegrambot-command", CommandNode);
|
||||
};
|
||||
BIN
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/command/icons/telegram.png
generated
vendored
Normal file
BIN
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/command/icons/telegram.png
generated
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 984 B |
BIN
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/notify/icons/telegram.png
generated
vendored
Normal file
BIN
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/notify/icons/telegram.png
generated
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 984 B |
75
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/notify/notify.html
generated
vendored
Normal file
75
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/notify/notify.html
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType("telegrambot-notify", {
|
||||
category: "telegram",
|
||||
paletteLabel: "notify",
|
||||
color: "#3babdd",
|
||||
icon: "telegram.png",
|
||||
align: "right",
|
||||
defaults: {
|
||||
name: { value: "" },
|
||||
bot: { value: "", type: "telegrambot-config", required: true },
|
||||
chatId: { value: "", required: false },
|
||||
message: { value: "", required: false },
|
||||
parseMode: { value: "", required: false }
|
||||
},
|
||||
inputs: 1,
|
||||
outputs: 0,
|
||||
label: function() {
|
||||
return this.name || "telegram notify";
|
||||
},
|
||||
labelStyle: function() {
|
||||
return this.name ? "node_label_italic" : "";
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="telegrambot-notify">
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-bot">Bot</label>
|
||||
<input type="text" id="node-input-bot" placeholder="Bot">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-chatId">Chat ID</label>
|
||||
<input type="text" id="node-input-chatId" placeholder="(e.g. 1234)">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-parseMode">Parse Mode</label>
|
||||
<select id="node-input-parseMode">
|
||||
<option value="">Plain Text</option>
|
||||
<option value="Markdown">Markdown</option>
|
||||
<option value="HTML">HTML</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-message">Message</label>
|
||||
<input type="text" id="node-input-message" placeholder="(e.g. Your lights turned themselves on. Freaky.)">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="telegrambot-notify">
|
||||
<p>Send a notification to a Telegram chat.</p>
|
||||
|
||||
<h3>Details</h3>
|
||||
<p>When a message arrives, a message will be sent to the specified Telegram bot + chat ID. You can either enter a static message, or pass one in via the input.</p>
|
||||
|
||||
<dl class="message-properties">
|
||||
<dt>Name <span class="property-type">string</span></dt>
|
||||
<dd>Label for this node for easy reference</dd>
|
||||
|
||||
<dt>Bot <span class="property-type">string</span></dt>
|
||||
<dd>Telegram bot configuration for sending the message</dd>
|
||||
|
||||
<dt class="optional">Chat ID <span class="property-type">int</span></dt>
|
||||
<dd>Optional. Telegram chat ID to send the message. Specify a value to have this node send to the same chat every time. Leave blank to allow passing in the chat ID via <code>msg.telegram.chat.id</code>.</dd>
|
||||
|
||||
<dt>Parse Mode <span class="property-type">enum</span></dt>
|
||||
<dd>How the Telegram client should parse the message. Can be plain text, <a href="https://core.telegram.org/bots/api/#markdown-style" target="_blank" rel="noopener noreferrer">Markdown</a>, or <a href="https://core.telegram.org/bots/api/#html-style" target="_blank" rel="noopener noreferrer">HTML</a>.</dd>
|
||||
|
||||
<dt class="optional">Message <span class="property-type">string</span></dt>
|
||||
<dd>Optional. If provided, this message will be sent when the node triggers. If not provided, the contents of <code>msg.payload</code> will be sent.</dd>
|
||||
</dl>
|
||||
</script>
|
||||
63
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/notify/notify.js
generated
vendored
Normal file
63
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/notify/notify.js
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
var utils = require('../../lib/utils.js');
|
||||
|
||||
module.exports = function(RED) {
|
||||
function NotifyNode(config) {
|
||||
RED.nodes.createNode(this, config);
|
||||
var node = this;
|
||||
|
||||
// Get base configuration
|
||||
this.bot = RED.nodes.getNode(config.bot);
|
||||
this.chatId = parseInt(config.chatId);
|
||||
this.parseMode = config.parseMode;
|
||||
this.staticMessage = config.message;
|
||||
|
||||
// Initialize bot
|
||||
utils.initializeBot(node);
|
||||
|
||||
// Verify inputs
|
||||
if (isNaN(this.chatId)) {
|
||||
this.chatId = null;
|
||||
}
|
||||
|
||||
this.on("input", function(msg){
|
||||
if (!(node.staticMessage || msg.payload)) {
|
||||
utils.updateNodeStatusFailed(node, "message payload is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!utils.validateChatId(node, msg)) {
|
||||
utils.updateNodeStatusFailed(node, "message has no chatID");
|
||||
return;
|
||||
}
|
||||
|
||||
var chatId = node.chatId || msg.telegram.chat.id;
|
||||
var message = node.staticMessage || msg.payload;
|
||||
var chunkSize = 4000;
|
||||
var done = false;
|
||||
var messageToSend;
|
||||
var options = { parse_mode: node.parseMode };
|
||||
|
||||
do {
|
||||
if (message.length > chunkSize) {
|
||||
messageToSend = message.substr(0, chunkSize);
|
||||
message = message.substr(chunkSize);
|
||||
} else {
|
||||
messageToSend = message;
|
||||
done = true;
|
||||
}
|
||||
|
||||
node.telegramBot.sendMessage(chatId, messageToSend, options).then(function(sent){
|
||||
msg.telegram = { sentMessageId: sent.message_id };
|
||||
node.send(msg);
|
||||
});
|
||||
} while (!done);
|
||||
});
|
||||
|
||||
this.on("close", function(){
|
||||
node.telegramBot.off("message");
|
||||
node.status({});
|
||||
});
|
||||
}
|
||||
|
||||
RED.nodes.registerType("telegrambot-notify", NotifyNode);
|
||||
};
|
||||
BIN
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/payload/icons/telegram.png
generated
vendored
Normal file
BIN
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/payload/icons/telegram.png
generated
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 984 B |
151
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/payload/payload.html
generated
vendored
Normal file
151
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/payload/payload.html
generated
vendored
Normal file
@@ -0,0 +1,151 @@
|
||||
<script type="text/javascript">
|
||||
// Configuration inspired by node-red's template node
|
||||
// https://github.com/node-red/node-red/blob/master/nodes/core/core/80-template.html
|
||||
|
||||
RED.nodes.registerType("telegrambot-payload", {
|
||||
category: "telegram",
|
||||
paletteLabel: "payload",
|
||||
color: "#3babdd",
|
||||
icon: "telegram.png",
|
||||
align: "right",
|
||||
defaults: {
|
||||
name: { value: "" },
|
||||
bot: { value: "", type: "telegrambot-config", required: true },
|
||||
chatId: { value: "", required: false },
|
||||
sendMethod: { value: "sendMessage", required: false },
|
||||
payload: { value: "", required: false }
|
||||
},
|
||||
inputs: 1,
|
||||
outputs: 1,
|
||||
label: function() {
|
||||
return this.name || "telegram payload";
|
||||
},
|
||||
labelStyle: function() {
|
||||
return this.name ? "node_label_italic" : "";
|
||||
},
|
||||
oneditprepare: function() {
|
||||
this.editor = RED.editor.createEditor({
|
||||
id: 'node-input-payload-editor',
|
||||
mode: 'ace/mode/json',
|
||||
value: $("#node-input-payload").val()
|
||||
});
|
||||
|
||||
RED.library.create({
|
||||
url: "functions",
|
||||
type: "function",
|
||||
editor: that.editor,
|
||||
fields: ['name','outputs']
|
||||
});
|
||||
|
||||
this.editor.focus();
|
||||
},
|
||||
oneditsave: function() {
|
||||
$("#node-input-payload").val(this.editor.getValue());
|
||||
this.editor.destroy();
|
||||
delete this.editor;
|
||||
},
|
||||
oneditcancel: function() {
|
||||
this.editor.destroy();
|
||||
delete this.editor;
|
||||
},
|
||||
oneditresize: function(size) {
|
||||
var rows = $("#dialog-form>div:not(.node-text-editor-row)");
|
||||
var height = $("#dialog-form").height();
|
||||
for (var i=0; i<rows.size(); i++) {
|
||||
height -= $(rows[i]).outerHeight(true);
|
||||
}
|
||||
var editorRow = $("#dialog-form>div.node-text-editor-row");
|
||||
height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
|
||||
$(".node-text-editor").css("height",height+"px");
|
||||
this.editor.resize();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="telegrambot-payload">
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-bot">Bot</label>
|
||||
<input type="text" id="node-input-bot" placeholder="Bot">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-chatId">Chat ID</label>
|
||||
<input type="text" id="node-input-chatId" placeholder="(e.g. 1234)">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-sendMethod">Method</label>
|
||||
<select id="node-input-sendMethod">
|
||||
<option value="sendMessage">sendMessage</option>
|
||||
<option value="sendPhoto">sendPhoto</option>
|
||||
<option value="sendAudio">sendAudio</option>
|
||||
<option value="sendDocument">sendDocument</option>
|
||||
<option value="sendSticker">sendSticker</option>
|
||||
<option value="sendVideo">sendVideo</option>
|
||||
<option value="sendVoice">sendVoice</option>
|
||||
<option value="sendVideoNote">sendVideoNote</option>
|
||||
<option value="sendMediaGroup">sendMediaGroup</option>
|
||||
<option value="sendLocation">sendLocation</option>
|
||||
<option value="sendVenue">sendVenue</option>
|
||||
<option value="sendContact">sendContact</option>
|
||||
<option value="sendChatAction">sendChatAction</option>
|
||||
<option value="">- set by msg.method -</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row node-text-editor-row">
|
||||
<input type="hidden" id="node-input-payload" autofocus="autofocus">
|
||||
<div style="height: 250px; min-height:150px;" class="node-text-editor" id="node-input-payload-editor" ></div>
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="telegrambot-payload">
|
||||
<p>Send a generic JSON payload via the specified method to Telegram.</p>
|
||||
|
||||
<h3>Details</h3>
|
||||
<p>When a message arrives, a payload will be sent to the specified Telegram bot + chat ID using the configured method.
|
||||
You can either enter a static payload, or pass one in via the input.</p>
|
||||
|
||||
<p>You should use this when you need more control than just a generic message. Provide a static
|
||||
payload in the configuration, or use a function node linked into this one for a more dynamic configuration.</p>
|
||||
|
||||
<dl class="message-properties">
|
||||
<dt>Name <span class="property-type">string</span></dt>
|
||||
<dd>Label for this node for easy reference</dd>
|
||||
|
||||
<dt>Bot <span class="property-type">string</span></dt>
|
||||
<dd>Telegram bot configuration for sending the message</dd>
|
||||
|
||||
<dt class="optional">Chat ID <span class="property-type">int</span></dt>
|
||||
<dd>Optional. Telegram chat ID to send the message. Specify a value to have this node send to the same chat every time. Leave blank to allow passing in the chat ID via <code>msg.telegram.chat.id</code>.</dd>
|
||||
|
||||
<dt>Method <span class="property-type">string</span></dt>
|
||||
<dd>Telegram API method used for sending the payload. If not configured in the node, <code>msg.method</code> will be used.</dd>
|
||||
|
||||
<dt class="optional">Payload <span class="property-type">json</span></dt>
|
||||
<dd>Optional. If provided, this payload will be sent when the node triggers. If not provided, the contents of <code>msg.payload</code> will be sent. See below for more.</dd>
|
||||
</dl>
|
||||
|
||||
<h3>Payload</h3>
|
||||
<p>The payload will be passed directly to the Telegram API. Refer to <a href="https://core.telegram.org/bots/api#available-methods" target="_blank" rel="noopener noreferrer">the API documentation</a> for specific information.</p>
|
||||
|
||||
<p>You are responsible for building and validating the appropriate payload prior to sending it to this node.</p>
|
||||
|
||||
<p>Any example payload for the <code>sendMessage</code> method would be:</p>
|
||||
|
||||
<p><pre>{
|
||||
"text": "Hello, *world*!",
|
||||
"parse_mode": "Markdown"
|
||||
}</pre></p>
|
||||
|
||||
<h4>Sending Files</h4>
|
||||
<p>Some of the send methods support sending rich media files such as photos, videos, etc. The easiest way to do this is to provide a URL to the media.</p>
|
||||
|
||||
<p>For more advanced/dynamic configurations, refer to the <a href="https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files" target="_blank" rel="noopener noreferrer">underlying API documentation</a> on how to send files.</p>
|
||||
|
||||
<h3>Outputs</h3>
|
||||
<p>The <code>msg.payload</code> will contain the response from Telegram for the corresponding <code>sendMethod</code>.</p>
|
||||
|
||||
<p>Refer to <a href="https://core.telegram.org/bots/api#available-methods" target="_blank" rel="noopener noreferrer">the API documentation</a> for specific properties in the message.</p>
|
||||
</script>
|
||||
130
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/payload/payload.js
generated
vendored
Normal file
130
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/payload/payload.js
generated
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
var utils = require('../../lib/utils.js');
|
||||
|
||||
module.exports = function(RED) {
|
||||
function buildArgs(chatId, payload, ...keys) {
|
||||
var values = [];
|
||||
var n = keys.length;
|
||||
|
||||
for (var i = 0; i < n; i++) {
|
||||
values.push(payload[keys[i]]);
|
||||
delete payload[keys[i]];
|
||||
}
|
||||
|
||||
return [chatId, ...values, payload];
|
||||
}
|
||||
|
||||
function PayloadNode(config) {
|
||||
RED.nodes.createNode(this, config);
|
||||
var node = this;
|
||||
|
||||
// Get base configuration
|
||||
this.bot = RED.nodes.getNode(config.bot);
|
||||
this.chatId = parseInt(config.chatId);
|
||||
this.sendMethod = config.sendMethod;
|
||||
this.staticPayload = config.payload;
|
||||
|
||||
// Initialize bot
|
||||
utils.initializeBot(node);
|
||||
|
||||
// Verify inputs
|
||||
if (isNaN(this.chatId)) {
|
||||
this.chatId = null;
|
||||
}
|
||||
|
||||
if (this.staticPayload && typeof this.staticPayload !== "object") {
|
||||
try {
|
||||
this.staticPayload = JSON.parse(this.staticPayload);
|
||||
} catch(ex) {
|
||||
utils.updateNodeStatusFailed(node, "staticPayload is not valid JSON");
|
||||
node.warn(ex.message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.on("input", function(msg){
|
||||
if (!(node.staticPayload || msg.payload)) {
|
||||
utils.updateNodeStatusFailed(node, "message payload is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!utils.validateChatId(node, msg)) {
|
||||
utils.updateNodeStatusFailed(node, "message has no chatID");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!node.sendMethod && !msg.method) {
|
||||
utils.updateNodeStatusFailed(node, "sendMethod is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.method && typeof node.telegramBot[msg.method] !== "function") {
|
||||
utils.updateNodeStatusFailed(node, "sendMethod is not a valid method");
|
||||
return;
|
||||
}
|
||||
|
||||
var chatId = node.chatId || msg.telegram.chat.id;
|
||||
var sendMethod = node.sendMethod || msg.method;
|
||||
var payload = node.staticPayload;
|
||||
var args = [];
|
||||
|
||||
if (!payload && msg.payload) {
|
||||
if (typeof msg.payload === "string") {
|
||||
try {
|
||||
payload = JSON.parse(msg.payload);
|
||||
} catch(ex) {
|
||||
utils.updateNodeStatusFailed(node, "payload is malformed");
|
||||
node.warn(ex.message);
|
||||
}
|
||||
} else if (typeof msg.payload === "object") {
|
||||
payload = msg.payload;
|
||||
} else {
|
||||
utils.updateNodeStatusFailed(node, "payload is malformed");
|
||||
node.warn(`expected payload to be string or object, got ${typeof msg.payload}`);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
payload = JSON.parse(JSON.stringify(payload));
|
||||
} catch (ex) {
|
||||
utils.updateNodeStatusFailed(node, "payload is malformed");
|
||||
node.warn(ex.message);
|
||||
}
|
||||
}
|
||||
|
||||
switch(sendMethod) {
|
||||
case "sendMessage": args = buildArgs(chatId, payload, "text"); break;
|
||||
case "sendPhoto": args = buildArgs(chatId, payload, "photo"); break;
|
||||
case "sendAudio": args = buildArgs(chatId, payload, "audio"); break;
|
||||
case "sendDocument": args = buildArgs(chatId, payload, "document"); break;
|
||||
case "sendSticker": args = buildArgs(chatId, payload, "sticker"); break;
|
||||
case "sendVideo": args = buildArgs(chatId, payload, "video"); break;
|
||||
case "sendVoice": args = buildArgs(chatId, payload, "voice"); break;
|
||||
case "sendVideoNote": args = buildArgs(chatId, payload, "video_note"); break;
|
||||
case "sendMediaGroup": args = buildArgs(chatId, payload, "media"); break;
|
||||
case "sendLocation": args = buildArgs(chatId, payload, "latitude", "longitude"); break;
|
||||
case "sendVenue": args = buildArgs(chatId, payload, "latitude", "longitude", "title", "address"); break;
|
||||
case "sendContact": args = buildArgs(chatId, payload, "phone_number", "first_name"); break;
|
||||
case "sendChatAction": args = buildArgs(chatId, payload, "chat_action"); break;
|
||||
|
||||
case "answerCallbackQuery": args = buildArgs(chatId, payload, "callback_query_id"); break;
|
||||
case "editMessageReplyMarkup": args = buildArgs(chatId, payload, "reply_markup"); break;
|
||||
}
|
||||
|
||||
if (args.length > 0) {
|
||||
node.telegramBot[sendMethod](...args).then(function(response){
|
||||
msg.payload = response;
|
||||
node.send(msg);
|
||||
});
|
||||
} else {
|
||||
node.warn("empty arguments after parsing payload");
|
||||
}
|
||||
});
|
||||
|
||||
this.on("close", function(){
|
||||
node.telegramBot.off("message");
|
||||
node.status({});
|
||||
});
|
||||
}
|
||||
|
||||
RED.nodes.registerType("telegrambot-payload", PayloadNode);
|
||||
};
|
||||
239
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/switch/switch.html
generated
vendored
Normal file
239
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/switch/switch.html
generated
vendored
Normal file
@@ -0,0 +1,239 @@
|
||||
<script type="text/javascript">
|
||||
// Configuration inspired by node-red's switch node
|
||||
// https://github.com/node-red/node-red/blob/master/nodes/core/logic/10-switch.html
|
||||
var timeoutIdx = 99999;
|
||||
|
||||
RED.nodes.registerType("telegrambot-switch", {
|
||||
category: "telegram",
|
||||
paletteLabel: "switch",
|
||||
color: "#3babdd",
|
||||
icon: "switch.png",
|
||||
defaults: {
|
||||
name: { value: "" },
|
||||
bot: { value: "", type: "telegrambot-config", required: true },
|
||||
chatId: { value: "", required: false },
|
||||
question: { value: "" },
|
||||
answers: { value: [] },
|
||||
outputs: { value: 1 },
|
||||
autoAnswerCallback: { value: true, required: false },
|
||||
timeoutValue: { value: null },
|
||||
timeoutUnits: { value: null }
|
||||
},
|
||||
inputs: 1,
|
||||
outputs: 1,
|
||||
label: function() {
|
||||
return this.name || "telegram switch";
|
||||
},
|
||||
labelStyle: function() {
|
||||
return this.name ? "node_label_italic" : "";
|
||||
},
|
||||
outputLabels: function(index) {
|
||||
if (index < this.answers.length) {
|
||||
var answer = this.answers[index];
|
||||
return (answer.length > 15) ? `${answer.substring(0,15)}...` : answer;
|
||||
} else {
|
||||
return "timeout";
|
||||
}
|
||||
},
|
||||
oneditprepare: function() {
|
||||
var node = this;
|
||||
var outputCount = $("#node-input-outputs").val("{}");
|
||||
|
||||
$("#node-input-timeoutUnits").change(function(){
|
||||
if ($(this).val() == "") {
|
||||
$("#node-input-timeoutValue").val("");
|
||||
$("#node-input-timeoutValue").prop("disabled", true);
|
||||
|
||||
var currentOutputs = JSON.parse(outputCount.val() || "{}");
|
||||
delete currentOutputs[timeoutIdx];
|
||||
outputCount.val(JSON.stringify(currentOutputs));
|
||||
} else {
|
||||
$("#node-input-timeoutValue").prop("disabled", false);
|
||||
|
||||
var currentOutputs = JSON.parse(outputCount.val() || "{}");
|
||||
currentOutputs[timeoutIdx] = timeoutIdx;
|
||||
outputCount.val(JSON.stringify(currentOutputs));
|
||||
}
|
||||
});
|
||||
|
||||
$("#node-input-answer-container").css("min-height","250px").css("min-width","450px").editableList({
|
||||
addItem: function(container, i, opt) {
|
||||
container.css({
|
||||
overflow: "hidden",
|
||||
whiteSpace: "nowrap"
|
||||
});
|
||||
|
||||
if (!opt.hasOwnProperty("answer")) {
|
||||
opt.answer = "";
|
||||
}
|
||||
|
||||
if (!opt.hasOwnProperty("idx")) {
|
||||
opt._idx = Math.floor((0x99999-0x10000)*Math.random()).toString();
|
||||
}
|
||||
|
||||
var answer = opt.answer;
|
||||
var row = $("<div/>").appendTo(container);
|
||||
var valueField = $("<input/>",{class:"node-input-answer-value",type:"text",style:"margin-left: 5px;"}).appendTo(row).typedInput({default:"str",types:["str"]});
|
||||
var finalspan = $("<span/>",{style:"float: right;margin-top: 6px;"}).appendTo(row);
|
||||
finalspan.append(` → <span class="node-input-answer-index">${(i+1)}</span> `);
|
||||
|
||||
if (typeof answer != "undefined") {
|
||||
valueField.typedInput("value", answer)
|
||||
}
|
||||
|
||||
var currentOutputs = JSON.parse(outputCount.val() || "{}");
|
||||
currentOutputs[opt.hasOwnProperty("idx") ? opt.idx : opt._idx] = i;
|
||||
outputCount.val(JSON.stringify(currentOutputs));
|
||||
},
|
||||
removeItem: function(opt){
|
||||
var currentOutputs = JSON.parse(outputCount.val() || "{}");
|
||||
|
||||
if (opt.hasOwnProperty("idx")) {
|
||||
currentOutputs[opt.idx] = -1;
|
||||
} else {
|
||||
delete currentOutputs[opt._idx];
|
||||
}
|
||||
|
||||
var answers = $("#node-input-answer-container").editableList("items");
|
||||
answers.each(function(i) {
|
||||
$(this).find(".node-input-answer-index").html(i + 1);
|
||||
var data = $(this).data("data");
|
||||
currentOutputs[data.hasOwnProperty("idx") ? data.idx : data._idx] = i;
|
||||
});
|
||||
outputCount.val(JSON.stringify(currentOutputs));
|
||||
},
|
||||
sortItems: function(answers) {
|
||||
var currentOutputs = JSON.parse(outputCount.val() || "{}");
|
||||
var answers = $("#node-input-answer-container").editableList("items");
|
||||
answers.each(function(i) {
|
||||
$(this).find(".node-input-answer-index").html(i + 1);
|
||||
var data = $(this).data("data");
|
||||
currentOutputs[data.hasOwnProperty("idx")? data.idx : data._idx] = i;
|
||||
});
|
||||
outputCount.val(JSON.stringify(currentOutputs));
|
||||
},
|
||||
sortable: true,
|
||||
removable: true
|
||||
});
|
||||
|
||||
for (var i=0; i < this.answers.length; i++) {
|
||||
var answer = this.answers[i];
|
||||
$("#node-input-answer-container").editableList("addItem", { idx: i, answer: answer });
|
||||
}
|
||||
},
|
||||
oneditsave: function() {
|
||||
var answers = $("#node-input-answer-container").editableList("items");
|
||||
var node = this;
|
||||
|
||||
node.answers = [];
|
||||
|
||||
answers.each(function(i){
|
||||
var answerData = $(this).data("data");
|
||||
var answer = $(this);
|
||||
var value = answer.find(".node-input-answer-value").typedInput("value");
|
||||
|
||||
node.answers.push(value);
|
||||
});
|
||||
},
|
||||
oneditresize: function(size) {
|
||||
var rows = $("#dialog-form>div:not(.node-input-answer-container-row)");
|
||||
var height = size.height;
|
||||
|
||||
for (var i = 0; i < rows.size(); i++) {
|
||||
height -= $(rows[i]).outerHeight(true);
|
||||
}
|
||||
|
||||
var editorRow = $("#dialog-form>div.node-input-answer-container-row");
|
||||
height -= (parseInt(editorRow.css("marginTop")) + parseInt(editorRow.css("marginBottom")));
|
||||
$("#node-input-answer-container").editableList("height",height);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="telegrambot-switch">
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-bot">Bot</label>
|
||||
<input type="text" id="node-input-bot" placeholder="Bot">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-chatId">Chat ID</label>
|
||||
<input type="text" id="node-input-chatId" placeholder="(e.g. 1234)">
|
||||
</div>
|
||||
<div class="form-row node-type-duration">
|
||||
<label>Timeout</label>
|
||||
<input type="text" id="node-input-timeoutValue" style="text-align:end; width:70px !important">
|
||||
<select id="node-input-timeoutUnits" style="width:140px !important">
|
||||
<option value="">disabled</option>
|
||||
<option value="ms">milliseconds</option>
|
||||
<option value="s">seconds</option>
|
||||
<option value="min">minutes</option>
|
||||
<option value="hr">hours</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-autoAnswerCallback">Auto-Answer</label>
|
||||
<input type="checkbox" id="node-input-autoAnswerCallback" style="display:inline-block; width:auto; vertical-align:top;">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-question">Question</label>
|
||||
<input type="text" id="node-input-question" placeholder="(e.g. Should I continue?)">
|
||||
</div>
|
||||
<div class="form-row node-input-answer-container-row">
|
||||
<input type="hidden" id="node-input-outputs"/>
|
||||
<ol id="node-input-answer-container"></ol>
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="telegrambot-switch">
|
||||
<p>Route messages based on a response from a user via Telegram</p>
|
||||
|
||||
<h3>Details</h3>
|
||||
<p>When a message arrives, a message will be sent to the specified Telegram bot + chat ID with
|
||||
the configured answers presented as quick reply options. When the user selects a reply, the
|
||||
message will be forwarded to the corresponding output.</p>
|
||||
|
||||
<dl class="message-properties">
|
||||
<dt>Name <span class="property-type">string</span></dt>
|
||||
<dd>Label for this node for easy reference</dd>
|
||||
|
||||
<dt>Bot <span class="property-type">string</span></dt>
|
||||
<dd>Telegram bot configuration for sending the message</dd>
|
||||
|
||||
<dt class="optional">Chat ID <span class="property-type">int</span></dt>
|
||||
<dd>Optional. Telegram chat ID to send the message. Specify a value to have this node send to the same chat every time. Leave blank to allow passing in the chat ID via <code>msg.telegram.chat.id</code>.</dd>
|
||||
|
||||
<dt class="optional">Auto-Answer <span class="property-type">list</span></dt>
|
||||
<dd>When enabled, automatically respond to the Telegram callback to resolve it as finished. Otherwise, you will have to send a payload to Telegram yourself to mark the request as complete.</dd>
|
||||
|
||||
<dt class="optional">Timeout <span class="property-type">int</span></dt>
|
||||
<dd>If provided, the message will time out after the specific time in milliseconds and the reply options will be removed from the chat.</dd>
|
||||
|
||||
<dt class="optional">Question <span class="property-type">string</span></dt>
|
||||
<dd>Question to ask the user. If not provided, <code>msg.payload</code> will be used.</dd>
|
||||
|
||||
<dt>Answers <span class="property-type">list</span></dt>
|
||||
<dd>The answer choices to present the user. Each answer will correspond to its designated output.</dd>
|
||||
</dl>
|
||||
|
||||
<h3>Outputs</h3>
|
||||
|
||||
<dl class="message-properties">
|
||||
<dt>payload</dt>
|
||||
<dd>The original message that entered the node.</dd>
|
||||
|
||||
<dt>telegram</dt>
|
||||
<dd>Telegram metadata of the sent message.</dd>
|
||||
|
||||
<dt>chatId <span class="property-type">deprecated</span></dt>
|
||||
<dd>The chat ID the messages are being sent/received in. Use <code>msg.telegram.chat.id</code> instead.</dd>
|
||||
|
||||
<dt>messageId <span class="property-type">deprecated</span></dt>
|
||||
<dd>The ID of the message that was sent prompting for a response. Use <code>msg.telegram.message_id</code> instead.</dd>
|
||||
</dl>
|
||||
|
||||
<p>In the case of a timeout, the original message is sent to the last output labeled "timeout". Please note that in the case that Node-RED restarts, the timeout will silently fail.</p>
|
||||
</script>
|
||||
181
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/switch/switch.js
generated
vendored
Normal file
181
nodered/rootfs/data/node_modules/node-red-contrib-telegrambot-home/nodes/switch/switch.js
generated
vendored
Normal file
@@ -0,0 +1,181 @@
|
||||
var utils = require('../../lib/utils.js');
|
||||
|
||||
module.exports = function(RED) {
|
||||
function SwitchNode(config) {
|
||||
RED.nodes.createNode(this, config);
|
||||
var node = this;
|
||||
|
||||
// Get base configuration
|
||||
this.bot = RED.nodes.getNode(config.bot);
|
||||
this.chatId = parseInt(config.chatId);
|
||||
this.question = config.question || "";
|
||||
this.answers = config.answers || [];
|
||||
this.timeoutValue = config.timeoutValue || null;
|
||||
this.timeoutUnits = config.timeoutUnits || null;
|
||||
this.timeoutCallback = null;
|
||||
this.autoAnswerCallback = config.autoAnswerCallback;
|
||||
|
||||
// Initialize bot
|
||||
utils.initializeBot(node);
|
||||
|
||||
// Verify inputs
|
||||
if (isNaN(this.chatId)) {
|
||||
this.chatId = null;
|
||||
}
|
||||
|
||||
if (this.timeoutValue !== null) {
|
||||
if (this.timeoutUnits === null) {
|
||||
utils.updateNodeStatusFailed(node, "timeout units not provided");
|
||||
return;
|
||||
}
|
||||
|
||||
this.timeoutDuration = utils.timeUnits(parseInt(this.timeoutValue, 10), this.timeoutUnits);
|
||||
|
||||
if (this.timeoutDuration === NaN) {
|
||||
utils.updateNodeStatusFailed(node, "timeout not parsable");
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.timeoutDuration <= 0) {
|
||||
utils.updateNodeStatusFailed(node, "timeout should be greater than 0");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Compute output ports
|
||||
var portCount = (this.timeoutValue === null) ? this.answers.length : this.answers.length + 1;
|
||||
var ports = new Array(portCount);
|
||||
|
||||
for (var i = 0; i < portCount; i++) {
|
||||
ports[i] = null;
|
||||
}
|
||||
|
||||
this.on("input", function(msg){
|
||||
if (!utils.validateChatId(node, msg)) {
|
||||
utils.updateNodeStatusFailed(node, "message has no chatID");
|
||||
return;
|
||||
}
|
||||
|
||||
var chatId = node.chatId || msg.telegram.chat.id;
|
||||
var question = node.question || msg.payload;
|
||||
var answers = node.answers || [];
|
||||
|
||||
if (question && answers.length > 0) {
|
||||
var listener = function(botMsg){
|
||||
var username = botMsg.from.username;
|
||||
var fromChatId = botMsg.message.chat.id;
|
||||
var messageId = botMsg.message.message_id;
|
||||
var callbackQueryId = botMsg.id;
|
||||
|
||||
if (botMsg.data && fromChatId === chatId && messageId === msg.telegram.messageId) {
|
||||
if (node.bot.isAuthorized(chatId, username)) {
|
||||
// Remove this listener since we got our reply
|
||||
node.telegramBot.removeListener("callback_query", listener);
|
||||
|
||||
// Clear the timeout
|
||||
if (node.timeoutCallback) {
|
||||
clearTimeout(node.timeoutCallback);
|
||||
}
|
||||
|
||||
// Update node status
|
||||
utils.updateNodeStatusSuccess(node);
|
||||
|
||||
if (node.autoAnswerCallback) {
|
||||
// Answer the callback so progress can stop showing
|
||||
node.telegramBot.answerCallbackQuery(callbackQueryId).then(function(sent){
|
||||
// nothing to do here
|
||||
});
|
||||
|
||||
// Remove quick reply options
|
||||
node.telegramBot.editMessageReplyMarkup("{}", { chat_id: chatId, message_id: messageId }).then(function(sent){
|
||||
// nothing to do here
|
||||
});
|
||||
}
|
||||
|
||||
// Augment original message with additional Telegram info
|
||||
msg.telegram.callbackQueryId = callbackQueryId;
|
||||
|
||||
// Continue with the original message
|
||||
var outPorts = ports.slice(0);
|
||||
var outputPort = parseInt(botMsg.data);
|
||||
|
||||
if (!isNaN(outputPort) && outputPort < portCount) {
|
||||
outPorts[outputPort] = msg;
|
||||
node.send(outPorts);
|
||||
} else {
|
||||
node.warn("invalid callback data received from telegram");
|
||||
}
|
||||
} else {
|
||||
node.warn(`received callback in ${chatId} from '${username}' was unauthorized`);
|
||||
}
|
||||
} else {
|
||||
// This is not the listener you are looking for
|
||||
}
|
||||
};
|
||||
|
||||
var timeoutListener = function(sentMsg){
|
||||
utils.updateNodeStatus(node, "yellow", "dot", "timed out waiting for reply");
|
||||
|
||||
// Remove this listener
|
||||
node.telegramBot.removeListener("callback_query", listener);
|
||||
|
||||
var messageId = sentMsg.message_id;
|
||||
var sentChatId = sentMsg.chat.id;
|
||||
|
||||
// Remove reply keyboard from message
|
||||
if (messageId && sentChatId) {
|
||||
node.telegramBot.editMessageReplyMarkup("{}", { chat_id: sentChatId, message_id: messageId }).then(function(sent){
|
||||
// nothing to do here
|
||||
});
|
||||
}
|
||||
|
||||
// output to timeout
|
||||
var outPorts = ports.slice(0);
|
||||
var outputPort = portCount - 1;
|
||||
outPorts[outputPort] = msg;
|
||||
node.send(outPorts);
|
||||
};
|
||||
|
||||
|
||||
var chunkSize = 4000;
|
||||
var answerOpts = answers.map(function(answer, idx){
|
||||
return { text: answer, callback_data: idx };
|
||||
});
|
||||
var options = {
|
||||
reply_markup: {
|
||||
inline_keyboard: [answerOpts]
|
||||
}
|
||||
};
|
||||
|
||||
if (question.length > chunkSize) {
|
||||
utils.updateNodeStatusFailed(node, "message larger than allowed chunk size");
|
||||
} else {
|
||||
node.telegramBot.sendMessage(chatId, question, options).then(function(sent){
|
||||
// Store sent message so we know how to respond later
|
||||
msg.telegram = sent;
|
||||
msg.telegram.chatId = chatId; // deprecated
|
||||
msg.telegram.messageId = sent.message_id; // deprecated
|
||||
|
||||
if (node.timeoutDuration > 0) {
|
||||
node.timeoutCallback = setTimeout(function(){
|
||||
timeoutListener(sent);
|
||||
}, node.timeoutDuration);
|
||||
utils.updateNodeStatusPending(node, `waiting for reply (${node.timeoutValue}${node.timeoutUnits})`);
|
||||
} else {
|
||||
utils.updateNodeStatusPending(node, "waiting for reply");
|
||||
}
|
||||
});
|
||||
|
||||
node.telegramBot.on("callback_query", listener);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.on("close", function(){
|
||||
node.telegramBot.off("message");
|
||||
node.status({});
|
||||
});
|
||||
};
|
||||
|
||||
RED.nodes.registerType("telegrambot-switch", SwitchNode);
|
||||
};
|
||||
Reference in New Issue
Block a user