-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
127 lines (111 loc) · 4.16 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const NodeHelper = require('node_helper');
const pyScriptPath = __dirname + "/python";
let { PythonShell } = require('python-shell')
var oneLinerJoke = require('one-liner-joke');
var wd = require("word-definition");
module.exports = NodeHelper.create({
answer: "none",
doneWithPython: "false",
customCommands: ["joke", 'wikipedia', 'time', 'date'],
start() {
console.log(`Starting node helper for: ${this.name}`);
},
capitalize: function(word) {
const lower = word.toLowerCase();
return word.charAt(0).toUpperCase() + lower.slice(1);
},
getTime: function() {
var today = new Date();
if (today.getMinutes() < 10) {
var time = today.getHours() + ":0" + (today.getMinutes());
} else {
var time = today.getHours() + ":" + (today.getMinutes());
}
var weekday = today.getDay();
switch (weekday) {
case 0:
weekday = "Sunday";
break;
case 1:
weekday = "Monday";
break;
case 2:
weekday = "Tuesday";
break;
case 3:
weekday = "Wednesday";
break;
case 4:
weekday = "Thursday";
break;
case 5:
weekday = "Friday";
break;
case 6:
weekday = "Saturday";
break;
}
var finalDate = time + " on a " + weekday;
return finalDate;
},
getDate: function() {
var date;
//Get User Time
var today = new Date();
if (today.getMonth() + 1 < 10 && today.getDate() < 10) {
date = today.getFullYear() + '-0' + (today.getMonth() + 1) + '-0' + today.getDate();
} else if (today.getMonth() + 1 < 10) {
date = today.getFullYear() + '-0' + (today.getMonth() + 1) + '-' + today.getDate();
} else if (today.getDate() < 10) {
date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-0' + today.getDate();
} else {
date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
}
finalDate = date;
return finalDate;
},
runPython: async function(payload, self) {
var options = {
mode: 'text',
pythonPath: __dirname + "/python/miniconda3/bin/python",
pythonOptions: ['-u'],
scriptPath: pyScriptPath,
args: [payload]
};
await PythonShell.run('askBot.py', options, function(err, results) {
if (err) throw err;
self.answer = results[0];
if (self.customCommands.includes(self.answer)) {
if (self.answer == "joke") {
var joke = oneLinerJoke.getRandomJoke();
self.sendSocketNotification('ANSWER', joke.body);
} else if (self.answer == "wikipedia") {
wd.getDef(results[1], "en", null, function(definition) {
if (definition.err) {
self.sendSocketNotification('ANSWER', 'Could not find that word sorry.');
} else {
var word = self.capitalize(results[1]);
self.sendSocketNotification('ANSWER', word + ": " + definition.definition);
}
});
} else if (self.answer == "time") {
var time = self.getTime();
self.sendSocketNotification('ANSWER', time);
} else if (self.answer == "date") {
var date = self.getDate();
self.sendSocketNotification('ANSWER', date);
}
} else {
self.sendSocketNotification('ANSWER', self.answer);
}
});
},
socketNotificationReceived(notification, payload) {
if (notification === 'CONFIG') {
this.config = payload;
} else if (notification === 'GET_ANSWER') {
var self = this;
this.runPython(payload, self);
}
}
});