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

Binary file not shown.

After

Width:  |  Height:  |  Size: 984 B

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

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