Upgrade of submodules

This commit is contained in:
Plague Doctor 2020-11-09 08:30:19 +11:00
parent 3547b92ab9
commit 4983272d9c
4 changed files with 344 additions and 829 deletions

View File

@ -1,218 +0,0 @@
<!-- ------------------------------------------------------------------------------------------ -->
<script type="text/javascript">
RED.nodes.registerType('matrix bot',{
category: 'config',
color: '#a6bbcf',
defaults: {
name: {type:"text"},
room: {type:"text"},
userId: {type:"text", required:true},
accessToken: {type:"text", required:true},
matrixServerURL: {type:"text", required:true, value:"https://matrix.org"}
},
inputs:0,
outputs:1,
icon: "matrix.png",
label: function() {
return this.name||"matrix bot";
}
});
</script>
<script type="text/x-red" data-template-name="matrix bot">
<div class="form-row">
<label for="node-config-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-config-input-userId"><i class="fa fa-user"></i> User ID</label>
<input type="text" id="node-config-input-userId">
</div>
<div class="form-row">
<label for="node-config-input-accessToken"><i class="fa fa-key"></i> Access Token</label>
<input type="text" id="node-config-input-accessToken">
</div>
<div class="form-row">
<label for="node-config-input-matrixServerURL"><i class="fa fa-globe"></i> Server URL</label>
<input type="text" id="node-config-input-matrixServerURL">
</div>
<div class="form-row">
<label for="node-config-input-room"><i class="fa fa-comments-o"></i> Room ID (optional)</label>
<input type="text" id="node-config-input-room">
</div>
</script>
<script type="text/x-red" data-help-name="matrix bot">
<p>A configuration node that holds the details to connect to a Matrix chat server</p>
<p>
<ul>
<li><code>User ID</code>: the user ID in the Matrix server, for instance @mybot:matrix.org</li>
<li><code>Access Token</code>: the access token of the user (it can be retrieved from the HTTP request URLs when connecting to the server using a browser)</li>
<li><code>Server URL</code>: URL of the Matrix homeserver, e.g. https://matrix.org</li>
<li><code>Room ID</code>: ID of the chatroom to join when starting. If no room is specified, it will automatically join any room where it is invited</li>
</ul>
</p>
</script>
<!-- ------------------------------------------------------------------------------------------ -->
<script type="text/javascript">
RED.nodes.registerType('matrix sender',{
category: 'output',
color: '#a6bbcf',
defaults: {
name: { value: "" },
bot: { value:"", type: "matrix bot", required: true }
},
inputs:1,
outputs:0,
icon: "matrix.png",
label: function() {
return this.name||"matrix sender";
}
});
</script>
<script type="text/x-red" data-template-name="matrix sender">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-bot"><i class="fa fa-globe"></i> Connection</label>
<input type="text" id="node-input-bot" placeholder="Bot">
</div>
</script>
<script type="text/x-red" data-help-name="matrix sender">
<p>An output Matrix node that sends a message to a chatroom.</p>
<p>Sends the <code>msg.payload</code> property as text to the chatroom is specified either by <code>msg.roomId</code> (which is filled by receiver nodes) or in the configuration node.</p>
</script>
<!-- ------------------------------------------------------------------------------------------ -->
<script type="text/javascript">
RED.nodes.registerType('matrix file sender',{
category: 'output',
color: '#a6bbcf',
defaults: {
name: { value: "" },
bot: { value:"", type: "matrix bot", required: true }
},
inputs:1,
outputs:0,
icon: "matrix.png",
label: function() {
return this.name||"matrix file sender";
}
});
</script>
<script type="text/x-red" data-template-name="matrix file sender">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-bot"><i class="fa fa-globe"></i> Connection</label>
<input type="text" id="node-input-bot" placeholder="Bot">
</div>
</script>
<script type="text/x-red" data-help-name="matrix file sender">
<p>An output Matrix node that sends a message to a chatroom.</p>
<p>Sends the <code>msg.payload</code> property as text to the chatroom is specified either by <code>msg.roomId</code> (which is filled by receiver nodes) or in the configuration node.</p>
</script>
<!-- ------------------------------------------------------------------------------------------ -->
<script type="text/javascript">
RED.nodes.registerType('matrix receiver',{
category: 'input',
color: '#a6bbcf',
defaults: {
name: { value: "" },
bot: { value:"", type: "matrix bot", required: true }
},
inputs:0,
outputs:1,
icon: "matrix.png",
label: function() {
return this.name||"matrix receiver";
}
});
</script>
<script type="text/x-red" data-template-name="matrix receiver">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-bot"><i class="fa fa-globe"></i> Connection</label>
<input type="text" id="node-input-bot" placeholder="Bot">
</div>
</script>
<script type="text/x-red" data-help-name="matrix receiver">
<p>An input Matrix node that outputs messages received in a chatroom.</p>
<p>Outputs a message with the properties:
<ul>
<li><code>msg.payload</code>: text of the received message</li>
<li><code>msg.sender</code>: ID of the sender</li>
<li><code>msg.roomId</code>: ID of the room where it was sent</li>
</ul>
</p>
</script>
<!-- ------------------------------------------------------------------------------------------ -->
<script type="text/javascript">
RED.nodes.registerType('matrix command',{
category: 'input',
color: '#a6bbcf',
defaults: {
name: { value: "" },
bot: { value:"", type: "matrix bot", required: true },
command: { value: "", required: true }
},
inputs:0,
outputs:1,
icon: "matrix.png",
label: function() {
return this.name||"matrix command";
}
});
</script>
<script type="text/x-red" data-template-name="matrix command">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-command"><i class="fa fa-terminal"></i> Command</label>
<input type="text" id="node-input-command" placeholder="Command">
</div>
<div class="form-row">
<label for="node-input-bot"><i class="fa fa-globe"></i> Connection</label>
<input type="text" id="node-input-bot" placeholder="Bot">
</div>
</script>
<script type="text/x-red" data-help-name="matrix command">
<p>An input Matrix node that triggers the output when a command is received from the chatroom.</p>
<p>Outputs a message with the properties:
<ul>
<li><code>msg.payload</code>: rest of the received text (excluding the command)</li>
<li><code>msg.sender</code>: ID of the sender</li>
<li><code>msg.roomId</code>: ID of the room where it was sent</li>
<li><code>msg.originalMessage</code>: text sent in the chat (including the command)</li>
</ul>
</p>
</script>

View File

@ -1,393 +0,0 @@
module.exports = function (RED) {
"use strict";
var sdk = require("matrix-js-sdk");
var md = require("markdown-it")();
const fs = require('fs');
// --------------------------------------------------------------------------------------------
// The configuration node holds the configuration and credentials for all nodes.
function MatrixBotNode(config) {
RED.nodes.createNode(this, config);
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
node.log("Initializing Matrix Bot node");
// Configuration options passed by Node Red
node.userId = config.userId;
node.room = config.room;
// TODO: Switch from configuration to credentials and check with if (this.credentials)
node.accessToken = config.accessToken;
node.matrixServerURL = config.matrixServerURL;
node.matrixClient = sdk.createClient({
baseUrl: node.matrixServerURL,
accessToken: node.accessToken,
userId: node.userId
});
// If no room is specified, join any room where we are invited
if (!node.room || node.room === "") {
node.matrixClient.on("RoomMember.membership", function (event, member) {
if (member.membership === "invite" && member.userId === node.userId) {
node.log("Trying to join room " + member.roomId);
node.matrixClient.joinRoom(member.roomId).then(function () {
node.log("Automatically accepted invitation to join room " + member.roomId);
}).catch(function (e) {
node.warn("Cannot join room (probably because I was kicked) " + member.roomId + ": " + e);
});
}
});
}
node.matrixClient.on("sync", function (state, prevState, data) {
switch (state) {
case "ERROR":
// update UI to say "Connection Lost"
node.warn("Connection to Matrix server lost");
node.updateConnectionState(false);
break;
case "SYNCING":
// update UI to remove any "Connection Lost" message
node.updateConnectionState(true);
break;
case "PREPARED":
// the client instance is ready to be queried.
node.log("Synchronized to Matrix server.");
if (node.room) {
node.log("Trying to join room " + node.room);
node.matrixClient.joinRoom(node.room, { syncRoom: false })
.then(function (joinedRoom) {
node.log("Joined " + node.room);
node.room = joinedRoom.roomId;
node.updateConnectionState(true);
}).catch(function (e) {
node.warn("Error joining " + node.room + ": " + e);
});
} else {
node.log("No room configured. Will only join rooms where I'm invited");
}
break;
}
});
node.log("Connecting to Matrix server...");
node.matrixClient.startClient();
// Called when the connection state may have changed
this.updateConnectionState = function (connected) {
if (node.connected !== connected) {
node.connected = connected;
if (connected) {
node.emit("connected");
} else {
node.emit("disconnected");
}
}
};
// When Node-RED updates nodes, disconnect from server to ensure a clean start
node.on("close", function (done) {
node.log("Matrix configuration node closing...");
if (node.matrixClient) {
node.log("Disconnecting from Matrix server...");
node.matrixClient.stopClient();
node.updateConnectionState(false);
}
done();
});
}
RED.nodes.registerType("matrix bot", MatrixBotNode);
// --------------------------------------------------------------------------------------------
// The output node sends a message to the chat.
function MatrixOutNode(config) {
RED.nodes.createNode(this, config);
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
// Configuration options passed by Node Red
node.configNode = RED.nodes.getNode(config.bot);
node.configNode.on("connected", function () {
node.status({ fill: "green", shape: "ring", text: "connected" });
});
node.configNode.on("disconnected", function () {
node.status({ fill: "red", shape: "ring", text: "disconnected" });
});
this.on("input", function (msg) {
if (!node.configNode || !node.configNode.matrixClient) {
node.warn("No configuration");
return;
}
if (msg.payload) {
node.log("Sending message " + msg.payload);
var destRoom = "";
if (msg.roomId) {
destRoom = msg.roomId;
} else if (node.configNode.room) {
destRoom = node.configNode.room;
} else {
node.warn("Room must be specified in msg.roomId or in configuration");
return;
}
node.configNode.matrixClient.sendHtmlMessage(destRoom, msg.payload.toString(), md.render(msg.payload.toString()))
.then(function () {
node.log("Message sent: " + msg.payload);
}).catch(function (e) {
node.warn("Error sending message " + e);
});
} else {
node.warn("msg.payload is empty");
}
});
this.on("close", function (done) {
node.log("Matrix out node closing...");
done();
});
}
RED.nodes.registerType("matrix sender", MatrixOutNode);
// --------------------------------------------------------------------------------------------
// Tim: File Send node
function MatrixFileOutNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.configNode = RED.nodes.getNode(config.bot);
node.configNode.on('connected', () => {
node.status({ fill: 'green', shape: 'ring', text: 'connected' });
});
node.configNode.on('disconnected', () => {
node.status({ fill: 'red', shape: 'ring', text: 'disconnected' });
});
this.on('input', msg => {
if (!node.configNode || !node.configNode.matrixClient) {
node.warn('No configuration');
return;
}
if (msg.payload) {
node.log('Sending message ' + msg.payload);
let destRoom = '';
if (msg.roomId) {
destRoom = msg.roomId;
}
else if (node.configNode.room) {
destRoom = node.configNode.room;
}
else {
node.warn('Room must be specified in msg.roomId or in config');
return;
}
const filePath = msg.payload;
fs.readFile(filePath, (err, data) => {
if (err) {
node.warn('Error reading file', err);
return;
}
const tks = filePath.split('/');
const fname = tks[tks.length - 1];
const opts = {
rawResponse: false,
name: fname
}
node.configNode.matrixClient.uploadContent(data, opts).then(res => {
node.log('success uploading file', res);
console.log('Uploaded file', res);
const imageUri = res.content_uri;
console.log('imageUri', imageUri);
node.configNode.matrixClient.sendImageMessage(destRoom, imageUri, {}, 'image.png', () => {
console.log('image send func');
}).then(() => {
node.log('image sent success');
console.log('image sent success')
}).catch(e => {
node.warn('error sending img: ', e);
console.log('error sending image', e)
})
}).catch(err => {
node.warn('Error uploading file', err);
})
});
}
else {
node.warn('msg.payload empty')
}
});
this.on('close', done => {
node.log('matrix file out node closing...');
done();
});
}
RED.nodes.registerType("matrix file sender", MatrixFileOutNode);
// --------------------------------------------------------------------------------------------
// The input node receives messages from the chat.
function MatrixInNode(config) {
RED.nodes.createNode(this, config);
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
node.configNode = RED.nodes.getNode(config.bot);
node.log("MatrixInNode initializing...");
if (!node.configNode) {
node.warn("No configuration node");
return;
}
node.status({ fill: "red", shape: "ring", text: "disconnected" });
node.configNode.on("disconnected", function () {
node.status({ fill: "red", shape: "ring", text: "disconnected" });
});
node.configNode.on("connected", function () {
node.status({ fill: "green", shape: "ring", text: "connected" });
node.configNode.matrixClient.on("Room.timeline", function (event, room, toStartOfTimeline, data) {
if (toStartOfTimeline) {
return; // don't print paginated results
}
if (event.getType() !== "m.room.message") {
return; // only keep messages
}
if (!event.getSender() || event.getSender() === node.configNode.userId) {
return; // ignore our own messages
}
if (!event.getUnsigned() || event.getUnsigned().age > 1000) {
return; // ignore old messages
}
// TODO process messages other than text
node.log(
// the room name will update with m.room.name events automatically
"Received chat message: (" + room.name + ") " + event.getSender() + " :: " + event.getContent().body
);
var msg = {
payload: event.getContent().body,
sender: event.getSender(),
roomId: room.roomId
};
node.send(msg);
});
});
this.on("close", function (done) {
node.log("Matrix in node closing...");
done();
});
}
RED.nodes.registerType("matrix receiver", MatrixInNode);
// --------------------------------------------------------------------------------------------
// The command node receives messages from the chat.
function MatrixCommandNode(config) {
RED.nodes.createNode(this, config);
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
node.command = config.command;
node.configNode = RED.nodes.getNode(config.bot);
node.log("MatrixCommandNode initializing...");
if (!node.configNode) {
node.warn("No configuration node");
return;
}
node.status({ fill: "red", shape: "ring", text: "disconnected" });
node.configNode.on("disconnected", function () {
node.status({ fill: "red", shape: "ring", text: "disconnected" });
});
node.configNode.on("connected", function () {
node.status({ fill: "green", shape: "ring", text: "connected" });
node.configNode.matrixClient.on("Room.timeline", function (event, room, toStartOfTimeline, data) {
if (toStartOfTimeline) {
return; // don't print paginated results
}
if (event.getType() !== "m.room.message") {
return; // only keep messages
}
if (!event.getSender() || event.getSender() === node.configNode.userId) {
return; // ignore our own messages
}
if (!event.getUnsigned() || event.getUnsigned().age > 1000) {
return; // ignore old messages
}
// TODO process messages other than text
node.log(
// the room name will update with m.room.name events automatically
"Received chat message: (" + room.name + ") " + event.getSender() + " :: " + event.getContent().body
);
var message = event.getContent().body;
var tokens = message.split(" ");
if (tokens[0] == node.command) {
node.log("Recognized command " + node.command + " Processing...");
var remainingText = message.replace(node.command, "");
var msg = {
payload: remainingText,
sender: event.getSender(),
roomId: room.roomId,
originalMessage: message
};
node.send([msg, null]);
}
});
});
this.on("close", function (done) {
node.log("Matrix command node closing...");
done();
});
}
RED.nodes.registerType("matrix command", MatrixCommandNode);
}

View File

@ -94,6 +94,42 @@
<!-- ------------------------------------------------------------------------------------------ -->
<script type="text/javascript">
RED.nodes.registerType('matrix file sender',{
category: 'output',
color: '#a6bbcf',
defaults: {
name: { value: "" },
bot: { value:"", type: "matrix bot", required: true }
},
inputs:1,
outputs:0,
icon: "matrix.png",
label: function() {
return this.name||"matrix file sender";
}
});
</script>
<script type="text/x-red" data-template-name="matrix file sender">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-bot"><i class="fa fa-globe"></i> Connection</label>
<input type="text" id="node-input-bot" placeholder="Bot">
</div>
</script>
<script type="text/x-red" data-help-name="matrix file sender">
<p>An output Matrix node that sends a message to a chatroom.</p>
<p>Sends the <code>msg.payload</code> property as text to the chatroom is specified either by <code>msg.roomId</code> (which is filled by receiver nodes) or in the configuration node.</p>
</script>
<!-- ------------------------------------------------------------------------------------------ -->
<script type="text/javascript">
RED.nodes.registerType('matrix receiver',{
category: 'input',

View File

@ -1,303 +1,393 @@
module.exports = function(RED) {
module.exports = function (RED) {
"use strict";
"use strict";
var sdk = require("matrix-js-sdk");
var md = require("markdown-it")();
var sdk = require("matrix-js-sdk");
var md = require("markdown-it")();
const fs = require('fs');
// --------------------------------------------------------------------------------------------
// The configuration node holds the configuration and credentials for all nodes.
// --------------------------------------------------------------------------------------------
// The configuration node holds the configuration and credentials for all nodes.
function MatrixBotNode(config) {
RED.nodes.createNode(this, config);
function MatrixBotNode(config) {
RED.nodes.createNode(this, config);
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
node.log("Initializing Matrix Bot node");
node.log("Initializing Matrix Bot node");
// Configuration options passed by Node Red
node.userId = config.userId;
node.room = config.room;
// Configuration options passed by Node Red
node.userId = config.userId;
node.room = config.room;
// TODO: Switch from configuration to credentials and check with if (this.credentials)
node.accessToken = config.accessToken;
node.matrixServerURL = config.matrixServerURL;
// TODO: Switch from configuration to credentials and check with if (this.credentials)
node.accessToken = config.accessToken;
node.matrixServerURL = config.matrixServerURL;
node.matrixClient = sdk.createClient({
baseUrl: node.matrixServerURL,
accessToken: node.accessToken,
userId: node.userId
});
node.matrixClient = sdk.createClient({
baseUrl: node.matrixServerURL,
accessToken: node.accessToken,
userId: node.userId
});
// If no room is specified, join any room where we are invited
if (!node.room || node.room === "") {
node.matrixClient.on("RoomMember.membership", function(event, member) {
if (member.membership === "invite" && member.userId === node.userId) {
node.log("Trying to join room " + member.roomId);
node.matrixClient.joinRoom(member.roomId).then(function() {
node.log("Automatically accepted invitation to join room " + member.roomId);
}).catch(function(e) {
node.warn("Cannot join room (probably because I was kicked) " + member.roomId + ": " + e);
});
}
});
}
// If no room is specified, join any room where we are invited
if (!node.room || node.room === "") {
node.matrixClient.on("RoomMember.membership", function (event, member) {
if (member.membership === "invite" && member.userId === node.userId) {
node.log("Trying to join room " + member.roomId);
node.matrixClient.joinRoom(member.roomId).then(function () {
node.log("Automatically accepted invitation to join room " + member.roomId);
}).catch(function (e) {
node.warn("Cannot join room (probably because I was kicked) " + member.roomId + ": " + e);
});
}
});
}
node.matrixClient.on("sync", function(state, prevState, data) {
switch (state) {
case "ERROR":
// update UI to say "Connection Lost"
node.warn("Connection to Matrix server lost");
node.updateConnectionState(false);
break;
case "SYNCING":
// update UI to remove any "Connection Lost" message
node.updateConnectionState(true);
break;
case "PREPARED":
// the client instance is ready to be queried.
node.log("Synchronized to Matrix server.");
node.matrixClient.on("sync", function (state, prevState, data) {
switch (state) {
case "ERROR":
// update UI to say "Connection Lost"
node.warn("Connection to Matrix server lost");
node.updateConnectionState(false);
break;
case "SYNCING":
// update UI to remove any "Connection Lost" message
node.updateConnectionState(true);
break;
case "PREPARED":
// the client instance is ready to be queried.
node.log("Synchronized to Matrix server.");
if (node.room) {
node.log("Trying to join room " + node.room);
if (node.room) {
node.log("Trying to join room " + node.room);
node.matrixClient.joinRoom(node.room, {syncRoom:false})
.then(function(joinedRoom) {
node.log("Joined " + node.room);
node.room = joinedRoom.roomId;
node.updateConnectionState(true);
}).catch(function(e) {
node.warn("Error joining " + node.room + ": " + e);
});
} else {
node.log("No room configured. Will only join rooms where I'm invited");
}
break;
}
});
node.matrixClient.joinRoom(node.room, { syncRoom: false })
.then(function (joinedRoom) {
node.log("Joined " + node.room);
node.room = joinedRoom.roomId;
node.updateConnectionState(true);
}).catch(function (e) {
node.warn("Error joining " + node.room + ": " + e);
});
} else {
node.log("No room configured. Will only join rooms where I'm invited");
}
break;
}
});
node.log("Connecting to Matrix server...");
node.log("Connecting to Matrix server...");
node.matrixClient.startClient();
node.matrixClient.startClient();
// Called when the connection state may have changed
this.updateConnectionState = function(connected){
if (node.connected !== connected) {
node.connected = connected;
if (connected) {
node.emit("connected");
} else {
node.emit("disconnected");
}
}
this.updateConnectionState = function (connected) {
if (node.connected !== connected) {
node.connected = connected;
if (connected) {
node.emit("connected");
} else {
node.emit("disconnected");
}
}
};
// When Node-RED updates nodes, disconnect from server to ensure a clean start
node.on("close", function (done) {
node.log("Matrix configuration node closing...");
if (node.matrixClient) {
node.log("Disconnecting from Matrix server...");
node.matrixClient.stopClient();
node.updateConnectionState(false);
}
done();
});
node.on("close", function (done) {
node.log("Matrix configuration node closing...");
if (node.matrixClient) {
node.log("Disconnecting from Matrix server...");
node.matrixClient.stopClient();
node.updateConnectionState(false);
}
done();
});
}
}
RED.nodes.registerType("matrix bot", MatrixBotNode);
RED.nodes.registerType("matrix bot", MatrixBotNode);
// --------------------------------------------------------------------------------------------
// The output node sends a message to the chat.
// --------------------------------------------------------------------------------------------
// The output node sends a message to the chat.
function MatrixOutNode(config) {
RED.nodes.createNode(this, config);
function MatrixOutNode(config) {
RED.nodes.createNode(this, config);
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
// Configuration options passed by Node Red
node.configNode = RED.nodes.getNode(config.bot);
node.configNode.on("connected", function(){
node.status({ fill: "green", shape: "ring", text: "connected" });
node.configNode.on("connected", function () {
node.status({ fill: "green", shape: "ring", text: "connected" });
});
node.configNode.on("disconnected", function(){
node.status({ fill: "red", shape: "ring", text: "disconnected" });
node.configNode.on("disconnected", function () {
node.status({ fill: "red", shape: "ring", text: "disconnected" });
});
this.on("input", function (msg) {
if (! node.configNode || ! node.configNode.matrixClient) {
node.warn("No configuration");
return;
}
if (!node.configNode || !node.configNode.matrixClient) {
node.warn("No configuration");
return;
}
if (msg.payload) {
node.log("Sending message " + msg.payload);
node.log("Sending message " + msg.payload);
var destRoom = "";
if (msg.roomId) {
destRoom = msg.roomId;
} else if (node.configNode.room) {
destRoom = node.configNode.room;
} else {
node.warn("Room must be specified in msg.roomId or in configuration");
return;
}
var destRoom = "";
if (msg.roomId) {
destRoom = msg.roomId;
} else if (node.configNode.room) {
destRoom = node.configNode.room;
} else {
node.warn("Room must be specified in msg.roomId or in configuration");
return;
}
node.configNode.matrixClient.sendHtmlMessage(destRoom, msg.payload.toString(), md.render(msg.payload.toString()))
.then(function() {
node.log("Message sent: " + msg.payload);
}).catch(function(e){
node.warn("Error sending message " + e);
});
} else {
node.configNode.matrixClient.sendHtmlMessage(destRoom, msg.payload.toString(), md.render(msg.payload.toString()))
.then(function () {
node.log("Message sent: " + msg.payload);
}).catch(function (e) {
node.warn("Error sending message " + e);
});
} else {
node.warn("msg.payload is empty");
}
});
});
this.on("close", function(done) {
node.log("Matrix out node closing...");
done();
});
this.on("close", function (done) {
node.log("Matrix out node closing...");
done();
});
}
RED.nodes.registerType("matrix sender", MatrixOutNode);
RED.nodes.registerType("matrix sender", MatrixOutNode);
// --------------------------------------------------------------------------------------------
// Tim: File Send node
function MatrixFileOutNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.configNode = RED.nodes.getNode(config.bot);
node.configNode.on('connected', () => {
node.status({ fill: 'green', shape: 'ring', text: 'connected' });
});
node.configNode.on('disconnected', () => {
node.status({ fill: 'red', shape: 'ring', text: 'disconnected' });
});
this.on('input', msg => {
if (!node.configNode || !node.configNode.matrixClient) {
node.warn('No configuration');
return;
}
if (msg.payload) {
node.log('Sending message ' + msg.payload);
let destRoom = '';
if (msg.roomId) {
destRoom = msg.roomId;
}
else if (node.configNode.room) {
destRoom = node.configNode.room;
}
else {
node.warn('Room must be specified in msg.roomId or in config');
return;
}
const filePath = msg.payload;
fs.readFile(filePath, (err, data) => {
if (err) {
node.warn('Error reading file', err);
return;
}
const tks = filePath.split('/');
const fname = tks[tks.length - 1];
const opts = {
rawResponse: false,
name: fname
}
node.configNode.matrixClient.uploadContent(data, opts).then(res => {
node.log('success uploading file', res);
console.log('Uploaded file', res);
const imageUri = res.content_uri;
console.log('imageUri', imageUri);
node.configNode.matrixClient.sendImageMessage(destRoom, imageUri, {}, 'image.png', () => {
console.log('image send func');
}).then(() => {
node.log('image sent success');
console.log('image sent success')
}).catch(e => {
node.warn('error sending img: ', e);
console.log('error sending image', e)
})
}).catch(err => {
node.warn('Error uploading file', err);
})
});
}
else {
node.warn('msg.payload empty')
}
});
this.on('close', done => {
node.log('matrix file out node closing...');
done();
});
}
RED.nodes.registerType("matrix file sender", MatrixFileOutNode);
// --------------------------------------------------------------------------------------------
// The input node receives messages from the chat.
// --------------------------------------------------------------------------------------------
// The input node receives messages from the chat.
function MatrixInNode(config) {
RED.nodes.createNode(this, config);
function MatrixInNode(config) {
RED.nodes.createNode(this, config);
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
node.configNode = RED.nodes.getNode(config.bot);
node.log("MatrixInNode initializing...");
if (!node.configNode) {
node.warn("No configuration node");
return;
node.warn("No configuration node");
return;
}
node.status({ fill: "red", shape: "ring", text: "disconnected" });
node.configNode.on("disconnected", function(){
node.status({ fill: "red", shape: "ring", text: "disconnected" });
node.configNode.on("disconnected", function () {
node.status({ fill: "red", shape: "ring", text: "disconnected" });
});
node.configNode.on("connected", function() {
node.status({ fill: "green", shape: "ring", text: "connected" });
node.configNode.matrixClient.on("Room.timeline", function(event, room, toStartOfTimeline, data) {
if (toStartOfTimeline) {
return; // don't print paginated results
}
if (event.getType() !== "m.room.message") {
return; // only keep messages
}
if (!event.getSender() || event.getSender() === node.configNode.userId) {
return; // ignore our own messages
}
if (!event.getUnsigned() || event.getUnsigned().age > 1000) {
return; // ignore old messages
}
// TODO process messages other than text
node.log(
// the room name will update with m.room.name events automatically
"Received chat message: (" + room.name + ") " + event.getSender() + " :: " + event.getContent().body
);
var msg = {
payload: event.getContent().body,
sender: event.getSender(),
roomId: room.roomId
};
node.send(msg);
});
});
node.configNode.on("connected", function () {
node.status({ fill: "green", shape: "ring", text: "connected" });
node.configNode.matrixClient.on("Room.timeline", function (event, room, toStartOfTimeline, data) {
if (toStartOfTimeline) {
return; // don't print paginated results
}
if (event.getType() !== "m.room.message") {
return; // only keep messages
}
if (!event.getSender() || event.getSender() === node.configNode.userId) {
return; // ignore our own messages
}
if (!event.getUnsigned() || event.getUnsigned().age > 1000) {
return; // ignore old messages
}
// TODO process messages other than text
node.log(
// the room name will update with m.room.name events automatically
"Received chat message: (" + room.name + ") " + event.getSender() + " :: " + event.getContent().body
);
var msg = {
payload: event.getContent().body,
sender: event.getSender(),
roomId: room.roomId
};
node.send(msg);
});
});
this.on("close", function(done) {
node.log("Matrix in node closing...");
done();
});
this.on("close", function (done) {
node.log("Matrix in node closing...");
done();
});
}
}
RED.nodes.registerType("matrix receiver", MatrixInNode);
RED.nodes.registerType("matrix receiver", MatrixInNode);
// --------------------------------------------------------------------------------------------
// The command node receives messages from the chat.
// --------------------------------------------------------------------------------------------
// The command node receives messages from the chat.
function MatrixCommandNode(config) {
RED.nodes.createNode(this, config);
function MatrixCommandNode(config) {
RED.nodes.createNode(this, config);
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
node.command = config.command;
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
node.command = config.command;
node.configNode = RED.nodes.getNode(config.bot);
node.log("MatrixCommandNode initializing...");
if (!node.configNode) {
node.warn("No configuration node");
return;
node.warn("No configuration node");
return;
}
node.status({ fill: "red", shape: "ring", text: "disconnected" });
node.configNode.on("disconnected", function(){
node.status({ fill: "red", shape: "ring", text: "disconnected" });
node.configNode.on("disconnected", function () {
node.status({ fill: "red", shape: "ring", text: "disconnected" });
});
node.configNode.on("connected", function() {
node.status({ fill: "green", shape: "ring", text: "connected" });
node.configNode.matrixClient.on("Room.timeline", function(event, room, toStartOfTimeline, data) {
if (toStartOfTimeline) {
return; // don't print paginated results
}
if (event.getType() !== "m.room.message") {
return; // only keep messages
}
if (!event.getSender() || event.getSender() === node.configNode.userId) {
return; // ignore our own messages
}
if (!event.getUnsigned() || event.getUnsigned().age > 1000) {
return; // ignore old messages
}
// TODO process messages other than text
node.log(
// the room name will update with m.room.name events automatically
"Received chat message: (" + room.name + ") " + event.getSender() + " :: " + event.getContent().body
);
node.configNode.on("connected", function () {
node.status({ fill: "green", shape: "ring", text: "connected" });
node.configNode.matrixClient.on("Room.timeline", function (event, room, toStartOfTimeline, data) {
if (toStartOfTimeline) {
return; // don't print paginated results
}
if (event.getType() !== "m.room.message") {
return; // only keep messages
}
if (!event.getSender() || event.getSender() === node.configNode.userId) {
return; // ignore our own messages
}
if (!event.getUnsigned() || event.getUnsigned().age > 1000) {
return; // ignore old messages
}
// TODO process messages other than text
node.log(
// the room name will update with m.room.name events automatically
"Received chat message: (" + room.name + ") " + event.getSender() + " :: " + event.getContent().body
);
var message = event.getContent().body;
var message = event.getContent().body;
var tokens = message.split(" ");
var tokens = message.split(" ");
if (tokens[0] == node.command) {
node.log("Recognized command " + node.command + " Processing...");
var remainingText = message.replace(node.command, "");
if (tokens[0] == node.command) {
node.log("Recognized command " + node.command + " Processing...");
var remainingText = message.replace(node.command, "");
var msg = {
payload: remainingText,
sender: event.getSender(),
roomId: room.roomId,
originalMessage: message
payload: remainingText,
sender: event.getSender(),
roomId: room.roomId,
originalMessage: message
};
node.send([msg, null]);
}
}
});
});
});
});
this.on("close", function(done) {
node.log("Matrix command node closing...");
done();
});
}
this.on("close", function (done) {
node.log("Matrix command node closing...");
done();
});
}
RED.nodes.registerType("matrix command", MatrixCommandNode);
RED.nodes.registerType("matrix command", MatrixCommandNode);
}