-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
119 lines (94 loc) · 2.62 KB
/
server.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
// REQUIREMENTS
const dotenv = require('dotenv');
const express = require('express');
const Telegraf = require('telegraf');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
// DOTENV SETUP
dotenv.config();
// DATABASE SETUP
const adapter = new FileSync('.data/db.json');
const db = low(adapter);
const defaultEntries = {
entries: [],
};
db.defaults(defaultEntries).write();
// WEBSERVER SETUP
const app = express();
app.use(express.static('public'));
app.get('/', (request, response) => {
response.sendFile(__dirname + '/views/index.html');
});
app.get('/entries', (request, response) => {
const entries = db
.get('entries')
.sortBy('moduleCode')
.value();
const result = entries.map((entry) => {
return { moduleCode: entry.moduleCode, chatUrl: entry.chatUrl };
});
response.send(result);
});
app.get('/entries/:moduleCode', (request, response) => {
const moduleCode = request.params.moduleCode;
const entry = db
.get('entries')
.find({ moduleCode: moduleCode })
.value();
const result = entry === undefined ? 'Not Found' : entry.chatUrl;
response.send(result);
});
app.listen(process.env.PORT);
// CHATBOT SETUP
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.telegram.getMe().then((botInfo) => {
bot.options.username = botInfo.username;
});
bot.start(async (ctx) => {
if (ctx.chat.title === undefined) {
return ctx.reply('Opps, maybe try again? Did you make me an admin?');
}
if (!ctx.chat.title.match('^[A-Z0-9]+ Discussion$')) {
return ctx.reply(
'Your group name is invalid. Ensure that it is in the form of "CS1231 Discussion".',
);
}
if (ctx.chat.type !== 'supergroup') {
return ctx.reply(
'Your group is not a supergroup. Please convert it into one.',
);
}
const moduleCode = ctx.chat.title.split(' ')[0];
const chatIdExist =
db
.get('entries')
.find({ chatId: ctx.chat.id })
.size() > 0;
const moduleCodeExist =
db
.get('entries')
.find({ moduleCode: moduleCode })
.size() > 0;
if (chatIdExist || moduleCodeExist) {
return ctx.reply('This chat has already been registered');
}
ctx
.exportChatInviteLink()
.then((chatUrl) => {
db.get('entries')
.push({ chatId: ctx.chat.id, chatUrl: chatUrl, moduleCode: moduleCode })
.write();
ctx.reply('Chat successfully registered');
})
.catch((err) => {
console.log(err);
});
});
bot.on(['new_chat_members', 'left_chat_member'], (ctx) => {
try {
ctx.deleteMessage(ctx.message.message_id);
} catch (err) {
console.log(err);
}
});
bot.startPolling();