-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.php
322 lines (279 loc) · 9.13 KB
/
bot.php
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
if (isset($_GET['botId'])) {
$server = "db4free.net";
$username = "scratchbot";
$password = "qaz123wsx";
$dbname = "scratchbot";
// Create connection
$conn = new mysqli($server, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT content, user_id FROM `blocks` WHERE botId=".$_GET['botId'];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$xml = $row["content"];
$xml = substr($xml,0,5)."id=\"startBlocks\" style=\"display: none\"".substr($xml,5);
echo $xml;
$user_id = $row["user_id"];
}
} else {
header("Location: /");
}
$conn->close();
} else {
header("Location: /");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>ScratchBot</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
</head>
<body>
<script src="blockly/blockly_compressed.js"></script>
<script src="blockly/blocks_compressed.js"></script>
<script src="blockly/msg/js/en.js"></script>
<script src="blockly/javascript_compressed.js"></script>
<link rel="stylesheet" type="text/css" href="chatbox-media/chatbox_style.css">
<div class="col-sm-8" style="display:none;">
<div id="blocklyDiv" style="height:500px;width:100%;"></div>
</br>
<button onclick="saveXml()">Save</button>
<button onclick="resetWorkspeace()">Reset</button>
<button onclick="showCode()">Show JavaScript</button>
<p id="codeViewer"></p>
</div>
<style type="text/css">
.chatbox{
width: 400px;
min-width: 400px;
max-width: 400px;
height: 530px;
min-height: 530px;
max-height: 530px;
margin: auto;
}
</style>
<div class="chatbox">
<div class="chatcontent">
<div class="content owner">
<div class="user-photo"><img src="./chatbox-media/food.png"></div>
<p class="message">Welcome!</p>
</div>
</div>
<div class="chat-input">
<textarea id="chatbox-user-input" onkeydown="pressed(event)" placeholder="Please type your input."></textarea>
<button id="chatbox-send" onclick="sendMsg()">SEND</button>
</div>
</div>
<?php include 'define_blocks.php';?>
<script>
// global var
var orderName;
var orderAddress;
var orderFood;
var orderType;
var orderPhone;
var orderEmail;
var totalStep = 0;
var currentStep = 0;
var exitStep = -1;
// initial
var workspace = Blockly.inject('blocklyDiv',
{media: 'media/',
toolbox: document.getElementById('toolbox')});
Blockly.Xml.domToWorkspace(document.getElementById('startBlocks'),workspace);
function showCode() {
// Generate JavaScript code and display it.
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
var code = Blockly.JavaScript.workspaceToCode(workspace);
document.getElementById("codeViewer").innerHTML = code;
}
function runCode(input) {
// Generate JavaScript code and run it.
window.LoopTrap = 1000;
Blockly.JavaScript.INFINITE_LOOP_TRAP =
'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
var runCode = Blockly.JavaScript.workspaceToCode(workspace);
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
try {
//get user input
var userInput = input;
eval(runCode);
exitStep = -1;
} catch (e) {
console.log(e);
}
totalStep = 0;
currentStep = 0;
}
function saveXml() {
var xml = Blockly.Xml.workspaceToDom(workspace);
console.log(xml);
var xmlText = new XMLSerializer().serializeToString(xml);
//var xmlTextNode = document.createTextNode(xmlText);
$.ajax({
url: "db_save_xml.php",
type: "POST",
data: ({"xml":xmlText}),
success: function(data) {
alert("Saved");
},
});
// Show to homepage
// var parentDiv = document.getElementById('xml_saved');
// parentDiv.appendChild(xmlTextNode);
}
function resetWorkspeace() {
Blockly.mainWorkspace.clear();
}
</script>
<script>
var jsonmenu;
var fullmenu = "";
var restaurantName;
var restaurantPhone;
var restaurantAddress;
function sendMsg(){
/*Start of User Input*/
var userInput = document.getElementById('chatbox-user-input').value;
document.getElementById('chatbox-user-input').value = "";
var userdiv = document.createElement("div");
userdiv.setAttribute("class", "content customer");
var userinnerdiv = document.createElement("div");
userinnerdiv.setAttribute("class", "user-photo");
var userimg = document.createElement("img");
userimg.setAttribute("src", "./chatbox-media/you.png");
var userp = document.createElement("p");
userp.setAttribute("class", "message");
userp.innerHTML = userInput;
document.getElementsByClassName("chatcontent")[0].appendChild(userdiv);
document.getElementsByClassName("chatcontent")[0].lastElementChild.appendChild(userinnerdiv);
document.getElementsByClassName("chatcontent")[0].lastElementChild.getElementsByClassName("user-photo")[0].appendChild(userimg);
document.getElementsByClassName("chatcontent")[0].lastElementChild.appendChild(userp);
var contentbox = document.getElementsByClassName("chatcontent")[0];
contentbox.scrollTop = contentbox.scrollHeight;
/*End of User Input*/
/*Start of Bot Reply*/
runCode(userInput);
/*End of Bot Reply*/
}
function botReply(replyMsg){
var botdiv = document.createElement("div");
botdiv.setAttribute("class", "content owner");
var botinnerdiv = document.createElement("div");
botinnerdiv.setAttribute("class", "user-photo");
var botimg = document.createElement("img");
botimg.setAttribute("src", "./chatbox-media/food.png");
var botp = document.createElement("p");
botp.setAttribute("class", "message");
botp.innerHTML = replyMsg;/*INSERT BOT REPLY VALUE HERE!*/
// delay reply
setTimeout(function(){
document.getElementsByClassName("chatcontent")[0].appendChild(botdiv);
document.getElementsByClassName("chatcontent")[0].lastElementChild.appendChild(botinnerdiv);
document.getElementsByClassName("chatcontent")[0].lastElementChild.getElementsByClassName("user-photo")[0].appendChild(botimg);
document.getElementsByClassName("chatcontent")[0].lastElementChild.appendChild(botp);
var contentbox = document.getElementsByClassName("chatcontent")[0];
contentbox.scrollTop = contentbox.scrollHeight;
}, 1000);
}
function pressed(e) {
if (e.keyCode == 13 && !e.shiftKey) {
sendMsg();
// prevent default behavior
e.preventDefault();
}
}
function isAnswerPositive(answer){
return true;
}
function isAnswerNegative(answer){
return false;
}
function sendOrder(){
orderDetail = "";
orderDetail += "Type: "+orderType+"<br>";
orderDetail += "Food ordered: "+getFoodNameByList(orderFood)+"<br>";
orderDetail += "Name: "+orderName+"<br>";
orderDetail += "Address: "+orderAddress;
botReply(orderDetail);
}
function getFoodNameByList(orderFood){
orderFoodList = orderFood.split(",");
orderFoodString = "";
for (var i = 0; i < orderFoodList.length; i++){
if((orderFoodList[i]-1)< jsonmenu.length){
orderFoodString += "[";
orderFoodString += jsonmenu[orderFoodList[i]-1].name;
orderFoodString += "] ";
}
}
return orderFoodString;
}
function saveOrder(){
console.log("saving");
$.ajax({
url: "db_save_order.php",
type: "POST",
data: {"orderType":orderType,
"orderFood":getFoodNameByList(orderFood),
"orderName":orderName,
"orderPhone":orderPhone,
"orderEmail":orderEmail,
"orderAddress":orderAddress
},
success: function(data) {
alert("Saved");
},
});
}
window.onload = function getMenu(){
$.ajax({
url: "db_get_menu.php",
type: "POST",
data: {"user_id":<?php echo $user_id ?>},
success: function(data) {
obj = JSON.parse(data);
jsonmenu = JSON.parse(data);
for (var i = 0; i < obj.length; i++) {
fullmenu += i+1;
fullmenu += " : ";
fullmenu += obj[i].name;
fullmenu += " , ";
fullmenu += obj[i].price;
fullmenu += "<br>";
}
console.log(fullmenu);
},
error: function(){
alert("fail to get menu");
}
});
$.ajax({
url: "db_get_restaurant.php",
type: "POST",
data: {"user_id":<?php echo $user_id ?>},
success: function(data) {
restaurantName = data.split(",")[0];
restaurantPhone = data.split(",")[1];
restaurantAddress = data.split(",")[2];
console.log(data);
},
error: function(){
alert("fail to get restaurant info");
}
});
}
</script>
<script>
</script>
<?php include 'footer.php';?>