diff --git a/README.md b/README.md index f70d2ac..399e022 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,11 @@ You can either install from within Node-RED by searching for `node-red-contrib-m npm install node-red-contrib-matrix-chat ``` +### Extra functionality +You are not limited by just the nodes we have created. If you turn on global access when setting up your Matrix Client you can access the client directly from any function node to write your own logic. + +View an example [here](https://github.com/Skylar-Tech/node-red-contrib-matrix-chat/tree/master/examples#use-function-node-to-run-any-command) + ### End-to-End Encryption Notes Currently this module has no way of getting encryption keys from other devices on the same account. Therefore it is recommended you use the bot exclusively with Node-RED after it's creation. Failure to do so will lead to your bot being unable to receive messages from e2ee rooms it joined from another client. Shared secret registration makes this super easy since it returns a token and device ID. diff --git a/examples/README.md b/examples/README.md index 8c1dae2..b1b51b7 100644 --- a/examples/README.md +++ b/examples/README.md @@ -9,6 +9,7 @@ Build something cool with these nodes? Feel free to submit a pull request to sha - [Create User with Shared Secret Registration](#create-user-with-shared-secret-registration) - [Create/Edit Synapse User](#createedit-synapse-user) +- [Use function node to run any command](#use-function-node-to-run-any-command) - [Respond to "ping" with "pong"](#respond-to-ping-with-pong) - [Respond to "html" with an HTML message](#respond-to-html-with-an-html-message) - [Respond to "image" with an uploaded image](#respond-to-image-with-an-uploaded-image) @@ -39,6 +40,18 @@ Allows an administrator to create or modify a user account with a specified `msg ![img.png](add-user-with-admin-user.png) +### Use function node to run any command + +[View JSON](acustom-redact-function-node.json) + +If we do not have a node for something you want to do (such as redacting events/messages) you can do this manually with a function node. + +**Note:** You should make sure to catch any errors in your function node otherwise you could cause Node-RED to crash. + +To view what sort of functions you have access to check out the `client.ts` file from `matrix-js-sdk` [here](https://github.com/matrix-org/matrix-js-sdk/blob/master/src/client.ts). + +![img.png](custom-redact-function-node.png) + ### Respond to "ping" with "pong" [View JSON](respond-to-html-with-html.json) diff --git a/examples/custom-redact-function-node.json b/examples/custom-redact-function-node.json new file mode 100644 index 0000000..a4af92d --- /dev/null +++ b/examples/custom-redact-function-node.json @@ -0,0 +1,113 @@ +[ + { + "id": "89eed7ddf7a96070", + "type": "function", + "z": "f025a8b9fbd1b054", + "name": "Redact Message", + "func": "let matrixClient = global.get(\"matrixClient['@bot:example.com']\"),\n matrixOnline = global.get(\"matrixClientOnline['@bot:example.com']\");\n\nif(!matrixOnline) {\n msg.error = 'Matrix client not connected.';\n return [null, msg];\n}\n \nmatrixClient.redactEvent(msg.topic, msg.eventId, undefined, {\n reason: 'Redacted, muhahaha!'\n})\n .then(function(e) {\n msg.eventId = e.eventId;\n node.send([msg, null]);\n })\n .catch(function(e){\n node.warn(\"Error sending message \" + e);\n msg.error = e;\n node.send([null, msg]);\n });", + "outputs": 2, + "noerr": 0, + "initialize": "", + "finalize": "", + "libs": [], + "x": 730, + "y": 1580, + "wires": [ + [ + "5decd492e2da79d9" + ], + [ + "fadcf8c32bdfd8be" + ] + ] + }, + { + "id": "5decd492e2da79d9", + "type": "debug", + "z": "f025a8b9fbd1b054", + "name": "", + "active": true, + "tosidebar": true, + "console": false, + "tostatus": false, + "complete": "true", + "targetType": "full", + "statusVal": "", + "statusType": "auto", + "x": 890, + "y": 1560, + "wires": [] + }, + { + "id": "f3636f4221511a81", + "type": "comment", + "z": "f025a8b9fbd1b054", + "name": "If message is \"redact\" then redact the message.", + "info": "", + "x": 420, + "y": 1540, + "wires": [] + }, + { + "id": "9f777828840bb9a2", + "type": "matrix-receive", + "z": "f025a8b9fbd1b054", + "name": "", + "roomId": "", + "acceptText": true, + "acceptEmotes": false, + "acceptStickers": false, + "acceptReactions": false, + "acceptFiles": false, + "acceptImages": false, + "x": 320, + "y": 1580, + "wires": [ + [ + "21a00b09f8c85399" + ] + ] + }, + { + "id": "21a00b09f8c85399", + "type": "switch", + "z": "f025a8b9fbd1b054", + "name": "filter on msg.payload", + "property": "payload", + "propertyType": "msg", + "rules": [ + { + "t": "eq", + "v": "redact", + "vt": "str" + } + ], + "checkall": "true", + "repair": false, + "outputs": 1, + "x": 520, + "y": 1580, + "wires": [ + [ + "89eed7ddf7a96070" + ] + ] + }, + { + "id": "fadcf8c32bdfd8be", + "type": "debug", + "z": "f025a8b9fbd1b054", + "name": "", + "active": true, + "tosidebar": true, + "console": false, + "tostatus": false, + "complete": "true", + "targetType": "full", + "statusVal": "", + "statusType": "auto", + "x": 890, + "y": 1600, + "wires": [] + } +] \ No newline at end of file diff --git a/examples/custom-redact-function-node.png b/examples/custom-redact-function-node.png new file mode 100644 index 0000000..a5c1f30 Binary files /dev/null and b/examples/custom-redact-function-node.png differ diff --git a/src/matrix-server-config.html b/src/matrix-server-config.html index fc77518..1e6fffc 100644 --- a/src/matrix-server-config.html +++ b/src/matrix-server-config.html @@ -11,7 +11,8 @@ defaults: { name: { value: null }, autoAcceptRoomInvites: { value: true }, - enableE2ee: { type: "checkbox", value: true } + enableE2ee: { type: "checkbox", value: true }, + global: { type: "checkbox", value: true } }, icon: "matrix.png", label: function() { @@ -67,10 +68,24 @@ Enable end-to-end encryption (requires DeviceID) -
let client = global.get("matrixClient['@bot:example.com']")
+