-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
193 lines (165 loc) · 4.81 KB
/
index.js
File metadata and controls
193 lines (165 loc) · 4.81 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
const express = require('express');
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 5000;
const https = require('https');
memory = {
questions: quizEntries,
threads: {}
};
var quizEntries = [
{ qid: 1, q: 'What is the capital of India?', a1: 'Mumbai', a2: 'Delhi', a3: 'Kolkata', a4: 'Chennai', c: 'a2' },
{ qid: 2, q: 'Who wrote Julius Caeser?', a1: 'William Shakespeare', a2: 'Omprakash Mishra', a3: 'Dhinchak Pooja', a4: 'Dinesh Yadav', c: 'a1' },
{ qid: 3, q: 'What is the curreny in Japan', a1: 'Yen', a2: 'Rupee', a3: 'Dollars', a4: 'Pounds', c: 'a1' }
];
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function getQues(ques) {
return {
raw: ques,
text: `${ques.q}\n\nA. ${ques.a1}\nB. ${ques.a2}\nC. ${ques.a3}\nD. ${ques.a4}`
}
}
const app = express();
app.use(bodyParser.json());
app.get('/stocks/quote/:symbol', (req, res) => {
const options = {
hostname: 'www1.nseindia.com',
port: 443,
path: '/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=' + req.params.symbol,
method: 'GET',
headers: {
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Host': 'www1.nseindia.com',
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
'X-Requested-With': 'XMLHttpRequest'
}
};
https.request(options, (httpsRes) => {
// console.log('statusCode:', httpsRes.statusCode);
// console.log('headers:', httpsRes.headers);
httpsRes.on('data', (d) => {
res.send(d);
}).on('error', (e) => {
console.error(e);
});
});
});
app.get('/', (req, res) => {
const date = new Date().toString();
res.send(`Hello Heroku App! The current time is ${date}`);
});
app.get('/stats', (req, res) => {
res.send({
memory: memory
});
});
app.post('/hook', (req, res) => {
console.log(req.body);
if (req.body.message) {
var message = req.body.message;
var chat_id = message.chat.id;
var user_name = (message.from.first_name !== '' ? message.from.first_name : 'Guest');
if (message.text) {
var text = message.text;
if (text.indexOf('/help') >= 0) {
res.send({
method: 'sendMessage',
chat_id: chat_id,
text:
'Welcome to the QuizBot.\n' +
'You can select the below options:\n\n' +
'/start - To start the quiz\n' +
'/stop - To stop the quiz\n' +
'/help - To get help regarding the quiz\n' +
'/score - To get your current score\n' +
'/current_question - To get the current question'
});
}
else if (text.indexOf('/start') >= 0) {
// Shuffle the questions
var user_ques = shuffle(quizEntries);
// Throw the first question to the user
var ques = getQues(user_ques[0]);
// Store the details against the user
// TODO: Just store the question ids in the array
memory.threads[chat_id] = {
user_name: user_name,
questions: user_ques,
score: 0,
current_ques: ques.raw
}
res.send({
method: 'sendMessage',
chat_id: chat_id,
text: `Hi ${user_name}, let\'s start with the quiz.\n\n${ques.text}`,
reply_markup: {
'keyboard': [['A', 'B'], ['C', 'D']],
'one_time_keyboard': true,
'resize_keyboard': true,
'remove_keyboard': true
}
});
}
else if (text.indexOf('/stop') >= 0) {
delete memory.threads[chat_id];
res.send({
method: 'sendMessage',
chat_id: chat_id,
text: 'Thank you for playing QuizBot. Hope you enjoyed.'
});
}
else if (text.indexOf('/score') >= 0) {
res.send({
method: 'sendMessage',
chat_id: chat_id,
text: `Hi ${user_name}, your score is ${memory.threads[chat_id].score}.`
});
}
else if (text.indexOf('/stats') >= 0) {
res.send({
method: 'sendMessage',
chat_id: chat_id,
text: JSON.stringify(memory.threads[chat_id], null, 2)
});
}
else if (text.indexOf('/stats') >= 0) {
res.send({
method: 'sendMessage',
chat_id: chat_id,
text: JSON.stringify(memory.threads[chat_id].current_ques)
});
}
else {
if (['A', 'B', 'C', 'D'].includes(text)) {
res.send({
method: 'sendMessage',
chat_id: chat_id,
text: 'Question Option'
});
} else {
res.send({
method: 'sendMessage',
chat_id: chat_id,
text: 'Other response'
});
}
}
}
}
});
app.listen(PORT, () => {
console.log(`Listening on ${PORT}`);
});