-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
executable file
·138 lines (118 loc) · 5.13 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
const config = require('./config')
var MongoClient = require('mongodb').MongoClient
const TelegramBot = require('node-telegram-bot-api')
var mongoGroups
MongoClient.connect(config.mongo_connection).then(function (db) { // first - connect to database
mongoGroups = db.collection('groups')
}).then(function (res) {
const bot = new TelegramBot(config.bot_token, { polling: true }) // second - start bot
bot.onText(/\/config/, function (msg, match) { // request configuration keyboard to PM
if (msg.chat.type == 'supergroup') {
bot.getChatAdministrators(msg.chat.id).then((admins) => { // get list of admins
if (admins.filter(x => x.user.id == msg.from.id).length > 0) { // if sender is admin
getConfigKeyboard(msg.chat.id).then(kbd => { // prepare keyboard
bot.sendMessage(msg.from.id, `*${msg.chat.title}*`, { // and sent it
parse_mode: "markdown",
reply_markup: kbd
})
})
}
})
} else if (msg.chat.type == 'private') {
bot.sendMessage(msg.chat.id, "You sould use this command in supergroups that you want to configure")
}
})
// delete messages according on filters
bot.on('message', (msg) => {
if (msg.chat.type != 'supergroup') {
return //we can delete messages only from supergroups
}
mongoGroups.findOne({ groupId: msg.chat.id }).then(res => { // load group configuration
msg.cfg = res
if (isJoinedMessage(msg) || isArabicMessage(msg) || isUrlMessage(msg)) {
bot.deleteMessage(msg.chat.id, msg.message_id)
}
})
console.dir(msg) // debug output
})
// buttons response in menu
bot.on('callback_query', query => {
let groupId = Number(query.data.split("#")[0])
let prop = query.data.split("#")[1] // get info from button
mongoGroups.findOne({ groupId: groupId }).then(g => {
let val = !g[prop] // switch selected button
mongoGroups.updateOne({ groupId: groupId }, { $set: { [prop]: val } }).then(() => { // store switched value
bot.answerCallbackQuery({
callback_query_id: query.id
}).then((cb) => {
getConfigKeyboard(groupId).then(kbd => { // update keyboard
bot.editMessageReplyMarkup(kbd, {
chat_id: query.message.chat.id,
message_id: query.message.message_id
})
})
})
})
})
})
// checks if message is 'user joined/left to jroup'
let isJoinedMessage = function (msg) {
return (msg.cfg && msg.cfg.joinedMsg) && (msg.new_chat_member !== undefined || msg.left_chat_member !== undefined)
}
// checks if message contains arabic symbols
let isArabicMessage = function (msg) {
return (msg.cfg && msg.cfg.arabicMsg) && /[\u0600-\u06FF]/.test(msg.text) // testing on 'arabic' regex
}
// cheks if message contains url
let isUrlMessage = function (msg) {
if (!(msg.cfg && msg.cfg.urlMsg)) {
return false
}
if (msg.entities) {
for (let e of msg.entities) {
if (e.type == 'url') { // url messages have type 'url'
return true
}
}
}
return false
}
let getConfigKeyboard = function (chatId) { // prepare config keyboard
let getSetOfKeys = function (groupConfig) {
return {
inline_keyboard: [
[{
text: `${groupConfig.joinedMsg ? "✔️" : "❌"} | delete 'joined' messages`,
callback_data: `${groupConfig.groupId}#joinedMsg`
}], [{
text: `${groupConfig.arabicMsg ? "✔️" : "❌"} | delete arabic messages`,
callback_data: `${groupConfig.groupId}#arabicMsg`
}], [{
text: `${groupConfig.urlMsg ? "✔️" : "❌"} | delete messages with urls`,
callback_data: `${groupConfig.groupId}#urlMsg`
}]
]
}
}
return new Promise(function (resolve, rej) {
mongoGroups.findOne({ groupId: chatId }).then(res => {
if (res == undefined || res.length == 0) {
let g = new GroupConfig(chatId)
mongoGroups.insertOne(g).then(() => { resolve(getSetOfKeys(g)) })
} else {
resolve(getSetOfKeys(res))
}
})
})
}
}).catch(function (e) {
console.log("FATAL :: " + e)
})
class GroupConfig {
constructor(id) {
this.joinedMsg = false
this.arabicMsg = false
this.urlMsg = false
this.groupId = id
}
}