forked from kalliope-project/MMM-kalliope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
64 lines (54 loc) · 1.85 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* node helper */
const NodeHelper = require('node_helper');
const express = require('express');
/* This helper will expose a new route to MagicMirror.
The route is /ovos and will only accept a POST request with a JSON payload.
Only two notifications are supported:
- OVOS_SEND_MESSAGE
- OVOS_DELETE_MESSAGE
Payload examples:
- '{"notification":"OVOS_SEND_MESSAGE", "payload": "Listening"}'
- '{"notification":"OVOS_DELETE_MESSAGE", "payload": "Deleting"}'
*/
module.exports = NodeHelper.create({
start: function () {
var self = this;
self.config = {};
// Make sure the payload is a valid JSON.
self.expressApp.use(express.json());
// Create the new route
this.expressApp.post('/ovos', (req, res) => {
var notification = req.body.notification;
var payload = req.body.payload;
// Check if X-Api-Key header is part of the request.
if (!req.headers.hasOwnProperty('x-api-key')) {
res
.status(401)
.json({ status: false, error: 'x-api-key header is missing' });
}
if (notification && payload) {
if (self.config.hasOwnProperty('apiKey')) {
// Compare X-Api-Key and API key from configuration.
if (req.headers['x-api-key'] === self.config.apiKey) {
self.sendSocketNotification(notification, payload);
res.status(200).json({
status: true,
payload: payload,
notification: notification,
});
} else {
res.status(401).json({ status: false, error: 'unauthorized' });
}
}
} else {
res.status(400).json({ status: false, error: 'bad request' });
}
});
},
socketNotificationReceived: function (notification, payload) {
var self = this;
if (notification === 'INIT') {
self.config.apiKey = payload;
}
},
});