-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
219 lines (191 loc) · 6.57 KB
/
chat.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
const chat = {
1: {
text: 'List your disaster',
options: [
{
text: 'Earthquake',
next: 2
},
{
text: 'Cyclone',
next: 7
},
{
text: 'Landslide',
next: 9
},
{
text: 'Flood',
next: 8
},
{
text: 'Fire',
next: 11
},
{
text: 'Avalanche',
next: 12
},
]
},
2: { //Earthquake manager
text: 'Move away from the windows and outside walls, try to cover your head under a strong table.',
next: 3
},
3: {
text: 'Emergency services have your location details, someone will come help you',
options: [
{
text: "i want to call services",
next: 4
},
{
text: "Show me area map",
next: 6
}
]
},
4: {
text: 'Disaster management - 108\n Emergency service - 112 \n Fire - 101 \n N.D.M.A. - 011-24363260',
next: 5
},
5: {
text: 'Want more help?',
options: [
{
text: "Back to Disaster list",
next: 1
},
{
text: 'No, thank you',
next: 10
}
]
},
6: {
text: 'Navigate to your location',
options: [
{
text: "Go to Maps",
url: "https://www.google.com/maps"
}
]
},
7: { //Cyclone
text: 'You should remain in shelters until informed by those in charge that you may return home. Any loose and dangling wire from the lamp post should be strictly avoided. People should keep away from disaster areas unless you are required to assist.',
next : 3
},
8: { //Flood
text: 'Do not enter deep, unknown waters; use a stick to check water depth. Come back home only when officials ask you to do so. Make a family communications plan. Clean and disinfect everything that got wet.',
next : 3
},
9: { //Landslide
text: 'Try and get out of the path of the landslide or mudflow. Run to the nearest high ground in a direction away from the path. If rocks and other debris are approaching, run for the nearest shelter such as a group of trees or a building. If escape is not possible, curl into a tight ball and protect your head.',
next : 3
},
11: { //Fire
text: 'Evacuate calmly and quickly whenever a fire alarm or carbon monoxide alarm sounds.Keep important items such as medications and medical equipment handy for quick access in the event of a building evacuation.',
next : 3
},
12: { //Avalanche
text: 'Get proper equipment to protect yourself from head injuries and create air pockets. Receive first aid training so you can recognize and treat suffocation, hypothermia, traumatic injury and shock. Wear a helmet to help reduce head injuries and create air pockets. Wear an avalanche beacon to help rescuers locate you.',
next : 3
},
10: {
text: 'You will be saved. Don\'t worry'
},
};
const bot = function () {
const peekobot = document.getElementById('peekobot');
const container = document.getElementById('peekobot-container');
const inner = document.getElementById('peekobot-inner');
let restartButton = null;
const sleep = function (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
const scrollContainer = function () {
inner.scrollTop = inner.scrollHeight;
};
const insertNewChatItem = function (elem) {
//container.insertBefore(elem, peekobot);
peekobot.appendChild(elem);
scrollContainer();
//debugger;
elem.classList.add('activated');
};
const printResponse = async function (step) {
const response = document.createElement('div');
response.classList.add('chat-response');
response.innerHTML = step.text;
insertNewChatItem(response);
await sleep(1500);
if (step.options) {
const choices = document.createElement('div');
choices.classList.add('choices');
step.options.forEach(function (option) {
const button = document.createElement(option.url ? 'a' : 'button');
button.classList.add('choice');
button.innerHTML = option.text;
if (option.url) {
button.href = option.url;
} else {
button.dataset.next = option.next;
}
choices.appendChild(button);
});
insertNewChatItem(choices);
} else if (step.next) {
printResponse(chat[step.next]);
}
};
const printChoice = function (choice) {
const choiceElem = document.createElement('div');
choiceElem.classList.add('chat-ask');
choiceElem.innerHTML = choice.innerHTML;
insertNewChatItem(choiceElem);
};
const disableAllChoices = function () {
const choices = document.querySelectorAll('.choice');
choices.forEach(function (choice) {
choice.disabled = 'disabled';
});
return;
};
const handleChoice = async function (e) {
if (!e.target.classList.contains('choice') || 'A' === e.target.tagName) {
// Target isn't a button, but could be a child of a button.
var button = e.target.closest('#peekobot-container .choice');
if (button !== null) {
button.click();
}
return;
}
e.preventDefault();
const choice = e.target;
disableAllChoices();
printChoice(choice);
scrollContainer();
await sleep(1500);
if (choice.dataset.next) {
printResponse(chat[choice.dataset.next]);
}
// Need to disable buttons here to prevent multiple choices
};
const handleRestart = function () {
startConversation();
}
const startConversation = function () {
printResponse(chat[1]);
}
const init = function () {
container.addEventListener('click', handleChoice);
restartButton = document.createElement('button');
restartButton.innerText = "Restart";
restartButton.classList.add('restart');
restartButton.addEventListener('click', handleRestart);
container.appendChild(restartButton);
startConversation();
};
init();
}
bot();