-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
272 lines (237 loc) · 8.29 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
require('dotenv').config()
const { Telegraf, Markup, session } = require('telegraf')
const { Database } = require('./src/db/Database')
const {
getUserQuestion,
addUserQuestion,
deleteUserQuestion,
getNextQuestion,
getAllUserQuestions,
updateRecall
} = require('./src/services/userQuestionsService')
const { getQuestion } = require('./src/services/questionsService')
const bot = new Telegraf(process.env.BOT_TOKEN)
const welcome = (name) =>
`Hi ${name} 👋
I'm a bot helping you practice LeetCode. Just tell me what LeetCode problems you'd like to practice again \
and I'll help you schedule the next rounds based on the forgetting curve.
Check all available actions with /help.`
const add = 'Start adding your LeetCode problem by sending me its question id (one per message, please 😊).'
const help = `<b>What can this bot do?</b>
This is a bot that can intelligently schedule your next LeetCode problems to practice again based on \
the forgetting curve. It uses a simple yet powerful model of forgetting (similar to Duolingo) to let you practice \
problems with least recall probabilities.
Add or update a problem simply by sending its LeetCode question id to the bot.
<a href="https://github.com/SiyanH/leetrepeat-bot">Code</a> is open source on GitHub.
<b>Available actions</b>
/start shows welcome message
/next fetches your next problem
/all lists all your problems`
const questionURL = (titleSlug) => `https://leetcode.com/problems/${titleSlug}`
bot.use(session())
bot.use((ctx, next) => {
if (!ctx.session) {
ctx.session = {}
}
return next()
})
bot.start(ctx =>
ctx.reply(welcome(ctx.from.first_name))
.then(() =>
ctx.reply(add)
)
.catch(r => console.log('Bot failed to start\n' + r))
)
bot.help(ctx => ctx.replyWithHTML(help))
bot.command('all', async ctx => {
try {
const questions = await getAllUserQuestions(ctx.from.id)
// Split questions to show on multiple pages if number of questions is over limit
if (questions.length) {
let html = ''
const limit = 10 // number of questions shown on each page
ctx.session.userQuestionsHTML = []
for (let i = 0; i < questions.length; i++) {
if (i > 0 && i % limit === 0) {
ctx.session.userQuestionsHTML.push(html)
html = ''
}
const url = questionURL(questions[i].question[0].titleSlug)
html += `<a href="${url}">${questions[i].question[0].id}. ${questions[i].question[0].title}</a>\n`
}
ctx.session.userQuestionsHTML.push(html)
ctx.session.userQuestionsPage = 0
ctx.replyWithHTML(ctx.session.userQuestionsHTML[0], {
disable_web_page_preview: true,
reply_markup: ctx.session.userQuestionsHTML[1]
? Markup.inlineKeyboard([Markup.button.callback('>>', 'next_page')]).reply_markup : {}
})
} else {
ctx.reply(`You haven't got any problem in your bucket. Try adding one?`)
}
} catch (e) {
console.error('Bot failed to process command /all\n' + e)
}
})
bot.command('next', async ctx => {
try {
const userId = ctx.from.id
ctx.reply(`Sit tight...I'm fetching your next LeetCode problem to practice 😎`)
const question = await getNextQuestion(userId)
await ctx.reply('Here is you next question. Practice makes perfect 💪')
ctx.reply(questionURL(question.titleSlug))
} catch (e) {
console.error('Bot failed to process command /next\n' + e)
}
})
bot.on('message', async ctx => {
try {
const userId = ctx.from.id
const questionId = Number(ctx.message.text)
if (isNaN(questionId) || !Number.isInteger(questionId)) {
ctx.reply('Well, this is not a valid question id...Try again?')
} else {
const question = await getQuestion(questionId)
const userQuestion = await getUserQuestion(questionId, userId)
if (userQuestion) {
ctx.session.userQuestion = userQuestion
await ctx.replyWithHTML(`I've found this LeetCode problem in your question bucket.
You can <b>update</b> it with the recent status of your solution or <b>delete</b> it from your bucket.`)
ctx.reply(questionURL(question.titleSlug), Markup.inlineKeyboard([
Markup.button.callback('Delete', 'delete'),
Markup.button.callback('Update', 'update')
]))
} else {
const res = await addUserQuestion(questionId, userId)
if (res) {
await ctx.reply(`Okay, I've added the following LeetCode problem to your question bucket 👍`)
ctx.reply(questionURL(question.titleSlug))
} else {
ctx.reply(`Oops...Some error occurred so I couldn't add your question. Try again?`)
}
}
}
} catch (e) {
console.error('Bot failed to process user message\n' + e)
}
})
bot.action('next_page', async ctx => {
try {
await ctx.answerCbQuery()
const html = ctx.session.userQuestionsHTML[++ctx.session.userQuestionsPage]
let reply_markup
if (ctx.session.userQuestionsHTML[ctx.session.userQuestionsPage + 1]) {
reply_markup = Markup.inlineKeyboard([
Markup.button.callback('<<', 'previous_page'),
Markup.button.callback('>>', 'next_page')
]).reply_markup
} else {
reply_markup = Markup.inlineKeyboard([
Markup.button.callback('<<', 'previous_page'),
]).reply_markup
}
ctx.editMessageText(html, {
parse_mode: 'HTML',
disable_web_page_preview: true,
reply_markup
})
} catch (e) {
console.error(`Bot failed to process callback 'next_page'\n` + e)
}
})
bot.action('previous_page', async ctx => {
try {
await ctx.answerCbQuery()
const html = ctx.session.userQuestionsHTML[--ctx.session.userQuestionsPage]
let reply_markup
if (ctx.session.userQuestionsHTML[ctx.session.userQuestionsPage - 1]) {
reply_markup = Markup.inlineKeyboard([
Markup.button.callback('<<', 'previous_page'),
Markup.button.callback('>>', 'next_page')
]).reply_markup
} else {
reply_markup = Markup.inlineKeyboard([
Markup.button.callback('>>', 'next_page')
]).reply_markup
}
ctx.editMessageText(html, {
parse_mode: 'HTML',
disable_web_page_preview: true,
reply_markup
})
} catch (e) {
console.error(`Bot failed to process callback 'previous_page'\n` + e)
}
})
bot.action('update', async ctx => {
try {
await ctx.answerCbQuery()
ctx.editMessageReplyMarkup(Markup.inlineKeyboard([
Markup.button.callback('Rejected', 'rejected'),
Markup.button.callback('Accepted', 'accepted')
]).reply_markup)
} catch (e) {
console.error(`Bot failed to process callback 'update'\n` + e)
}
})
bot.action('delete', async ctx => {
try {
const res = await deleteUserQuestion(ctx.session.userQuestion._id)
if (res) {
ctx.answerCbQuery('Delete successful')
ctx.editMessageText(`[Deleted] ${ctx.callbackQuery.message.text}`)
} else {
ctx.answerCbQuery('Delete failed')
}
} catch (e) {
console.error(`Bot failed to process callback 'delete'\n` + e)
}
})
bot.action('accepted', async ctx => {
try {
const res = await updateRecall(ctx.session.userQuestion, 1)
if (res) {
ctx.answerCbQuery('Update successful')
ctx.editMessageText(`[Accepted] ${ctx.callbackQuery.message.text}`)
} else {
ctx.answerCbQuery('Update failed')
}
} catch (e) {
console.error(`Bot failed to process callback 'accepted'\n` + e)
}
})
bot.action('rejected', async ctx => {
try {
const res = await updateRecall(ctx.session.userQuestion, 0)
if (res) {
ctx.answerCbQuery('Update successful')
ctx.editMessageText(`[Rejected] ${ctx.callbackQuery.message.text}`)
} else {
ctx.answerCbQuery('Update failed')
}
} catch (e) {
console.error(`Bot failed to process callback 'rejected'\n` + e)
}
})
// Run with webhook
// When running locally, get domain from command 'npx lt --port xxxx'
Database.client.connect()
.then(
bot.launch({
webhook: {
domain: process.env.DOMAIN,
hookPath: `/${process.env.WEBHOOK_PATH}`,
port: process.env.PORT
}
}).catch(r => 'Bot failed to lunch\n' + r)
)
.catch(e => console.log('Failed to connect to Database\n' + e))
// Enable graceful stop
process.once('SIGINT', () => {
bot.stop('SIGINT')
Database.client.close()
})
process.once('SIGTERM', () => {
bot.stop('SIGTERM')
Database.client.close()
})