-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
74 lines (66 loc) · 1.61 KB
/
main.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
"use strict";
import "babel-polyfill";
import qs from "qs";
import Auth from "./quiz/auth.js";
import Quiz from "./quiz/quiz.js";
import Question from "./quiz/question.js";
import config from "./config.js";
import redis from "redis";
const redisClient = redis.createClient();
const bot = config.bot;
bot.onText(/\/start/, function onText(msg) {
showQuizList(msg);
});
function showQuizList(msg) {
const chatId = msg.chat.id;
Auth.getUser(chatId).then(
res => {
Quiz.showList(msg, bot);
},
error => {
console.error(error);
}
);
}
// NOTE: callback data should be very small, otherwise you'll see 400 error
bot.on("callback_query", msg => {
const chatId = msg.message.chat.id;
let json = {};
try {
json = qs.parse(msg.data);
} catch (error) {
console.error("Invalid query.");
bot.sendMessage(chatId, "Invalid query.");
}
if (json.action) {
switch (json.action) {
case "get_quiz":
Quiz.show(chatId, json);
break;
case "answer_question":
Question.handleAnswer(chatId, json);
break;
default:
console.error("Unknown callback query.");
break;
}
bot.answerCallbackQuery(msg.id);
}
});
// JUST FOR DEVELOPMENT
// ------------------------------------------
bot.onText(/\/delete user/, function onText(msg) {
redisClient.del(`${msg.chat.id}-user`);
bot.sendMessage(msg.chat.id, "User Removed.");
});
bot.onText(/\/r/, function onText(msg) {
const chatId = msg.chat.id;
Auth.getUser(chatId).then(
res => {
console.log(res.user);
},
error => {
console.error(error);
}
);
});