-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
39 lines (36 loc) · 1.1 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
"use strict"
var NodeHelper = require("node_helper")
module.exports = NodeHelper.create({
socketNotificationReceived: function (noti, payload) {
switch (noti) {
case "INIT":
console.log("[JOKE] MMM-BlaguesAPI Version:", require('./package.json').version)
this.config = payload
this.getJoke();
setInterval(() => {
this.getJoke();
}, this.config.fetchInterval);
break
}
},
getJoke() {
//console.info("getJoke");
let apiUrl;
if (this.config.type === 'random') {
apiUrl = 'https://www.blagues-api.fr/api/random';
} else {
apiUrl = `https://www.blagues-api.fr/api/type/${this.config.type}/random`;
}
fetch(apiUrl, {
method: 'get',
headers: new Headers({
'Authorization': 'Bearer ' + `${this.config.blaguesApiToken}`,
'Content-Type': 'application/x-www-form-urlencoded'
})
}).then((response) => {
response.json().then((joke) => {
this.sendSocketNotification("JOKE", joke);
});
});
}
})