From 58364cd0de818fd96a7d7e6667d17394b4c04f9a Mon Sep 17 00:00:00 2001 From: Jochen Friedrich Date: Tue, 25 Feb 2020 16:51:17 +0000 Subject: [PATCH] Implement send and receive text messages --- matrix.html | 88 +++++++++++++++++++++++++++++++++++++++++ matrix.js | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 197 insertions(+), 1 deletion(-) diff --git a/matrix.html b/matrix.html index fe7b331..f6dffae 100644 --- a/matrix.html +++ b/matrix.html @@ -116,6 +116,50 @@

Matrix input node

+ + + + + + + + + + + + + + + + diff --git a/matrix.js b/matrix.js index b9d29b6..c893041 100644 --- a/matrix.js +++ b/matrix.js @@ -104,7 +104,7 @@ module.exports = function(RED) { return; // ignore our own messages } - if (node.room && (node.room !== event.room)) { + if (node.room && (node.room !== room.roomId)) { return; } @@ -124,6 +124,58 @@ module.exports = function(RED) { } RED.nodes.registerType("matrix-input",MatrixInputNode); + function MatrixRecvTextNode(config) { + RED.nodes.createNode(this,config); + var node = this; + + node.configNode = RED.nodes.getNode(config.server); + + node.room = config.room; + node.filterself = config.filterself; + + 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) { + if (node.filterself && (!event.getSender() || event.getSender() === node.configNode.userid)) { + return; // ignore our own messages + } + + if (node.room && (node.room !== room.roomId)) { + return; + } + + if (event.event.type !== "m.room.message") { + return; + } + + var msg = { + payload: event.getContent().body, + sender: event.getSender(), + room: room.roomId + }; + + node.send(msg); + }); + }); + + this.on("close", function(done) { + done(); + }); + + } + RED.nodes.registerType("matrix-recvtext",MatrixRecvTextNode); + function MatrixOutputNode(config) { RED.nodes.createNode(this,config); var node = this; @@ -172,4 +224,60 @@ module.exports = function(RED) { } RED.nodes.registerType("matrix-output", MatrixOutputNode); + + function MatrixSendTextNode(config) { + RED.nodes.createNode(this,config); + var node = this; + + node.room = config.room; + node.notice = config.notice; + + node.configNode = RED.nodes.getNode(config.server); + + if (!node.configNode) { + node.warn("No configuration node"); + return; + } + + 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; + } + + var destRoom = ""; + if (node.room) { + destRoom = node.room; + } else if (node.configNode.room) { + destRoom = node.configNode.room; + } else { + node.warn("Room must be specified in configuration"); + return; + } + + if (node.notice) { + node.configNode.matrixClient.sendNotice(destRoom, msg.payload) + .then(function() { }) + .catch(function(e){ node.warn("Error sending message " + e); }); + } else { + node.configNode.matrixClient.sendTextMessage(destRoom, msg.payload) + .then(function() { }) + .catch(function(e){ node.warn("Error sending message " + e); }); + } + }); + + this.on("close", function(done) { + done(); + }); + } + + RED.nodes.registerType("matrix-sendtext", MatrixSendTextNode); }