From 5decfb9fadc71f2099809aff69b4e45fb7e3330f Mon Sep 17 00:00:00 2001 From: Gero Gerke Date: Tue, 5 Oct 2021 20:03:56 +0200 Subject: [PATCH] Matrix Notifications --- server/notification-providers/matrix.js | 37 +++++++++++++++++++++++++ server/notification.js | 2 ++ src/components/notifications/Matrix.vue | 25 +++++++++++++++++ src/components/notifications/index.js | 4 ++- src/languages/en.js | 1 + 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 server/notification-providers/matrix.js create mode 100644 src/components/notifications/Matrix.vue diff --git a/server/notification-providers/matrix.js b/server/notification-providers/matrix.js new file mode 100644 index 000000000..4593be6a6 --- /dev/null +++ b/server/notification-providers/matrix.js @@ -0,0 +1,37 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); +const Crypto = require('crypto') + +class Matrix extends NotificationProvider { + + name = "matrix"; + + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + let okMsg = "Sent Successfully. "; + + const size = 20; + const randomString = Crypto + .randomBytes(size) + .toString('base64') + .slice(0, size); + + try { + let config = { + headers: { + "Authorization": `Bearer ${notification.accessToken}`, + } + }; + let data = { + "msgtype": "m.text", + "body": msg + }; + + await axios.put(`${notification.homeserverUrl}/_matrix/client/r0/rooms/${notification.internalRoomId}/send/m.room.message/${randomString}`, data, config) + return okMsg; + } catch (error) { + this.throwGeneralAxiosError(error); + } + } +} + +module.exports = Matrix; diff --git a/server/notification.js b/server/notification.js index 134472410..207e0a37f 100644 --- a/server/notification.js +++ b/server/notification.js @@ -5,6 +5,7 @@ const Gotify = require("./notification-providers/gotify"); const Line = require("./notification-providers/line"); const LunaSea = require("./notification-providers/lunasea"); const Mattermost = require("./notification-providers/mattermost"); +const Matrix = require("./notification-providers/matrix"); const Octopush = require("./notification-providers/octopush"); const Pushbullet = require("./notification-providers/pushbullet"); const Pushover = require("./notification-providers/pushover"); @@ -34,6 +35,7 @@ class Notification { new Line(), new LunaSea(), new Mattermost(), + new Matrix(), new Octopush(), new Pushbullet(), new Pushover(), diff --git a/src/components/notifications/Matrix.vue b/src/components/notifications/Matrix.vue new file mode 100644 index 000000000..12c7dad80 --- /dev/null +++ b/src/components/notifications/Matrix.vue @@ -0,0 +1,25 @@ + + + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index e377803e6..6dd9ed928 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -15,6 +15,7 @@ import Apprise from "./Apprise.vue"; import Pushbullet from "./Pushbullet.vue"; import Line from "./Line.vue"; import Mattermost from "./Mattermost.vue"; +import Matrix from "./Matrix.vue"; /** * Manage all notification form. @@ -38,7 +39,8 @@ const NotificationFormList = { "apprise": Apprise, "pushbullet": Pushbullet, "line": Line, - "mattermost": Mattermost + "mattermost": Mattermost, + "matrix": Matrix } export default NotificationFormList diff --git a/src/languages/en.js b/src/languages/en.js index 46298b015..ccc7290d6 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -195,4 +195,5 @@ export default { "pushbullet": "Pushbullet", "line": "Line Messenger", "mattermost": "Mattermost", + "matrix": "Matrix", };