Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·223 lines (210 loc) · 6.79 KB
/
Copy pathserver.js
File metadata and controls
executable file
·223 lines (210 loc) · 6.79 KB
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
'use strict';
//Using Restify service we are creating a server to communicate with FB Messenger.
const Restify = require('restify');
const server = Restify.createServer({
name:'Test'
});
//Modules are being imported. Each module contains questions based on their topic.
const kinematics = require('./kinematics');
const dynamics = require('./dynamics');
const claw = require('./conservation_laws');
let sample = kinematics;
//Some constant messages which occur frequently.
const WELCOME_MESSAGE="Hi There! I'm Physics Bot. I can help you improve in Physics. Just follow the instructions and answer the questions :D"
const HAPPY_MESSAGE="Correct!! :D :D"
const SAD_MESSAGE="Sorry :( The answer is: "
//Importing jsonp and bodyParser to convert message object of FB from string to JSON Object
server.use(Restify.jsonp());
server.use(Restify.bodyParser());
//Dedicating a port in the local server.
const PORT = process.env.PORT || 3000;
const config = require('./config');
//A class has been created so that the server and SendAPI of Messenger communicate with ease.
const FBeamer = require('./fbeamer');
const f = new FBeamer(config);
//Registering the server with Webhook.
server.get('/',(req,res,next)=>{
f.registerHook(req,res);
return next();
});
//Some constants required by the Bot.
let correctans=0,wrongans=0,increment=0;
let welcome=0,ans=0,ind=0,hintcount=0;
let topics =[{
content_type:"text",
title: "Kinematics",
payload: "0"
},{
content_type:"text",
title: "Dynamics",
payload: "1"
},{
content_type:"text",
title: "Conservation Laws",
payload: "2"
}];
let choice = [{
content_type:"text",
title: "Yes",
payload: "100"
},{
content_type:"text",
title: "No",
payload: "101"
}]
//Post method of server to send messages as the bot.
server.post('/',(req,res,next)=>{
f.incoming(req,res,msg => {
//console.log("welcome flag: "+welcome);
//Typing feel of FB Messenger(Kindof loading bar)
f.sender_action(msg.sender,"typing_on",function(data){
return null;
})
//Increases/Decreases the difficulty of quesitons.
if(increment==-1){
increment++;
}else if(increment==3){
increment--;
}
//Welcome message.
if(welcome==0){
welcome=1;
f.txt(msg.sender,WELCOME_MESSAGE,function(data){
f.quickreply(msg.sender,"Choose your topic.",topics,function(data){
return null;
})
})
}
//Topic selection and question generation.
else if(welcome==1&&ans==0){
let n=parseInt(sample.length/3);
if(msg.message.quick_reply!=undefined&&msg.message.quick_reply.payload==0){
//console.log("kine");
sample = kinematics;
ind=parseInt((Math.random()*n)+(increment*n));
ans=1;
f.txt(msg.sender,sample[ind].qtext,function(data){
return null;
})
}else if(msg.message.quick_reply!=undefined&&msg.message.quick_reply.payload==1){
//console.log("dyn");
sample = dynamics;
//console.log(sample);
ind=parseInt((Math.random()*n)+(increment*n));
ans=1;
f.txt(msg.sender,sample[ind].qtext,function(data){
return null;
})
}else if(msg.message.quick_reply!=undefined&&msg.message.quick_reply.payload==2){
//console.log("claw");
sample = claw;
ind=parseInt((Math.random()*n)+(increment*n));
ans=1;
f.txt(msg.sender,sample[ind].qtext,function(data){
return null;
})
}else{
f.txt(msg.sender,"Sorry I didn't quite get your message. Please choose from the options.",function(data){
f.quickreply(msg.sender,"Choose your topic.",topics,function(data){
return null;
})
})
}
}
//Awaiting response and providing hints.
else if(welcome==1&&ans==1){
if(msg.message.text=="hint"){
f.txt(msg.sender,sample[ind].hint,function(data){
return null;
})
}
let temp = parseFloat(msg.message.text);
//If answered correctly increase difficulty.
if(temp==sample[ind].ans){
//welcome=0;ans=0;
f.txt(msg.sender,HAPPY_MESSAGE,function(data){
f.txt(msg.sender,"You can go to "+sample[ind].ref+" for further reference :)",function(data){
correctans++;
if(correctans==1){
correctans=0;
f.txt(msg.sender,"Increasing difficulty 3:-) ",function(data){
increment++;
if(increment==3){
f.txt(msg.sender,"You have reached max difficulty :D :D . You might want to change to another topic :D",function(data){
f.quickreply(msg.sender,"Would you like to continue??",choice,function(data){
return null;
})
})
}else{
f.quickreply(msg.sender,"Would you like to continue??",choice,function(data){
return null;
})
}
})
}else{
f.quickreply(msg.sender,"Would you like to continue??",choice,function(data){
return null;
})
}
})
})
}
//Continue Yes or No.
else if(msg.message.quick_reply!=undefined&&msg.message.quick_reply.payload==100){
ans=0;
f.quickreply(msg.sender,"Choose your topic.",topics,function(data){
return null;
})
}else if(msg.message.quick_reply!=undefined&&msg.message.quick_reply.payload==101){
welcome=0;ans=0;correctans=0;wrongans=0;increment=0;
f.txt(msg.sender,"Sorry to see you leave :( . All the best in your physics exam :D :D",function(data){
return null;
})
}
//If wrong answer received,it provides 2 chances with hint and finally decreases difficulty if wrong
//answer is received yet again.
else{
if(hintcount<2){
hintcount++;
f.txt(msg.sender,"Please try again",function(data){
f.txt(msg.sender,"Hint: "+sample[ind].hint,function(data){
return null;
})
})
}else{
hintcount=0;
f.txt(msg.sender,SAD_MESSAGE+sample[ind].ans,function(data){
f.txt(msg.sender,"You can go to "+sample[ind].ref+" for further reference :)",function(data){
wrongans++;
if(wrongans==1){
wrongans=0;
f.txt(msg.sender,"Decreasing difficulty :( ",function(data){
increment--;
if(increment==-1){
f.txt(msg.sender,"You need to brush up your concepts :( ",function(data){
f.quickreply(msg.sender,"Would you like to continue??",choice,function(data){
return null;
})
})
}else{
f.quickreply(msg.sender,"Would you like to continue??",choice,function(data){
return null;
})
}
})
}else{
f.quickreply(msg.sender,"Would you like to continue??",choice,function(data){
return null;
})
}
})
})
}
}
}
})
})
//Subscribing to the page.
f.subscribe();
//Server listening in Port#****
server.listen(PORT,() => console.log(`Running on PORT: ${PORT}`));