-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
262 lines (225 loc) · 7.34 KB
/
index.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// import {log} from './log.js';
require("dotenv").config();
const path = require("path")
const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const port = process.env.PORT || 3000;
const { Server } = require("socket.io");
const io = new Server(server);
app.use(express.static("public"));
/* Commented the below code because express.static("public") is serving the index.html file.
app.get('/', (req,res) => {
res.sendFile("public/index-script.js")
});
*/
// Global variables.
let BotHelloMessage = `<b>
Hello Dear,</b>
`
let options = `<div>
Select <b>1</b> to Place an order.</br>
Select <b>97</b> to see current order.</br>
Select <b>98</b> to see order history.</br>
Select <b>99</b> to checkout order.</br>
Select <b>0</b> to cancel order.</div>
`
const menu = {
1: "Pizza",
2: "Shawarma",
3: "Rice and Chicken",
4: "Meat Pie",
5: "Fish Pepper Soup"
}
const order = [];
io.on("connection", async (socket)=>{
console.log(`client connected.`);
// Handle bot reply for empty order.
function emptyOrderRespnse(type){
socket.emit("empty order", `<p><b>You do not have ${type==97 && "any order yet."
|| type==98 && "order history."
|| type==99&& "any order to checkout."
|| type==0 && "any order to cancle."}</b><p/>`
);
socket.emit("empty order", options);
return
}
socket.emit("botHello", BotHelloMessage);
socket.emit("menuOptions", options);
socket.on("disconnect", async ()=>{
console.log("client disconnected.")
});
// Bot reply for placing order.
socket.on("1", async() =>{
socket.emit("menu", menu);
});
// Bot reply for current order.
socket.on("97", async() =>{
if(order.length == 0){
emptyOrderRespnse(97)
}
});
// Bot reply for order history.
socket.on("98", async() =>{
if(order.length == 0){
emptyOrderRespnse(98)
}
});
// Bot reply for checkout order.
socket.on("99", async() =>{
if(order.length == 0){
emptyOrderRespnse(99)
}
});
// Bot reply for checkout order.
socket.on("0", async() =>{
if(order.length == 0){
emptyOrderRespnse(0)
}
});
});
server.listen(port, ()=> {
console.log(`Server Running on port ${port}...`);
});
// const express = require("express");
// const session = require("express-session");
// const http = require("http");
// const { Server } = require("socket.io");
// const app = express();
// const server = http.createServer(app);
// const io = new Server(server);
// const fastFoods = {
// 2: "Item1",
// 3: "Item2",
// 4: "Item3",
// 5: "Item4",
// };
// const orderHistory = [];
// const sessionMiddleware = session({
// secret: "secret-key",
// resave: false,
// saveUninitialized: true,
// });
// app.use(express.static("public"));
// app.use(sessionMiddleware);
// app.get("/", async (req, res) => {
// try {
// res.sendFile(__dirname + "/index.html");
// } catch (err) {
// console.log(err);
// res.status(500).send("Error serving restaurant.html");
// }
// });
// io.use((socket, next) => {
// sessionMiddleware(socket.request, socket.request.res, next);
// });
// io.on("connection", (socket) => {
// console.log("User connected:", socket.id);
// const state = {
// userName: "",
// currentOrder: [],
// };
// const botMessage = async (message) => {
// console.log("Bot message received:", message);
// socket.emit("bot-message", message);
// };
// const userMessage = async (message) => {
// console.log("User message received:", message);
// try {
// if (!state.userName) {
// // Save the user's name and update the welcome message
// state.userName = message;
// await botMessage(
// `Welcome to the ChatBot, ${state.userName}! Place an order\n
//1. Typehere\n9
//9. Typehere\n
//98. Typehere\n97. Typehere\n0. Cancel order`
// );
// } else {
// switch (message) {
// case "1":
// // Generate the list of items dynamically
// const itemOptions = Object.keys(fastFoods)
// .map((key) => `${key}. ${fastFoods[key]}`)
// .join("\n");
// await botMessage(
// `Here is a list of items you can order:\n ${itemOptions} \nPlease select one by typing its number.`
// );
// break;
// case "2":
// case "3":
// case "4":
// case "5":
// // Parse the number from the user input and add the corresponding item to the current order
// const selectedIndex = parseInt(message);
// if (fastFoods.hasOwnProperty(selectedIndex)) {
// const selectedItem = fastFoods[selectedIndex];
// state.currentOrder.push(selectedItem);
// await botMessage(
// `${selectedItem} has been added to your order. Do you want to add more items to your order? Type numbers. If not, type 99 to checkout.`
// );
// } else {
// await botMessage("Invalid selection.");
// }
// break;
// case "99":
// if (state.currentOrder.length === 0) {
// await botMessage(
// "No order to place. Place an order\n1. See menu"
// );
// } else {
// orderHistory.push(state.currentOrder);
// await botMessage("Order placed");
// state.currentOrder = [];
// }
// break;
// case "98":
// if (orderHistory.length === 0) {
// await botMessage("No previous orders");
// } else {
// const orderHistoryString = orderHistory
// .map(
// (order, index) => `Order ${index + 1}. ${order.join(", ")}`
// )
// .join("\n");
// await botMessage(
// `Here are your previous orders:\n${orderHistoryString}`
// );
// }
// break;
// case "97":
// if (state.currentOrder.length === 0) {
// await botMessage("No current order");
// } else {
// const currentOrderString = state.currentOrder.join(", ");
// await botMessage(
// `Here is your current order:\n${currentOrderString}`
// );
// }
// break;
// case "0":
// if (state.currentOrder.length === 0) {
// await botMessage("No order to cancel");
// } else {
// state.currentOrder = [];
// await botMessage("Order canceled");
// }
// break;
// default:
// await botMessage("Invalid input");
// }
// }
// } catch (err) {
// console.log(err);
// await botMessage("An error occurred while processing your request.");
// }
// };
// socket.on("user-message", userMessage);
// socket.on("disconnect", () => {
// console.log("User disconnected:", socket.id);
// });
// });
// server.listen(3000, () => {
// console.log("Listening on http://localhost:3000");
// });