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

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