-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
369 lines (323 loc) · 10.5 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import Fuse from 'fuse.js'
import dotenv from 'dotenv'
import prismaPackage from '@prisma/client'
import { states } from "./state-data.js"
import { districts } from "./district-data.js"
import { Telegraf, Markup } from 'telegraf'
import _ from 'lodash'
import pincodeDirectory from 'india-pincode-lookup'
import { setTelegramBot, pollData } from './poller.js'
import {
AGE_PREFERENCE,
optionsDistrictsSearch,
optionsStatesSearch,
CONVERSATION_STEPS
} from './constants.js'
const { PrismaClient } = prismaPackage
const prisma = new PrismaClient()
dotenv.config()
const fuseStates = new Fuse(states, optionsStatesSearch);
const fuseDistricts = new Fuse(districts, optionsDistrictsSearch);
const token = process.env.BOT_TOKEN
if (token === undefined) {
throw new Error('BOT_TOKEN must be provided!')
}
const bot = new Telegraf(token)
setTelegramBot(bot);
bot.use(Telegraf.log())
bot.start(async (ctx) => {
const user = await prisma.user.findFirst({
where: {
telegramId: ctx.from.id,
ConversationState: {
conversationStep: CONVERSATION_STEPS.INPUT_ACQUIRED_SUCCESSFULLY
}
},
include: {
ConversationState: true
}
})
if (user != null) {
ctx.reply(
'Hey there!👋 \n\n' +
'Welcome welcome back! \n\n' +
'Your notification preferences are already saved. Do you want to clear them?',
Markup.inlineKeyboard([
Markup.button.callback('Go ahead!', 'clear_preference'),
Markup.button.callback('Let it be!', 'noop_clear_preferences')])
)
}
else {
//Create an empty user with associated relation.
await prisma.user.upsert({
where: {
telegramId: ctx.from.id
},
create: {
telegramId: ctx.from.id,
pincode: null,
stateId: null,
districtId: null,
ConversationState: {
create: {
conversationStep: CONVERSATION_STEPS.NO_INPUT_PRESENT
}
}
},
update: {
pincode: null,
stateId: null,
districtId: null,
ConversationState: {
update: {
conversationStep: CONVERSATION_STEPS.NO_INPUT_PRESENT
}
}
}
})
ctx.reply(
'Hey there!👋 \n\n' +
'Welcome to CowinTrack Bot \n\n' +
'I will regularly check for empty slots in your area and will notify you as soon as they become available.\n\n' +
'Lets start.')
ctx.reply('Please select whether you want to be notified by specific district or pincode.',
Markup.inlineKeyboard([
Markup.button.callback('District', 'notify_by_district'),
Markup.button.callback('Pincode', 'notify_by_pincode')])
);
}
})
bot.action('noop_clear_preferences', async (ctx) => {
ctx.answerCbQuery();
ctx.reply(
'No problem! \n\n' +
'You\'re awesome! \n\n')
})
bot.action('clear_preference', async (ctx) => {
await prisma.user.upsert({
where: {
telegramId: ctx.from.id
},
create: {
telegramId: ctx.from.id,
pincode: null,
stateId: null,
districtId: null,
ConversationState: {
create: {
conversationStep: null
}
}
},
update: {
pincode: null,
stateId: null,
districtId: null,
ConversationState: {
update: {
conversationStep: null
}
}
}
})
ctx.reply(
'No problem! \n\n' +
'Let\'s start over! \n\n')
ctx.reply('Please tap /start to begin.');
})
bot.action('notify_by_district', async (ctx) => {
ctx.answerCbQuery();
await prisma.user.update({
where: {
telegramId: ctx.from.id
},
data: {
ConversationState: {
update: {
conversationStep: CONVERSATION_STEPS.INPUT_STATE_NAME
}
}
}
})
ctx.reply('Great! Please type the name of the state where you live in.')
})
bot.action('notify_by_pincode', async (ctx) => {
ctx.answerCbQuery()
await prisma.user.update({
where: {
telegramId: ctx.from.id
},
data: {
ConversationState: {
update: {
conversationStep: CONVERSATION_STEPS.INPUT_PINCODE
}
}
}
})
ctx.reply('Great! Please enter your pincode.')
})
bot.action(/age_select_([0-9]*_*[a-z]+)/, async (ctx) => {
switch (ctx.match[1]) {
case '45_plus':
await setAgePreference(ctx, AGE_PREFERENCE.FORTYFIVE_PLUS)
break;
case '18_plus':
await setAgePreference(ctx, AGE_PREFERENCE.EIGHTEEN_PLUS);
break;
case 'both':
await setAgePreference(ctx, AGE_PREFERENCE.BOTH)
break;
}
ctx.answerCbQuery();
})
bot.on('message', async (ctx) => {
const user = await prisma.user.findFirst({
where: {
telegramId: ctx.from.id
},
include: {
ConversationState: true
}
});
if (user != null)
switch (user.ConversationState.conversationStep) {
case CONVERSATION_STEPS.NO_INPUT_PRESENT:
ctx.reply('Please provide valid input.');
break;
case CONVERSATION_STEPS.INPUT_STATE_NAME:
handleStateInput(ctx, user);
break;
case CONVERSATION_STEPS.INPUT_DISTRICT_NAME:
handleDistrictInput(ctx, user);
break;
case CONVERSATION_STEPS.INPUT_PINCODE:
handlePinCodeInput(ctx, user);
break;
case CONVERSATION_STEPS.INPUT_ACQUIRED_SUCCESSFULLY:
ctx.reply('Please provide valid input.');
break;
default:
ctx.reply('Please provide valid input.');
break;
}
})
bot.launch()
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))
async function handleStateInput(ctx, user) {
const stateInput = ctx.message.text
let exactMatch = false;
const stateMatch = _.find(states, function (data) {
return data.state_name.trim().toLowerCase() == stateInput.trim().toLowerCase()
})
if (stateMatch)
exactMatch = true;
if (exactMatch) {
await prisma.user.update({
where: {
telegramId: ctx.from.id
},
data: {
stateId: stateMatch.state_id,
ConversationState: {
update: {
conversationStep: CONVERSATION_STEPS.INPUT_DISTRICT_NAME
}
}
}
})
ctx.replyWithMarkdown(`Great! Your state has been set to *${stateInput.trim()}*. Please type the name of the district now.`, {
})
} else {
const searchResult = fuseStates.search(stateInput.trim())
ctx.replyWithMarkdown(`Oops! Did you mean *${searchResult[0].item.state_name}*? Please re-enter your state name.`)
console.log(searchResult)
}
}
async function handleDistrictInput(ctx, user) {
const districtInput = ctx.message.text
let exactMatch = false;
const districtMatch = _.find(districts, function (data) {
return data.district_name.trim().toLowerCase() == districtInput.trim().toLowerCase()
})
if (districtMatch)
exactMatch = true;
if (exactMatch) {
await prisma.user.update({
where: {
telegramId: ctx.from.id
},
data: {
districtId: districtMatch.district_id,
ConversationState: {
update: {
conversationStep: CONVERSATION_STEPS.INPUT_AGE
}
}
}
})
ctx.replyWithMarkdown(`Great! Your district has been set to *${districtInput.trim()}*.`)
showAgeSelectKeyboard(ctx)
} else {
const searchResult = fuseDistricts.search(districtInput.trim())
ctx.replyWithMarkdown(`Oops! Did you mean *${searchResult[0].item.district_name}*? Please re-enter your district name.`)
console.log(searchResult)
}
}
async function handlePinCodeInput(ctx, user) {
const pinCodeInput = ctx.message.text
const lookupResult = pincodeDirectory.lookup(pinCodeInput.trim())
const pincodeData = _.first(lookupResult);
let exactMatch = false;
if (pincodeData)
exactMatch = true;
if (exactMatch) {
await prisma.user.update({
where: {
telegramId: ctx.from.id
},
data: {
pincode: pincodeData.pincode,
ConversationState: {
update: {
conversationStep: CONVERSATION_STEPS.INPUT_AGE
}
}
}
})
ctx.replyWithMarkdown(`Great! Your pincode has been set to *${pincodeData.pincode}*.`)
showAgeSelectKeyboard(ctx)
} else {
ctx.reply(`Oops! No such pincode exists. Please retry.`)
}
}
function showAgeSelectKeyboard(ctx) {
ctx.reply('Final step! Please select you age preference.',
Markup.inlineKeyboard([
Markup.button.callback('45+', 'age_select_45_plus'),
Markup.button.callback('18+', 'age_select_18_plus'),
Markup.button.callback('Both', 'age_select_both')]));
}
async function setAgePreference(ctx, agePreference) {
await prisma.user.update({
where: {
telegramId: ctx.from.id
},
data: {
agePreference: agePreference,
ConversationState: {
update: {
conversationStep: CONVERSATION_STEPS.INPUT_ACQUIRED_SUCCESSFULLY
}
}
}
})
ctx.replyWithMarkdown(`Great! Your age preference has been set.`)
ctx.reply('All done! Please wait while I save your preferences.')
ctx.reply('Preferences saved! You will be notified as soon as the slot opens.Stay tuned!. 😎')
}
(async () => {
await pollData();
})();