-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
706 lines (632 loc) · 26.9 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
require('dotenv').config();
/* TESTING START */
// const { Telegraf, Markup } = require('telegraf');
// const bot = new Telegraf(process.env.BOT_TOKEN);
/* TESTING END */
/* PRODUCTION START */
const { Composer } = require('micro-bot');
const bot = new Composer;
/* PRODUCTION END */
const axios = require('axios');
var firebaseAdminSdk = require("firebase-admin")
var { subjectsData, studyMaterials } = require('./data')
var admins = [{ chatId: 625310795, userId: 'elab_innovations', name: 'e-lab innovations' }, { chatId: 591998055, userId: 'KarthikSunilK', name: 'Karthik Sunil K' }]
var uploadMaterials = {}
var uploadSubject = {}
//https://stackoverflow.com/a/61844642/11409930
firebaseAdminSdk.initializeApp({
credential: firebaseAdminSdk.credential.cert(
JSON.parse(Buffer.from(process.env.GOOGLE_CONFIG_BASE64, 'base64').toString('ascii'))),
databaseURL: "https://tkmcebot-default-rtdb.firebaseio.com/"
});
var db = firebaseAdminSdk.database()
const updateData = (ctx) => {
if (ctx) {
ctx.telegram.sendChatAction(ctx.chat.id, 'typing')
}
let _subjectsData = []
db.ref("subjects").once("value", snapshot => {
Object.keys(snapshot.val()).forEach(sub => _subjectsData.push(snapshot.val()[sub]))
subjectsData = _subjectsData
if (ctx) {
ctx.telegram.sendMessage(ctx.chat.id, 'subjectsData updated, ' + subjectsData.length, +' items')
}
}, (errorObject) => {
console.log('subjectsData updation failed, ' + errorObject.name);
if (ctx) {
ctx.telegram.sendMessage(ctx.chat.id, 'subjectsData updation failed, ' + errorObject.name)
}
})
let _studyMaterials = []
db.ref("studyMaterials").once("value", snapshot => {
Object.keys(snapshot.val()).forEach(material => _studyMaterials.push(snapshot.val()[material]))
studyMaterials = _studyMaterials
if (ctx) {
ctx.telegram.sendMessage(ctx.chat.id, 'studyMaterials updated, ' + studyMaterials.length, +' items')
}
}, (errorObject) => {
console.log('studyMaterials updation failed, ' + errorObject.name);
if (ctx) {
ctx.telegram.sendMessage(ctx.chat.id, 'studyMaterials updation failed, ' + errorObject.name)
}
})
let _admins = []
db.ref("admins").once("value", snapshot => {
Object.keys(snapshot.val()).forEach(admin => _admins.push(snapshot.val()[admin]))
admins = _admins
if (ctx) {
ctx.telegram.sendMessage(ctx.chat.id, 'admins updated, ' + admins.length, +' items')
}
}, (errorObject) => {
console.log('admins updation failed, ' + errorObject.name);
if (ctx) {
ctx.telegram.sendMessage(ctx.chat.id, 'admins updation failed, ' + errorObject.name)
}
})
}
const codeToName = code => {
switch (code) {
case 'ce':
return 'CIVIL ENGINEERING'
case 'mech':
return 'MECHANICAL ENGINEERING'
case 'eee':
return 'ELECTRICAL & ELECTRONICS ENGINEERING'
case 'ece':
return 'ELECTRONICS & COMMUNICATION ENGINEERING'
case 'cse':
return 'COMPUTER SCIENCE AND ENGINEERING'
case 'ce':
return 'CHEMICAL ENGINEERING'
case 'archi':
return 'ARCHITECTURE'
default:
return 'UNKNOWN'
}
}
const reportError = (msg, ctx) => {
ctx.telegram.sendMessage(ctx.chat.id, '🪲 Got bug!\nReport to Developer 👨🏻💻', {
reply_markup: {
inline_keyboard: [
[{ text: "Karthik", url: "https://wa.me/918606683287?text=Got Bug\n" + msg }],
[{ text: "e-lab innovations", url: "https://wa.me/918089931063?text=Got Bug\n" + msg }]
]
}
})
}
const sendSemesters = (code, ctx) => {
ctx.editMessageText(`*${codeToName(code)}*\n\nSelect your *Semester* 👇`, {
reply_markup: {
inline_keyboard: [
[{ text: "SEMESTER 1", callback_data: code + "_1" }, { text: "SEMESTER 2", callback_data: code + "_2" }],
[{ text: "SEMESTER 3", callback_data: code + "_3" }, { text: "SEMESTER 4", callback_data: code + "_4" }],
[{ text: "SEMESTER 5", callback_data: code + "_5" }, { text: "SEMESTER 6", callback_data: code + "_6" }],
[{ text: "SEMESTER 7", callback_data: code + "_7" }, { text: "SEMESTER 8", callback_data: code + "_8" }],
[{ text: "◀️ Back", callback_data: "menu" }],
]
},
parse_mode: "MarkdownV2"
})
}
const sendSubjectList = (code, ctx) => {
let dept = code.split('_')[0]
let sem = code.split('_')[1]
let subjects = subjectsData.filter(subject => {
return subject.department == dept && subject.sem == sem
})
const mateCount = subCode => {
return studyMaterials.filter(material => material.subjectCode == subCode).length
}
let inline_keyboard = subjects.map(Subject => {
return [{ text: `📚 [${mateCount(Subject.code)}] ${Subject.code} - ${Subject.name}`, callback_data: ['sub', Subject.code, code].join('_') }]
})
inline_keyboard.push([{ text: "◀️ Back", callback_data: dept }])
ctx.editMessageText(`*${codeToName(dept)}*\n*Semester ${sem}*\n\nSelect a *Subject* 👇`, {
reply_markup: {
inline_keyboard: inline_keyboard
},
parse_mode: "MarkdownV2"
})
}
const sendSubjectDetails = (code, ctx) => {
let subCode = code.split('_')[1]
let dept = code.split('_')[2]
let sem = code.split('_')[3]
let subject = subjectsData.find(subject => {
return subject.code == subCode
})
const mateCount = module => {
return studyMaterials.filter(material => {
return material.subjectCode == subCode &&
(material.modules.includes(module + '') || module == 'A')
}).length
}
if (subject) {
ctx.editMessageText(`*${codeToName(dept)}*\n*Semester ${sem}*\n*${subCode}* \\- ${subject.name}\n\nSelect *Module*`, {
reply_markup: {
inline_keyboard: [
[{ text: `Module 1 📚 [${mateCount(1)}]`, callback_data: ['mod', subCode, '1', dept, sem].join('_') }, { text: `Module 2 📚 [${mateCount(2)}]`, callback_data: ['mod', subCode, '2', dept, sem].join('_') }],
[{ text: `Module 3 📚 [${mateCount(3)}]`, callback_data: ['mod', subCode, '3', dept, sem].join('_') }, { text: `Module 4 📚 [${mateCount(4)}]`, callback_data: ['mod', subCode, '4', dept, sem].join('_') }],
[{ text: `Module 5 📚 [${mateCount(5)}]`, callback_data: ['mod', subCode, '5', dept, sem].join('_') }, { text: `All 📚 [${mateCount('A')}]`, callback_data: ['mod', subCode, 'A', dept, sem].join('_') }],
[{ text: "◀️ Back", callback_data: [dept, sem].join('_') }]
]
},
parse_mode: "MarkdownV2"
})
}
}
const sendModuleDetails = (code, ctx) => {
let subCode = code.split('_')[1]
let module = code.split('_')[2]
let dept = code.split('_')[3]
let sem = code.split('_')[4]
let subject = subjectsData.find(subject => {
return subject.code == subCode
})
const mateCount = type => {
return studyMaterials.filter(material => {
return material.subjectCode == subCode &&
(material.modules.includes(module + '') || module == 'A') &&
material.type == type
}).length
}
if (subject) {
ctx.editMessageText(`*${codeToName(dept)}*\n*Semester ${sem}*\n*${subCode}* \\- ${subject.name}\n*Medule ${module}*\n\nSelect a type 👇`, {
reply_markup: {
inline_keyboard: [
[{ text: `✍️ Class Notes [${mateCount('CN')}]`, callback_data: ['mate_CN', subCode, module, dept, sem].join('_') }, { text: `📄 Printed Notes [${mateCount('PN')}]`, callback_data: ['mate_PN', subCode, module, dept, sem].join('_') }],
[{ text: `📚 Text Books [${mateCount('TB')}]`, callback_data: ['mate_TB', subCode, module, dept, sem].join('_') }, { text: `📃 Q Papers [${mateCount('QP')}]`, callback_data: ['mate_QP', subCode, module, dept, sem].join('_') }],
[{ text: `🎞 Videos [${mateCount('V')}]`, callback_data: ['mate_V', subCode, module, dept, sem].join('_') }, { text: "◀️ Back", callback_data: ['sub', subCode, dept, sem].join('_') }]
]
},
parse_mode: "MarkdownV2"
})
}
}
const sendMeterialDetails = (code, ctx) => {
let mateType = code.split('_')[1]
let subCode = code.split('_')[2]
let module = code.split('_')[3]
let materials = studyMaterials.filter(material => {
return material.subjectCode == subCode &&
(material.modules.includes(module + '') || module == 'A') &&
material.type == mateType
})
materials.forEach(async material => {
if (material.type == 'CN' ||
material.type == 'PN' ||
material.type == 'TB' ||
material.type == 'QP') {
try {
await ctx.telegram.sendDocument(ctx.chat.id, material.content, {
caption: `<b>${material.name}</b>\n\n @tkmcebot`,
parse_mode: 'HTML'
})
} catch (error) {
reportError(`sendDocument - ${material.content}`, ctx)
console.log(error);
}
}
if (material.type == 'V') {
try {
await ctx.replyWithHTML(`<b>${material.name}</b>\n\n${material.content}\n\n @tkmcebot`)
} catch (error) {
console.log(error);
reportError(`sendMessage - ${material.content}`, ctx)
}
}
})
}
const addMaterialToDB = (code, ctx) => {
ctx.telegram.sendChatAction(ctx.chat.id, 'typing')
let message_id = code.split('_')[1]
let uploadData = uploadMaterials[message_id]
if (uploadData) {
db.ref('studyMaterials').push(uploadData, error => {
if (!error) {
ctx.reply(`New material added\n click /updateDB to update database`)
delete uploadMaterials[message_id]
ctx.deleteMessage()
} else {
ctx.reply(`Failed from server\n ${JSON.stringify(error)}`)
}
})
}
}
const addSubjectToDB = (code, ctx) => {
ctx.telegram.sendChatAction(ctx.chat.id, 'typing')
let message_id = code.split('_')[1]
let newSubject = uploadSubject[message_id]
if (newSubject) {
db.ref('subjects').push(newSubject, error => {
if (!error) {
ctx.reply(`New subject added\n click /updateDB to update database`)
delete uploadSubject[message_id]
ctx.deleteMessage()
} else {
ctx.reply(`Failed from server\n ${JSON.stringify(error)}`)
}
})
}
}
//Start
bot.start((ctx) => {
let fname = ctx.message.chat.first_name ? ctx.message.chat.first_name : ''
let lname = ctx.message.chat.last_name ? ctx.message.chat.last_name : ''
let chatId = ctx.chat.id
let userId = ctx.chat.username ? ctx.chat.username : ''
ctx.telegram.sendChatAction(ctx.chat.id, 'typing')
// console.log(ctx.chat);
db.ref("users/" + chatId).once("value", snapshot => {
let isNew = !snapshot.val()
if (isNew) {
ctx.telegram.sendMessage(chatId, `Hi ${fname} ${lname}
I'm <a href="tg://user?id=1129048108">tkmce bot</a> developed to provide study materials.
Click /help to get help.\n\nClick here to select your department 👇`, {
reply_markup: {
inline_keyboard: [
[{ text: "CIVIL", callback_data: "ce" }, { text: "MECHANICAL", callback_data: "mech" }],
[{ text: "ELECTRICAL & ELECTRONICS", callback_data: "eee" }, { text: "ELECTRONICS & COMMUNICATION", callback_data: "ece" }],
[{ text: "COMPUTER SCIENCE", callback_data: "cse" }, { text: "CHEMICAL", callback_data: "ce" }],
[{ text: "ARCHITECTURE", callback_data: "archi" }, { text: "⚙️ More Options", callback_data: "more-options" }]
]
},
parse_mode: 'HTML'
})
db.ref('users').child(chatId).set({
userId,
chatId,
fname,
lname
});
} else {
ctx.telegram.sendMessage(chatId, `Hi ${fname} ${lname}\nWelcome back 😍\n\nClick here to select your department 👇`, {
reply_markup: {
inline_keyboard: [
[{ text: "CIVIL", callback_data: "ce" }, { text: "MECHANICAL", callback_data: "mech" }],
[{ text: "ELECTRICAL & ELECTRONICS", callback_data: "eee" }, { text: "ELECTRONICS & COMMUNICATION", callback_data: "ece" }],
[{ text: "COMPUTER SCIENCE", callback_data: "cse" }, { text: "CHEMICAL", callback_data: "ce" }],
[{ text: "ARCHITECTURE", callback_data: "archi" }, { text: "⚙️ More Options", callback_data: "more-options" }]
]
}
})
//update user data
db.ref('users').child(chatId).set({
userId,
chatId,
fname,
lname
});
}
}, (errorObject) => {
console.log('User read failed, ' + errorObject.name);
ctx.telegram.sendMessage(chatId, `Hi ${fname?fname:""} ${lname?lname:""}
I'm <a href="tg://user?id=1129048108">tkmce bot</a> developed to provide study materials.
Click /help to get help.\n\nClick here to select your department 👇`, {
reply_markup: {
inline_keyboard: [
[{ text: "CIVIL", callback_data: "ce" }, { text: "MECHANICAL", callback_data: "mech" }],
[{ text: "ELECTRICAL & ELECTRONICS", callback_data: "eee" }, { text: "ELECTRONICS & COMMUNICATION", callback_data: "ece" }],
[{ text: "COMPUTER SCIENCE", callback_data: "cse" }, { text: "CHEMICAL", callback_data: "ce" }],
[{ text: "ARCHITECTURE", callback_data: "archi" }, { text: "⚙️ More Options", callback_data: "more-options" }]
]
},
parse_mode: 'HTML'
})
})
})
//Hi
bot.hears(['Hi', 'hi', 'Hii', 'hii', 'hai', 'Hai'], (ctx) => {
ctx.telegram.sendMessage(ctx.chat.id, 'Click here to select your department 👇', {
reply_markup: {
inline_keyboard: [
[{ text: "CIVIL", callback_data: "ce" }, { text: "MECHANICAL", callback_data: "mech" }],
[{ text: "ELECTRICAL & ELECTRONICS", callback_data: "eee" }, { text: "ELECTRONICS & COMMUNICATION", callback_data: "ece" }],
[{ text: "COMPUTER SCIENCE", callback_data: "cse" }, { text: "CHEMICAL", callback_data: "ce" }],
[{ text: "ARCHITECTURE", callback_data: "archi" }, { text: "⚙️ More Options", callback_data: "more-options" }]
]
}
})
})
//menu
bot.action('menu', (ctx) => {
ctx.editMessageText('Click here to select you department 👇', {
reply_markup: {
inline_keyboard: [
[{ text: "CIVIL", callback_data: "ce" }, { text: "MECHANICAL", callback_data: "mech" }],
[{ text: "ELECTRICAL & ELECTRONICS", callback_data: "eee" }, { text: "ELECTRONICS & COMMUNICATION", callback_data: "ece" }],
[{ text: "COMPUTER SCIENCE", callback_data: "cse" }, { text: "CHEMICAL", callback_data: "ce" }],
[{ text: "ARCHITECTURE", callback_data: "archi" }, { text: "⚙️ More Options", callback_data: "more-options" }]
]
}
})
ctx.answerCbQuery()
})
//More options
bot.action('more-options', ctx => {
ctx.editMessageText('More Options', {
reply_markup: {
inline_keyboard: [
[{ text: "🧑💻 Contact Developers", callback_data: "contactDevs" }],
[{ text: "Contribute", callback_data: "contribute" }],
[{ text: "◀️ Back", callback_data: "menu" }]
]
}
})
ctx.answerCbQuery()
})
//Conact admin option
bot.action('contactDevs', ctx => {
ctx.editMessageText('Contact Developers', {
reply_markup: {
inline_keyboard: [
[{ text: "Karthik", url: "https://wa.me/918606683287?text=Hello from tkmce bot" }],
[{ text: "e-lab innovations", url: "https://t.me/elab_innovations" }],
[{ text: "◀️ Back", callback_data: "more-options" }]
]
}
})
ctx.answerCbQuery()
})
//Delete Message
bot.action('deleteMsg', ctx => {
ctx.deleteMessage()
})
//Contriute
bot.action('contribute', ctx => {
let msg = `I appreciate your interest in contributing.
Contact <b>admins</b> to upload study materials.
Thank you 😍
<b>ADMINS</b>
${admins.map(admin => `🧑💻 <a href="tg://user?id=${admin.chatId}">${admin.userId}</a>`).join('\n')}
`
ctx.telegram.sendMessage(ctx.chat.id, msg, {
parse_mode: 'HTML'
})
ctx.answerCbQuery()
})
//contribute
bot.command('contribute', ctx => {
let msg = `I appreciate your interest in contributing.
Contact <b>admins</b> to upload study materials.
Thank you 😍
<b>ADMINS</b>
${admins.map(admin => `🧑💻 <a href="tg://user?id=${admin.chatId}">${admin.userId}</a>`).join('\n')}
`
ctx.telegram.sendMessage(ctx.chat.id, msg, {
parse_mode: 'HTML'
})
})
//Help
bot.command('help', ctx => {
let fname = ctx.message.chat.first_name
let lname = ctx.message.chat.last_name
ctx.replyWithHTML(`Hi ${fname?fname:""} ${lname?lname:""}
I'm <a href="tg://user?id=1129048108">tkmce bot</a> developed to provide study materials.
Please send 'Hi' or click /start to get options.
For contributing study material, click /contribute
`)
})
bot.on('callback_query', (ctx) => {
let qData = ctx.callbackQuery.data
if (qData.startsWith('ce_') ||
qData.startsWith('mech_') ||
qData.startsWith('eee_') ||
qData.startsWith('ece_') ||
qData.startsWith('cse_') ||
qData.startsWith('ce_') ||
qData.startsWith('archi_')
) {
sendSubjectList(qData, ctx)
}
if (qData == 'ce' ||
qData == 'mech' ||
qData == 'eee' ||
qData == 'ece' ||
qData == 'cse' ||
qData == 'ce' ||
qData == 'archi'
) {
sendSemesters(qData, ctx)
}
if (qData.startsWith('sub_')) {
sendSubjectDetails(qData, ctx)
}
if (qData.startsWith('mod_')) {
sendModuleDetails(qData, ctx)
}
if (qData.startsWith('mate_')) {
sendMeterialDetails(qData, ctx)
}
if (qData.startsWith('addMaterial_')) {
addMaterialToDB(qData, ctx)
}
if (qData.startsWith('addSubject_')) {
addSubjectToDB(qData, ctx)
}
// Explicit usage
ctx.telegram.answerCbQuery(ctx.callbackQuery.id)
// Using context shortcut
ctx.answerCbQuery()
})
bot.on('sticker', (ctx) => {
ctx.reply("Sorry!!!\nthis bot can't perform the this command please send a Hi \nWe will update soon")
})
/* ADMIN */
//Update Database
bot.command('updateDB', ctx => {
let isAdmin = admins.find(admin => admin.chatId == ctx.chat.id)
if (isAdmin) {
updateData(ctx);
} else {
ctx.telegram.sendMessage(ctx.chat.id, '❗️ You can\'t do that')
}
})
//Send File ID
bot.command('getid', ctx => {
if (ctx.update.message.reply_to_message) {
if (ctx.update.message.reply_to_message.document) {
ctx.replyWithMarkdown("`" + ctx.update.message.reply_to_message.document.file_id + "`")
} else {
ctx.reply("That is not a document")
}
} else {
ctx.reply("Send as a reply to a document")
}
})
//Add New Subject
bot.command('addSubject', ctx => {
let isAdmin = admins.find(admin => admin.chatId == ctx.chat.id)
if (isAdmin) {
let text = ctx.update.message.text
text = text.replace('/addSubject ', '')
let parameters = text.split('&')
let helpText = `Send like
<code>/addSubject depertmentCode&semester&subjectCode&subjectName</code>
<b>depertmentCode</b>: depertment code in small letters
ce - CIVIL ENGINEERING'
mech - MECHANICAL ENGINEERING'
eee - ELECTRICAL & ELECTRONICS ENGINEERING'
ece - ELECTRONICS & COMMUNICATION ENGINEERING'
cse - COMPUTER SCIENCE AND ENGINEERING'
ce - CHEMICAL ENGINEERING'
archi - ARCHITECTURE'
<b>semester</b>: Semester (between 1 to 8)
<b>subjectCode</b>: Subject in capital letters
<b>subjectName</b>: Name of the subject
eg: <code>/addSubject ece&2&EST102&Programming In C</code>`
if (!(parameters.length >= 4)) {
ctx.replyWithHTML('⚠️ ' + helpText)
} else {
let _department = parameters[0]
let _semester = parameters[1]
let _subjectCode = parameters[2]
parameters.shift() //remove subjectCode
parameters.shift() //remove modules
parameters.shift() //remove type
let _subjetcName = parameters.join('&') // join full name
if (!['ce', 'mech', 'eee', 'ece', 'cse', 'ce', 'archi'].includes(_department)) {
ctx.replyWithHTML("⚠️ Department code not found\n\n" + helpText)
} else if (!(_semester > 0 && _semester < 9)) {
ctx.replyWithHTML("⚠️ Invalid semester\n\n" + helpText)
} else {
uploadSubject[ctx.update.message.message_id] = {department: _department, semester: _semester, subjectCode: _subjectCode, subjectName: _subjetcName}
ctx.replyWithHTML(`<b>New subject is ready add</b>\n
<b>Department:</b> ${_department} - ${codeToName(_department)}
<b>Semester:</b> ${_semester}
<b>Subject Code:</b> ${_subjectCode}
<b>Name:</b> ${_subjetcName}`, {
reply_markup: {
inline_keyboard: [
[{ text: "❌ Cancel", callback_data: "deleteMsg" }, { text: "⬆️ Add", callback_data: "addSubject_" + ctx.update.message.message_id }]
]
}
})
}
}
} else {
ctx.reply('⚠️ You don\'t have permission to perform this task. For assistance, contact your account admin')
}
})
//Add New Material
bot.command('addMaterial', (ctx) => {
let isAdmin = admins.find(admin => admin.chatId == ctx.chat.id)
if (isAdmin) {
let text = ctx.update.message.text
text = text.replace('/addMaterial ', '')
let parameters = text.split('&')
let helpText = `Send like
<code>/addMaterial subjectCode&moduels&type&name</code>
<b>subjectCode</b>: Subject in capital letters
<b>modules</b>: Modules sperated by comma(,)
<b>type</b>: Type of the material
CN - Class Note
PN - Printed Note
TB - Text Book
QP - Question Paper
V - Video link
<b>name</b>: Name of the material
eg: <code>/addMaterial HUT200&1,2&CN&Module 2 Class Note</code>`
if (!(parameters.length >= 4)) {
ctx.replyWithHTML('⚠️ ' + helpText)
} else {
if (ctx.update.message.reply_to_message) {
let subjectCode = parameters[0]
let modules = parameters[1].split(',')
let type = parameters[2]
parameters.shift() //remove subjectCode
parameters.shift() //remove modules
parameters.shift() //remove type
let name = parameters.join('&') // join full name
let validModules = () => {
let _err = false
modules.forEach(module => {
_err = _err | module < 0
_err = _err | module > 5
})
return !_err
}
if (!(subjectsData.find(m => m.code == subjectCode))) {
ctx.replyWithHTML("⚠️ Subject code not found\nCheck subject code or update DB\n\n" + helpText)
} else if(!(type == 'CN' || type == 'PN' || type == 'TB' || type == 'QP' || type == 'V')) {
ctx.replyWithHTML("⚠️ Wrong type\n\n" + helpText)
} else if(!(validModules())) {
ctx.replyWithHTML("⚠️ Wrong modules\n\n" + helpText)
} else if (ctx.update.message.reply_to_message.document) {
let content = ctx.update.message.reply_to_message.document.file_id;
uploadMaterials[ctx.update.message.message_id] = {subjectCode, modules, type, name, content}
let typeName = {CN:"✍️ Class Note", PN:"📄 Printed Note", TB:"📚 Text Book", QP:"📄 Q Paper", V:"🎞 Video"}
ctx.replyWithHTML(`<b>Your study material is ready upload</b>\n
<b>Name:</b> ${name}
<b>Suject:</b> ${subjectCode + ' - ' + subjectsData.find(m => m.code == subjectCode).name}
<b>Modules:</b> ${modules}
<b>Type:</b> ${typeName[type]}
<b>Content:</b>\n${ctx.update.message.reply_to_message.document.file_name}\n${content}`, {
reply_markup: {
inline_keyboard: [
[{ text: "❌ Cancel", callback_data: "deleteMsg" }, { text: "⬆️ Upload", callback_data: "addMaterial_" + ctx.update.message.message_id }]
]
}
})
} else {
let content = ctx.update.message.reply_to_message.text;
let typeName = {CN:"✍️ Class Note", PN:"📄 Printed Note", TB:"📚 Text Book", QP:"📄 Q Paper", V:"🎞 Video"}
type = 'V'
uploadMaterials[ctx.update.message.message_id] = {subjectCode, modules, type, name, content}
ctx.replyWithHTML(`<b>Your study material is ready upload</b>\n
<b>Name:</b> ${name}
<b>Suject:</b> ${subjectCode + ' - ' + subjectsData.find(m => m.code == subjectCode).name}
<b>Modules:</b> ${modules}
<b>Type:</b> ${typeName[type]}
<b>Content:</b>\n${content}`, {
reply_markup: {
inline_keyboard: [
[{ text: "❌ Cancel", callback_data: "deleteMsg" }, { text: "⬆️ Upload", callback_data: "addMaterial_" + ctx.update.message.message_id }]
]
}
})
}
} else {
ctx.reply("⚠️ This command works only for reply.\nReply to a document or text")
}
}
} else {
ctx.telegram.sendMessage(ctx.chat.id, '⚠️ You don\'t have permission to perform this task. For assistance, contact your account admin')
}
})
/* ADMIN */
//Testing
bot.on('document', (ctx) => {
// ctx.telegram.sendMessage(ctx.chat.id, ctx.update.message.document.file_id)
console.log(JSON.stringify(ctx.update.message.document))
// console.log(ctx);
})
updateData();
//shrouded-brushlands-98310
//https://shrouded-brushlands-98310.herokuapp.com/
/* PRODUCTION START */
module.exports = bot;
/* PRODUCTION END */
/* TEST START */
// bot.launch();
/* TEST END */