forked from bonnak/telegram_bot_for_admins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·220 lines (200 loc) · 8.05 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
// Import modules
const TelegramBot = require('node-telegram-bot-api');
const MongoCollections = require('./lib/MongoCollections');
const filterReducer = require('./lib/filters').filterReducer;
const Command = require('./lib/commands');
const CommonFunctions = require('./lib/commonFunctions');
const token = process.env.BOT_TOKEN || require('./config').bot_token;
const database = require('./lib/db');
const api = require('./api/app');
api.serve();
const actionTypes = {
command: 'COMMAND',
deleteConfigMessage: 'DELETE_CONFIG_MESSAGE',
deleteFilteredMessage: 'DELETE_FILTERED_MESAGE',
groupConfiguratiuon: 'GROUP_CONFIGURATION',
configWithNotEnoughtRights: 'TRYING_TO_CONFIGURE_GROUP_WITH_NOT_ENOUGHT_RIGHTS',
tryingConfigureInPrivate: 'SENDING_CONFIG_PM',
tryingConfigureNormalGroup: 'TRYING_TO_CONFIGURE_NORMAL_GROUP',
setHello: 'SET_HELLO_MESSAGE',
expiredConfigSession: 'CONFIG_SESSION_EXPIRED',
start: 'START_COMMAND',
help: 'HELP_COMMAND',
hello: 'HELLO_MESSAGE',
keyboardCallback: 'KEYBOARD_CALLBACK',
restrictingSpammer: 'RESTRICTING_SPAMMER',
log: 'VIEWING_LOG',
whitelistView: 'VIEWING_WHITELIST',
whitelistAdding: 'ADDING_LINKS_TO_WHITELIST',
whitelistNoLinksProvided: 'NO_LINKS_PROVIDED_TO_WHITELIST',
whitelistClear: 'CLEAR_WHITELIST',
whitelistRemoveLinks: 'REMOVE_LINKS_FROM_WHITELIST'
};
let options = {};
if (process.env.APP_URL) {
console.log('using webhooks, ' + process.env.APP_URL);
options = {
webHook: {
port: process.env.PORT
}
};
}
else {
console.log('using longpoll');
options = {
polling: {
autoStart: false
}
};
}
const bot = new TelegramBot(token, options);
const mongoCollections = new MongoCollections();
database.db(function (db) {
mongoCollections.mongoGroups = db.collection('groups');
mongoCollections.mongoMessages = db.collection('messagesLog');
mongoCollections.mongoNowConfigatates = db.collection('nowConfigurates');
mongoCollections.mongoActionLog = db.collection('actionLog');
mongoCollections.mongoWarns = db.collection('warns');
mongoCollections.mongoWhiteList = db.collection('mongoWhiteList');
mongoCollections.mongoGroupMembers = db.collection('members');
mongoCollections.mongoUserGroups = db.collection('userGroups');
mongoCollections.mongoMessages.createIndex({ postedDate: 1 }, { expireAfterSeconds: 60 * 60 * 24 * 60 }) //store messages for 60 days
.then(async () => {
let url = process.env.APP_URL;
//me = await bot.getMe()
if (url) {
console.log('hookin');
bot.setWebHook(`${url}/bot${token}`);
} else {
console.log('pollin');
bot.startPolling();
}
});
mongoCollections.mongoGroupMembers.createIndex({'userid': 1, 'groupId': 1}, {unique: true});
mongoCollections.mongoUserGroups.createIndex({'user': 1, 'group.id': 1}, {unique: true});
});
const command = new Command(log, actionTypes, bot, mongoCollections);
const commonFunctions = new CommonFunctions(bot);
subscribeToBotEvents();
//check if user sends messages too frequently
async function checkIfSpam(msg) {
let entry = { postedDate: { $gte: commonFunctions.secondsAgo(30) }, 'message.from.id': msg.from.id, 'message.chat.id': msg.chat.id };
let count = await mongoCollections.mongoMessages.count(entry);
if (count > 10)
restrictSpammer(msg);
}
function log(eventType, payload) {
mongoCollections.mongoActionLog.insertOne({ actionDate: new Date(), eventType, payload });
}
function restrictSpammer(msg) {
log(actionTypes.restrictingSpammer, msg);
bot.deleteMessage(msg.chat.id, msg.message_id).catch(() => { });
}
//create and format hello message
function prepareHelloMessage(cfg, msg) {
let message = '';
const name = (msg.new_chat_participant.first_name || '' + ' ' + msg.new_chat_participant.last_name || '').trim() || msg.new_chat_participant.username;
message = cfg.helloMsgString || 'Thanks for joining, *$name*. Please follow the guidelines of the group and enjoy your time';
return message.replace('$name', name);
}
async function kickByReply(msg) {
let admins = await commonFunctions.getChatAdmins(msg.chat);
if (commonFunctions.messageSenderIsAdmin(admins, msg) && !commonFunctions.messageSenderIsAdmin(admins, msg.reply_to_message)) {
await bot.kickChatMember(msg.chat.id, msg.reply_to_message.from.id);
bot.unbanChatMember(msg.chat.id, msg.reply_to_message.from.id);
}
}
async function banByReply(msg) {
let admins = await commonFunctions.getChatAdmins(msg.chat);
if (commonFunctions.messageSenderIsAdmin(admins, msg) && !commonFunctions.messageSenderIsAdmin(admins, msg.reply_to_message)) {
bot.kickChatMember(msg.chat.id, msg.reply_to_message.from.id);
}
}
//check if message need to be deleted according on filters
async function tryFilterMessage(msg) {
mongoCollections.mongoMessages.insertOne({ postedDate: new Date(), message: msg });
let cfg = await mongoCollections.mongoGroups.findOne({ groupId: msg.chat.id }); // load group configuration
if (cfg && cfg.helloMsg && msg.new_chat_member) {
log(actionTypes.hello, msg);
let helloMsg = prepareHelloMessage(cfg, msg);
let messageOptions = { parse_mode: 'markdown' };
if (!cfg.joinedMsg) {
messageOptions.reply_to_message_id = msg.message_id;
}
bot.sendMessage(msg.chat.id, helloMsg, messageOptions);
await mongoCollections.mongoGroupMembers.insertOne({
userid: msg.new_chat_member.id,
firstname: msg.new_chat_member.first_name,
lastname: msg.new_chat_member.last_name,
groupId: msg.chat.id,
joinDate: new Date()
});
}
if(cfg && msg.left_chat_member){
await mongoCollections.mongoGroupMembers.findOneAndDelete({userid: msg.left_chat_member.id});
}
let admins = await commonFunctions.getChatAdmins(msg.chat); // get list of admins
if (filterReducer(msg, cfg, admins)) {
log(actionTypes.deleteFilteredMessage, msg);
bot.deleteMessage(msg.chat.id, msg.message_id).catch(() => { });
}
if (!commonFunctions.messageSenderIsAdmin(admins, msg))
if (cfg.restrictSpam)
checkIfSpam(msg);
}
//prepair bot to interact with users
function subscribeToBotEvents() {
bot.onText(/\/config/, async function (msg) {
await command.configCommand(msg);
});
bot.onText(/^\/kick/, async (msg) => {
await kickByReply(msg);
});
bot.onText(/\/ban/, async (msg) => {
await banByReply(msg);
});
bot.onText(/\/warn/, async (msg) => {
await command.warnCommand(msg);
});
bot.onText(/\/unwarn/, async (msg) => {
await command.unwarnCommand(msg);
});
bot.onText(/^\/set_hello(\s(.*))?$/, async (msg, match) => {
await command.setHelloCommand(msg, match);
});
// Bot reaction on commands "/start"
bot.onText(/\/bossbot/, function (msg) {
command.startCommand(msg);
});
bot.onText(/\/log/, async function (msg) {
await command.logCommand(msg);
});
bot.onText(
/(\/whitelist|\/wl)(\s.*)?$/,
(msg, match) => command.whiteList(msg, match[2])
);
bot.onText(
/(\/unwhitelist|\/unwl)(\s.*)?$/,
(msg, match) => command.unWhiteList(msg, match[2])
);
// Bot reaction on commands "/help"
bot.onText(/\/help/, function (msg) {
command.helpCommand(msg);
});
bot.onText(/^\/max_length(\s(.*))?$/, async (msg, match) => {
command.maxLengthCommand(msg, match[2]);
});
bot.onText(/\/access/, function(msg){
command.accessCommand(msg)
});
// Bot reaction on any message
bot.on('message', async (msg) => {
if (msg.chat.type !== 'supergroup')
return; //we can delete messages only from supergroups
await tryFilterMessage(msg);
});
// Buttons responce in menu
bot.on('callback_query', async (query) => {
await command.menuCallback(query);
});
}