-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
136 lines (123 loc) · 2.87 KB
/
game.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
require('dotenv').config();
const Nexmo = require('Nexmo');
const fetch = require('node-fetch');
const { request } = require('express');
let answeringQuestion = false;
let currentQuestion = {};
let questionCount = 0;
let score = 0;
const alpha = [ 'A', 'B', 'C', 'D', 'E' ];
const nexmo = new Nexmo(
{
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
applicationId: process.env.APPLICATION_ID,
privateKey: process.env.PRIVATE_KEY_FILE,
},
{
debug: true,
// use Sandbox endpoint
apiHost: process.env.API_HOST,
}
);
async function generateQuestion () {
try {
answeringQuestion = true;
questionCount++;
const response = await fetch('https://opentdb.com/api.php?amount=1');
const json = await response.json();
currentQuestion = json.results[0];
let choices = [ ...currentQuestion.incorrect_answers ];
choices.push(currentQuestion.correct_answer);
shuffleQuestions(choices);
currentQuestion.choices = choices;
sendQuestion(currentQuestion);
} catch (error) {
console.log(error);
}
}
function checkAnswer (answer) {
answeringQuestion = false;
// find answer in choices
let index = alpha.indexOf(answer);
if (index < 0) {
sendError();
} else {
if (currentQuestion.choices[index] === currentQuestion.correct_answer) {
// correct answer
score++;
sendMessage('Correct!');
} else {
// incorrect answer
let correctIndex = currentQuestion.choices.indexOf(
currentQuestion.correct_answer
);
let incorrectResponse = `Sorry, the correct answer was ${alpha[
correctIndex
]}.`;
sendMessage(incorrectResponse);
}
setTimeout(sendAnother, 2000);
}
}
function sendQuestion (currentQuestion) {
let answers = '';
currentQuestion.choices.forEach((answer, idx) => {
answers += '\n' + alpha[idx] + '. ' + answer;
});
let str = currentQuestion.question + answers;
let str1 = str.replace(/"/gi, '"');
let questionText = str1.replace(/'/gi, "'");
sendMessage(questionText);
}
function sendAnother () {
sendMessage('Do you want another question? (Y/N)');
}
function sendScore () {
sendMessage(
`Thanks for playing! You answered ${score} question${score > 1
? 's'
: ''} correctly out of ${questionCount}.`
);
score = 0;
questionCount = 0;
}
function sendMessage (messageText) {
nexmo.channel.send(
{
type: 'whatsapp',
number: process.env.TO_NUMBER,
},
{
type: 'whatsapp',
number: process.env.WHATSAPP_NUMBER,
},
{
content: {
type: 'text',
text: messageText,
},
},
(err, data) => {
if (err) {
console.log(err);
} else {
console.log(data.message_uuid);
}
}
);
}
function shuffleQuestions (array) {
array.sort(() => Math.random() - 0.5);
}
function sendError () {
sendMessage('Sorry, I did not recognise that response. Please try again');
}
module.exports = {
answeringQuestion,
checkAnswer,
sendScore,
sendMessage,
sendError,
generateQuestion,
};