forked from echang15/MMM-Dad-Jokes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
47 lines (42 loc) · 863 Bytes
/
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
/*
* MagicMirror²
* Node Helper: MMM-Dad-Jokes
*
* By Eric Chang
* MIT Licensed
*/
const Log = require("logger");
const NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
start () {
Log.log("MMM-Dad-Jokes helper started");
},
async getJoke () {
const parent = this; // Save this object
try {
const response = await fetch(
"https://icanhazdadjoke.com",
{
method: "GET",
headers: {
Accept: "application/json",
"User-Agent":
"MMM-Dad-Jokes (https://github.com/brucetony/MMM-Dad-Jokes)"
}
}
);
const data = await response.json();
parent.sendSocketNotification(
"JOKE_RESULT",
data
);
} catch (err) {
Log.error(err);
}
},
socketNotificationReceived (notification, payload) {
if (notification === "GET_JOKE") {
this.getJoke(payload);
}
}
});