- New option for matrix-send-message node that allows replacing existing message if enabled and `msg.eventId` is passed in.
This commit is contained in:
Skylar Sadlier 2022-03-17 15:37:10 -06:00
parent 27dd4d81a2
commit 380e548425
2 changed files with 40 additions and 1 deletions

View File

@ -12,6 +12,7 @@
roomId: { value: null }, roomId: { value: null },
messageType: { value: 'm.text' }, messageType: { value: 'm.text' },
messageFormat: { value: '' }, messageFormat: { value: '' },
replaceMessage : { value: false }
}, },
label: function() { label: function() {
return this.name || "Send Message"; return this.name || "Send Message";
@ -60,6 +61,17 @@
<option value="msg.format">msg.format input</option> <option value="msg.format">msg.format input</option>
</select> </select>
</div> </div>
<div class="form-row">
<input
type="checkbox"
id="node-input-replaceMessage"
style="width: auto; margin-left: 105px; vertical-align: top"
/>
<label for="node-input-replaceMessage" style="width: auto;max-width:50%;">
Update existing message if <code>msg.eventId</code> is set
</label>
</div>
</script> </script>
<script type="text/html" data-help-name="matrix-send-message"> <script type="text/html" data-help-name="matrix-send-message">
@ -78,10 +90,15 @@
</dt> </dt>
<dd> the message text. </dd> <dd> the message text. </dd>
<dt>msg.replace
<span class="property-type">bool</span>
</dt>
<dd> If true and <code>msg.eventId</code> is present it will update an existing message. Posts a new message if false or <code>msg.eventId</code> is missing. </dd>
<dt class="optional">msg.formatted_payload <dt class="optional">msg.formatted_payload
<span class="property-type">string</span> <span class="property-type">string</span>
</dt> </dt>
<dd> the formatted HTML message (uses msg.payload if not defined). This only affects HTML messages.</dd> <dd> the formatted HTML message (uses <code>msg.payload</code> if not defined). This only affects HTML messages.</dd>
<dt class="optional">msg.type <dt class="optional">msg.type
<span class="property-type">string | null</span> <span class="property-type">string | null</span>

View File

@ -1,3 +1,5 @@
const {RelationType} = require("matrix-js-sdk");
module.exports = function(RED) { module.exports = function(RED) {
function MatrixSendImage(n) { function MatrixSendImage(n) {
RED.nodes.createNode(this, n); RED.nodes.createNode(this, n);
@ -9,6 +11,7 @@ module.exports = function(RED) {
this.roomId = n.roomId; this.roomId = n.roomId;
this.messageType = n.messageType; this.messageType = n.messageType;
this.messageFormat = n.messageFormat; this.messageFormat = n.messageFormat;
this.replaceMessage = n.replaceMessage;
// taken from https://github.com/matrix-org/synapse/blob/master/synapse/push/mailer.py // taken from https://github.com/matrix-org/synapse/blob/master/synapse/push/mailer.py
this.allowedTags = [ this.allowedTags = [
@ -116,6 +119,25 @@ module.exports = function(RED) {
: msg.payload.toString(); : msg.payload.toString();
} }
if((node.replaceMessage || msg.replace) && msg.eventId) {
content['m.new_content'] = {
msgtype: content.msgtype,
body: content.body
};
if('format' in content) {
content['m.new_content']['format'] = content['format'];
}
if('formatted_body' in content) {
content['m.new_content']['formatted_body'] = content['formatted_body'];
}
content['m.relates_to'] = {
rel_type: RelationType.Replace,
event_id: msg.eventId
};
content['body'] = ' * ' + content['body'];
}
node.server.matrixClient.sendMessage(msg.topic, content) node.server.matrixClient.sendMessage(msg.topic, content)
.then(function(e) { .then(function(e) {
node.log("Message sent: " + msg.payload); node.log("Message sent: " + msg.payload);