-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
147 lines (136 loc) · 3.87 KB
/
index.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
const express = require('express');
const server = express();
const MxSdk = require('machaao');
const lib = new MxSdk('<----Bot Token----->', 'prod', server);
const api = 'https://opentdb.com/api.php?amount=1&category=9&difficulty=easy&type=multiple';
const rp = require('request-promise');
const welcome_responses = [
'hi',
'hello',
'bojour',
'namaste',
'kia ora',
'bula',
'kemunacho',
'hey',
'yo',
'sup',
'whats up?',
'wassup',
'get_started',
'get started',
];
server.post('/incoming', async (req, res) => {
try {
//get incoming messages
const x = await lib.getUserMessages(req);
//manage user response
if (x.length > 0) {
const incoming = x[0].message_data;
switch (incoming.action_type) {
case 'postback':
if (welcome_responses.includes(incoming.text)) sendWelcomeMessage(req);
else sendFallbackMessage(req);
break;
case 'quick_reply':
if (welcome_responses.includes(incoming.text)) sendWelcomeMessage(req);
else if (incoming.text === 'start') sendQuestionMessage(req);
else checkResult(incoming, req);
break;
case 'get_started':
if (welcome_responses.includes(incoming.text)) sendWelcomeMessage(req);
else sendFallbackMessage(req);
break;
}
}
res.sendStatus(200);
} catch (error) {
console.error(error.stack);
res.send(500);
}
});
async function getQuestion() {
const options = {
method: 'GET',
uri: api,
headers: {
'Content-Type': 'application/json',
},
transform: function (body, response) {
if (typeof body === 'string') {
response.body = JSON.parse(body);
return response.body.results[0];
} else return response.body.results[0];
},
};
return rp(options);
}
function _getUserQuestion(payload) {
return payload.filter((q) => q.name === 'currentQuestion');
}
async function checkResult(incoming, req) {
const answer = await lib.getUserMessages(req);
const userTags = await lib.getUserTags(req);
if (userTags.length > 0) {
const currentQ = _getUserQuestion(userTags);
const correct_answer = currentQ[0].values[0].correct_answer;
if (correct_answer === incoming.text) await sendCorrectAnswerMessage(req);
else await sendWrongAnswerMessage(req, correct_answer);
}
}
async function sendWelcomeMessage(req) {
return lib.sendButtonsOrQuickRepliesMessage(
req,
"Hi I'm TriviaBot, here to entertain you with multi choice Trivia Questions! Click Start to begin!",
[],
[{ title: 'Start', content_type: 'text', payload: 'start' }] // sample quick reply
);
}
async function sendCorrectAnswerMessage(req) {
return lib.sendButtonsOrQuickRepliesMessage(
req,
'Correct Answer!',
[],
[{ title: 'Next Question', content_type: 'text', payload: 'start' }] // sample quick reply
);
}
async function sendWrongAnswerMessage(req, correct_answer) {
return lib.sendButtonsOrQuickRepliesMessage(
req,
`Sorry, the right answer is ${correct_answer}.`,
[],
[{ title: 'Next Question', content_type: 'text', payload: 'start' }] // sample quick reply
);
}
async function sendFallbackMessage(req) {
return lib.sendButtonsOrQuickRepliesMessage(
req,
"I'm not sure I understand, to begin the quiz, tap Start below!",
[],
[{ title: 'Start', content_type: 'text', payload: 'start' }] // sample quick reply
);
}
async function sendQuestionMessage(req) {
try {
//generate question
//set user tag
//send question
const q = await getQuestion();
const response = await lib.addUserTag('currentQuestion', [q], req);
let answers = [...q.incorrect_answers, q.correct_answer];
answers = answers.sort((a, b) => {
return 0.5 - Math.random();
});
const qrs = [];
answers.map((a) => {
qrs.push({ title: a, content_type: 'text', payload: a });
});
return lib.sendButtonsOrQuickRepliesMessage(req, q.question, [], qrs);
} catch (error) {
console.error(error.stack);
}
}
const p = process.env.PORT || 3000;
server.listen(p, () => {
console.log(`Listening on port ${p}`);
});