forked from jorgeluismorales/whatsapp-web-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessMessages.js
42 lines (36 loc) · 1.28 KB
/
processMessages.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
const Keyv = require("keyv");
const data = require("./data");
const printScreen = require("./printScreen");
const ttl = 60 * 60 * 1000;
const users = new Keyv();
const processMessages = function(client) {
client.onMessage(async message => {
let input = message.body.toLowerCase();
let currentUser = await users.get(message.from);
if (!currentUser) {
await users.set(message.from, ["root"], ttl);
printScreen(client, message, data["root"]);
} else {
const currentScreen = currentUser.reduce((object, current) => {
if (current === "root") {
return object[current];
} else {
return object["childrens"][current];
}
}, data);
if (Object.keys(currentScreen.childrens).includes(input)) {
if (currentScreen.childrens[input].type !== "data") {
currentUser.push(input);
await users.set(message.from, currentUser, ttl);
} else {
await users.delete(message.from);
}
printScreen(client, message, currentScreen.childrens[input]);
} else if (currentScreen.type === "submenu" && input === "x") {
await users.set(message.from, ["root"], ttl);
printScreen(client, message, data["root"]);
}
}
});
};
module.exports = processMessages;