-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
212 lines (191 loc) · 6.77 KB
/
app.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//Define modules
const TelegramBot = require('node-telegram-bot-api');
const fl = require('./lists/list_food').foodlist;
const resType = require('./lists/list_response_type');
const resNoProb = require('./lists/list_response_NoProb').responses;
//Import bot api token
const config = require('./config.json');
//Create a new polling bot instance
let bot = new TelegramBot(config.teleBotToken, {polling: true});
// Polling error watchdog
bot.on('polling_error', (error) => {
console.log(error);
});
//Thanks
bot.onText(/thank/i, (msg) => {
const arrayLength = resNoProb.length;
const arrayVal = Math.floor(Math.random() * (arrayLength + 1));
let responseText = resNoProb[arrayVal].response;
bot.sendMessage(msg.chat.id, responseText);
});
//Bot will send message containing food data when receiving text.
//Random
bot.onText(/(I hungry|eat what|what to eat)/i, (msg) => {
getRandomFood(function(finalName, finalType, finalCuisine){
let arrayLength = eval("resType." + finalType + ".length");
const arrayVal = Math.floor(Math.random() * (arrayLength + 1))
let responseText = eval("resType." + finalType)
bot.sendMessage(msg.chat.id, responseText[arrayVal].response + " " + finalName);
});
});
//Filtered food request
bot.onText(/(I want| I want to eat)/i, (msg) => {
console.log(msg.text);
let inputArray = msg.text.toLowerCase().split(/\s/); // Splits to [ 'i', 'want', 'to', 'eat', 'something', 'cheap' ]
//create new arrays
let identifierArray = [];
let functionArray = [];
for(item=0; item<inputArray.length; item++){
//type filter
if (inputArray[item] == "fastfood"){
functionArray.push("filterByType");
identifierArray.push("fast_food");
}
if (inputArray[item] == "restaurant"){
functionArray.push("filterByType");
identifierArray.push("casual_dining");
}
if ((inputArray[item] == "kopitiam") || (inputArray[item] == "hawker")){
functionArray.push("filterByType");
identifierArray.push("hawker");
}
//cuisine filter
if (inputArray[item] == "western"){
functionArray.push("filterByCuisine");
identifierArray.push("western");
}
if (inputArray[item] == "chinese"){
functionArray.push("filterByCuisine");
identifierArray.push("chinese");
}
if (inputArray[item] == "indian"){
functionArray.push("filterByCuisine");
identifierArray.push("indian");
}
if (inputArray[item] == "thai"){
functionArray.push("filterByCuisine");
identifierArray.push("thai");
}
if (inputArray[item] == "korean"){
functionArray.push("filterByCuisine");
identifierArray.push("korean");
}
if (inputArray[item] == "japanese"){
functionArray.push("filterByCuisine");
identifierArray.push("japanese");
}
if ((inputArray[item] == "vietnamese") || (inputArray[item] == "viet")){
functionArray.push("filterByCuisine");
identifierArray.push("vietnamese");
}
if (inputArray[item] == "malay"){
functionArray.push("filterByCuisine");
identifierArray.push("malay");
}
//price filter
if (inputArray[item] == "cheapest"){
functionArray.push("filterByPrice");
identifierArray.push("$");
}
if ((inputArray[item] == "cheaper") || (inputArray[item] == "cheap")){
functionArray.push("filterByPrice");
identifierArray.push("$$");
}
if ((inputArray[item] == "midrange") || (inputArray[item] == "mid")){
functionArray.push("filterByPrice");
identifierArray.push("$$$");
}
if (inputArray[item] == "highend"){
functionArray.push("filterByPrice");
identifierArray.push("$$$$");
}
if ((inputArray[item] == "expensive") || (inputArray[item] == "ex")){
functionArray.push("filterByPrice");
identifierArray.push("$$$$$");
}
}
processInput(identifierArray, functionArray, function(finalName, finalType, finalCuisine){
if (typeof finalType !== 'undefined'){
let arrayLength = eval("resType." + finalType + ".length");
const arrayVal = Math.floor(Math.random() * (arrayLength + 1))
let responseText = eval("resType." + finalType)
bot.sendMessage(msg.chat.id, responseText[arrayVal].response + " " + finalName);
} else {
bot.sendMessage(msg.chat.id, "Eh I can't find what you want leh. Ask again.");
}
});
});
// for each element in identifierArray
function processInput(identifierArray, functionArray, callback){
if (typeof identifierArray[0] !== 'undefined'){
const f0 = eval(functionArray[0] + "(identifierArray[0].toString())");
const random = Math.floor(Math.random() * (f0.length)) // get random from result
var finalName = f0[random].name;
var finalType = f0[random].type;
var finalCuisine = f0[random].cuisine;
if (typeof identifierArray[1] !== 'undefined'){
const f1 = eval(functionArray[1] + "(identifierArray[1].toString(), f0)");
const random = Math.floor(Math.random() * (f1.length)) // get random from result
var finalName = f1[random].name;
var finalType = f1[random].type;
var finalCuisine = f1[random].cuisine;
if (typeof identifierArray[2] !== 'undefined'){
const f2 = eval(functionArray[2] + "(identifierArray[2].toString(), f1)");
const random = Math.floor(Math.random() * (f2.length)) // get random from result
var finalName = f2[random].name;
var finalType = f2[random].type;
var finalCuisine = f2[random].cuisine;
}
}
}
return callback(finalName, finalType, finalCuisine);
}
//filter functions output filtered arrays
function filterByType(element, newArray){
if (newArray == null){
let filtered = fl.filter(function (el) {
return el.type == element;
});
return filtered;
} else {
let filtered = newArray.filter(function (el) {
return el.type == element;
});
return filtered;
}
}
function filterByCuisine(element, newArray){
if (newArray == null){
let filtered = fl.filter(function (el) {
return el.cuisine == element;
});
return filtered;
} else {
let filtered = newArray.filter(function (el) {
return el.cuisine == element;
});
return filtered;
}
}
function filterByPrice(element, newArray){
if (newArray == null){
let filtered = fl.filter(function (el) {
return el.price == element;
});
return filtered;
} else {
let filtered = newArray.filter(function (el) {
return el.price == element;
});
return filtered;
}
}
// Get random food data from DB "test" and return value from function call.
function getRandomFood(callback){
const randomFood = Math.floor(Math.random() * (fl.length))
const foodKey = fl[randomFood];
const finalName = foodKey.name;
const finalType = foodKey.type;
const finalCuisine = foodKey.cuisine;
return callback(finalName, finalType, finalCuisine);
}