Implement send and receive text messages
This commit is contained in:
		
							parent
							
								
									640726bf37
								
							
						
					
					
						commit
						58364cd0de
					
				
					 2 changed files with 197 additions and 1 deletions
				
			
		
							
								
								
									
										88
									
								
								matrix.html
									
										
									
									
									
								
							
							
						
						
									
										88
									
								
								matrix.html
									
										
									
									
									
								
							|  | @ -116,6 +116,50 @@ | ||||||
|     <p>Matrix input node</p> |     <p>Matrix input node</p> | ||||||
| </script> | </script> | ||||||
| 
 | 
 | ||||||
|  | <!-- INPUT Node "matrix-recvtext" --> | ||||||
|  | 
 | ||||||
|  | <script type="text/javascript"> | ||||||
|  |     RED.nodes.registerType('matrix-recvtext',{ | ||||||
|  |         category: 'function', | ||||||
|  |         color: '#a6bbcf', | ||||||
|  |         defaults: { | ||||||
|  |             name: {value:""}, | ||||||
|  |             room: {value:""}, | ||||||
|  |             server: {value:"", type:"matrix-server"}, | ||||||
|  |             filterself: {value:true} | ||||||
|  |         }, | ||||||
|  |         inputs:0, | ||||||
|  |         outputs:1, | ||||||
|  |         icon: "matrix.png", | ||||||
|  |         label: function() { | ||||||
|  |             return this.name||"matrix-recvtext"; | ||||||
|  |         } | ||||||
|  |     }); | ||||||
|  | </script> | ||||||
|  | 
 | ||||||
|  | <script type="text/x-red" data-template-name="matrix-recvtext"> | ||||||
|  |     <div class="form-row"> | ||||||
|  |         <label for="node-input-server"><i class="icon-tag"></i> Server</label> | ||||||
|  |         <input type="text" id="node-input-server" placeholder="Server"> | ||||||
|  |     </div> | ||||||
|  |     <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-room"><i class="icon-tag"></i> Room</label> | ||||||
|  |         <input type="text" id="node-input-room" placeholder="Room"> | ||||||
|  |     </div> | ||||||
|  |     <div class="form-row"> | ||||||
|  |         <label for="node-input-filterself"><i class="icon-tag"></i> Filter events sent by self</label> | ||||||
|  |         <input type="checkbox" id="node-input-filterself" placeholder="Filterself"> | ||||||
|  |     </div> | ||||||
|  | </script> | ||||||
|  | 
 | ||||||
|  | <script type="text/x-red" data-help-name="matrix-recvtext"> | ||||||
|  |     <p>Matrix receive text node</p> | ||||||
|  | </script> | ||||||
|  | 
 | ||||||
| <!-- OUTPUT Node "matrix-output" --> | <!-- OUTPUT Node "matrix-output" --> | ||||||
| 
 | 
 | ||||||
| <script type="text/javascript"> | <script type="text/javascript"> | ||||||
|  | @ -155,3 +199,47 @@ | ||||||
|     <p>Matrix output node</p> |     <p>Matrix output node</p> | ||||||
| </script> | </script> | ||||||
| 
 | 
 | ||||||
|  | <!-- OUTPUT Node "matrix-sendtext" --> | ||||||
|  | 
 | ||||||
|  | <script type="text/javascript"> | ||||||
|  |     RED.nodes.registerType('matrix-sendtext',{ | ||||||
|  |         category: 'function', | ||||||
|  |         color: '#a6bbcf', | ||||||
|  |         defaults: { | ||||||
|  |             name: {value:""}, | ||||||
|  |             room: {value:""}, | ||||||
|  |             server: {value:"", type:"matrix-server"}, | ||||||
|  |             notice: {value:true} | ||||||
|  |         }, | ||||||
|  |         inputs:1, | ||||||
|  |         outputs:0, | ||||||
|  |         icon: "matrix.png", | ||||||
|  |         label: function() { | ||||||
|  |             return this.name||"matrix-sendtext"; | ||||||
|  |         } | ||||||
|  |     }); | ||||||
|  | </script> | ||||||
|  | 
 | ||||||
|  | <script type="text/x-red" data-template-name="matrix-sendtext"> | ||||||
|  |     <div class="form-row"> | ||||||
|  |         <label for="node-input-server"><i class="icon-tag"></i> Server</label> | ||||||
|  |         <input type="text" id="node-input-server" placeholder="Server"> | ||||||
|  |     </div> | ||||||
|  |     <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-room"><i class="icon-tag"></i> Room</label> | ||||||
|  |         <input type="text" id="node-input-room" placeholder="Room"> | ||||||
|  |     </div> | ||||||
|  |     <div class="form-row"> | ||||||
|  |         <label for="node-input-notice"><i class="icon-tag"></i> Send Text as m.notice (silent)</label> | ||||||
|  |         <input type="checkbox" id="node-input-notice" placeholder="Notice"> | ||||||
|  |     </div> | ||||||
|  | </script> | ||||||
|  | 
 | ||||||
|  | <script type="text/x-red" data-help-name="matrix-sendtext"> | ||||||
|  |     <p>Matrix send text node</p> | ||||||
|  | </script> | ||||||
|  | 
 | ||||||
|  |  | ||||||
							
								
								
									
										110
									
								
								matrix.js
									
										
									
									
									
								
							
							
						
						
									
										110
									
								
								matrix.js
									
										
									
									
									
								
							|  | @ -104,7 +104,7 @@ module.exports = function(RED) { | ||||||
|                     return; // ignore our own messages
 |                     return; // ignore our own messages
 | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 if (node.room && (node.room !== event.room)) { |                 if (node.room && (node.room !== room.roomId)) { | ||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|  | @ -124,6 +124,58 @@ module.exports = function(RED) { | ||||||
|     } |     } | ||||||
|     RED.nodes.registerType("matrix-input",MatrixInputNode); |     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) { |     function MatrixOutputNode(config) { | ||||||
|         RED.nodes.createNode(this,config); |         RED.nodes.createNode(this,config); | ||||||
|         var node = this; |         var node = this; | ||||||
|  | @ -172,4 +224,60 @@ module.exports = function(RED) { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     RED.nodes.registerType("matrix-output", MatrixOutputNode); |     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); | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue