-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
96 lines (79 loc) · 3.14 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
//Import required modules
require('dotenv').config();
const fs = require('fs');
const readline = require('readline');
const TelegramBot = require('node-telegram-bot-api')
//Insert token here
const token = 'insert token here';
//Connect to Telegram servers(add proxy if you are in restricted countries like russia or iran or use System-wide proxy/VPN)
const bot = new TelegramBot(token, {polling: true});
//Connect bot with web
const express = require('express');
const app = express();
const port = 3000;
//Read blacklist.txt
function readf(){
var array = fs.readFileSync('blacklist.txt', 'utf8').toString().replace(/\r\n/g,'\n').split('\n');
return array;
}
//Responds to command "/add <word>"
bot.onText(/\/add (.+)/, (msg, match) => {
let chat_id = msg.chat.id;
let message_id = msg.message_id;
//Checks and see if the message sender had adminstarator permissions in the group
bot.getChatMember(msg.chat.id, msg.from.id).then(function(data) {
if ((data.status == "admin")) {
const newword = match[1];
//Append word to last line of the the file
fs.appendFile('blacklist.txt', '\r\n'+newword, function (err) {
if (err) throw err;
});
//Reads the Blacklist into arr again
let arr = readf();
//Remove the command and send a varification message
bot.deleteMessage(chat_id, message_id);
bot.sendMessage(chat_id,'Successfully added word ' + newword + ' to blacklist.');
}else{
bot.deleteMessage(chat_id, message_id);
bot.sendMessage(message.chat.id, "You do not have permission to do that!");
}
});
});
bot.on('message', (msg) => {
//Loads blacklist.txt words into arr
let arr = readf();
let chat_id = msg.chat.id;
let message_id = msg.message_id;
let arrayLength = arr.length;
let date_ob = new Date();
// current date
// adjust 0 before single digit date
let date = ("0" + date_ob.getDate()).slice(-2);
// current month
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
// current year
let year = date_ob.getFullYear();
// current hours
let hours = date_ob.getHours();
// current minutes
let minutes = date_ob.getMinutes();
// current seconds
let seconds = date_ob.getSeconds();
for (let i = 0; i < arrayLength; i++) {
//Scan for blacklisted words in messages
if (msg.text.toLowerCase().includes(arr[i])) {
const alert = "[" + year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds + "] Message with '" + arr[i] + "' was removed.";
console.log(alert);
const deletedmessage = "[" + year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds + "] \n" + msg.text;
bot.deleteMessage(chat_id, message_id);
fs.appendFile('logs.txt', '\r\n'+deletedmessage+'\n', function (err) {
if (err) throw err;
});
break;
}
}
}
)
//Return html respond
app.get('/', (req, res) => res.send('sk bot is online'));
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}\n`));