-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (97 loc) · 2.96 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
const {
Bot,
Keyboard,
InlineKeyboard,
HttpError,
GrammyError,
} = require('grammy');
const {
getRandomQuestion,
getCorrectAnswer
} = require('./utils');
require('dotenv').config();
const bot = new Bot(process.env.BOT_TOKEN);
bot.command('start', async (ctx) => {
const menuKeyboard = new Keyboard()
.text('.NET')
.text('Patterns')
.row()
.text('DBMS')
.text('Agile')
.row()
.text('Random')
.resized();
await ctx.reply(
'Hello! \n I\'m the bot who help you with preparation to .NET interview.\nChoose the question topic below: ', {
reply_markup: menuKeyboard,
});
});
bot.hears(
['.NET', 'Patterns', 'DBMS', 'Agile', 'Random'],
async (ctx) => {
const topic = ctx.message.text;
const {
question,
questionTopic
} = getRandomQuestion(topic);
let inlineKeyboard;
if (question.isChoosable) {
const mappedButtons = question.options.map((option) => [
InlineKeyboard.text(
option.text,
JSON.stringify({
type: `${questionTopic}-option`,
isCorrect: option.isCorrect,
questionId: question.id,
}),
),
]);
inlineKeyboard = InlineKeyboard.from(mappedButtons);
} else {
inlineKeyboard = new InlineKeyboard().text(
'Answer',
JSON.stringify({
type: questionTopic,
questionId: question.id,
}),
);
}
await ctx.reply(question.text, {
reply_markup: inlineKeyboard,
});
},
);
bot.on('callback_query:data', async (ctx) => {
const callbackData = JSON.parse(ctx.callbackQuery.data);
if (!callbackData.type.includes('option')) {
const answer = getCorrectAnswer(callbackData.type, callbackData.questionId);
await ctx.reply(answer, {
parse_mode: 'HTML',
disable_web_page_preview: true,
});
await ctx.answerCallbackQuery();
} else if (callbackData.isCorrect) {
await ctx.reply('Correct!');
await ctx.answerCallbackQuery();
} else {
const answer = getCorrectAnswer(
callbackData.type.split('-')[0],
callbackData.questionId,
);
await ctx.reply(`Wrong:( The correct answer is: ${answer}`);
await ctx.answerCallbackQuery();
}
});
bot.catch((err) => {
const ctx = err.ctx;
console.error(`Update handling error (id: ${ctx.update.update_id})`);
const e = err.error;
if (e instanceof GrammyError) {
console.error('Request error:', e.description);
} else if (e instanceof HttpError) {
console.error('Telegram is unavailable:', e);
} else {
console.error('Unknown error:', e);
}
});
bot.start();