Intial Commit

This commit is contained in:
valki
2020-10-17 18:42:50 +02:00
commit 664c6d8ca3
5892 changed files with 759183 additions and 0 deletions

View 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>

View 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);
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 984 B