Skip to content

Commit 1a8848b

Browse files
authored
Merge pull request #20 from kb2abot/spam-detect
(#18) added spam checking
2 parents 0c29f9f + 2648f3a commit 1a8848b

File tree

5 files changed

+109
-8
lines changed

5 files changed

+109
-8
lines changed

main/deploy/kb2abot.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const os = require("os");
22
const login = require("facebook-chat-api");
3+
const storageModel = require("./storage-model");
34

45
// import all plugin into pluginManager
56
const pluginManager = new kb2abot.helpers.PluginManager(kb2abot.plugins);
@@ -8,6 +9,7 @@ kb2abot.account = new Account({
89
id: kb2abot.id
910
});
1011
kb2abot.utils.DATASTORE.load();
12+
kb2abot.account.storage = Object.assign(storageModel.account, kb2abot.account.storage);
1113
console.newLogger.success(`Loaded datastore ${kb2abot.id}.json!`);
1214
setInterval(kb2abot.utils.DATASTORE.save, 5000);
1315

@@ -20,7 +22,13 @@ const fn = async function(err, message) {
2022
const group = kb2abot.account.addGroup(message.threadID, kb2abot.id);
2123
const member = group.addMember(message.senderID, message.threadID);
2224

23-
if (!group.storage.prefix) group.storage.prefix = "/";
25+
group.storage = Object.assign(storageModel.group, group.storage);
26+
member.storage = Object.assign(storageModel.member, member.storage);
27+
28+
console.log(group.storage);
29+
30+
if (Date.now() <= group.storage.blockTime)
31+
return;
2432

2533
if (message.body.toLowerCase() == "prefix") { // kiem tra prefix
2634
return api.sendMessage(
@@ -60,8 +68,8 @@ const fn = async function(err, message) {
6068
message
6169
);
6270
} catch (e) {
63-
console.newLogger.error(e);
64-
api.sendMessage("Lỗi: " + e.message, message.threadID);
71+
console.newLogger.error(e.stack);
72+
api.sendMessage(e.stack, message.threadID);
6573
}
6674
};
6775

@@ -92,8 +100,10 @@ const fn = async function(err, message) {
92100

93101
module.exports = appState => {
94102
login({appState}, function(err, api) {
95-
if (err)
96-
return console.newLogger.error(JSON.stringify(err));
103+
if (err) {
104+
console.newLogger.error(JSON.stringify(err));
105+
process.exit();
106+
}
97107
api.listenMqtt(
98108
fn.bind({
99109
api

main/deploy/storage-model.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
account: {
3+
4+
},
5+
group: {
6+
prefix: "/",
7+
blockTime: 0
8+
},
9+
member: {
10+
11+
}
12+
};

main/plugins/autoreply.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {botengines, fixEngineName} = kb2abot.utils.autoreply;
2-
const {getParam} = kb2abot.utils;
2+
const {getParam, constrain} = kb2abot.utils;
33
const keywords = ["autoreply", "auto"];
44

55
module.exports = {
@@ -24,8 +24,24 @@ module.exports = {
2424
},
2525

2626
onMessage: async function(api, message) {
27-
this.groupStorage.engine &&
28-
botengines[this.groupStorage.engine](api, message);
27+
if (!this.groupStorage.engine) return;
28+
if (!this.groupStorage.spamScore)
29+
this.groupStorage.spamScore = 0;
30+
if (!this.groupStorage.lastMessage)
31+
this.groupStorage.lastMessage = 0;
32+
const diff = Date.now() - this.groupStorage.lastMessage;
33+
if (diff <= 1500)
34+
this.groupStorage.spamScore += (2000 - diff)/1000;
35+
else
36+
this.groupStorage.spamScore -= (diff - 800)/1000;
37+
this.groupStorage.spamScore = constrain(this.groupStorage.spamScore, 0, 10);
38+
if (this.groupStorage.spamScore >= 10) {
39+
api.sendMessage("Too frequent! Blocking group for 5 mins...", message.threadID);
40+
this.group.storage.blockTime = Date.now() + 1000 * 60 * 5;
41+
this.groupStorage.spamScore = 0;
42+
}
43+
this.groupStorage.lastMessage = Date.now();
44+
botengines[this.groupStorage.engine](api, message);
2945
},
3046

3147
onCall: async function(api, message) {

main/plugins/spamdetect.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { constrain } = kb2abot.utils;
2+
3+
module.exports = {
4+
authorDetails: {
5+
name: "khoakomlem",
6+
contact: "fb.com/khoakomlem"
7+
},
8+
9+
friendlyName: "Kiểm tra spam",
10+
11+
keywords: ["spamdetect"],
12+
13+
description: "Block những group đang spam tin nhắn tới bot",
14+
15+
extendedDescription: "",
16+
17+
hideFromHelp: true,
18+
19+
disable: false,
20+
21+
onLoad: async function() {
22+
},
23+
24+
onMessage: async function(api, message) {
25+
if (message.body.indexOf(this.group.storage.prefix) != 0)
26+
return;
27+
if (!this.groupStorage.spamScore)
28+
this.groupStorage.spamScore = 0;
29+
if (!this.groupStorage.lastMessage)
30+
this.groupStorage.lastMessage = 0;
31+
const diff = Date.now() - this.groupStorage.lastMessage;
32+
if (diff <= 1500)
33+
this.groupStorage.spamScore += (2000 - diff)/1000;
34+
else
35+
this.groupStorage.spamScore -= (diff - 800)/1000;
36+
this.groupStorage.spamScore = constrain(this.groupStorage.spamScore, 0, 10);
37+
if (this.groupStorage.spamScore >= 10) {
38+
api.sendMessage("Too frequent! Blocking group for 5 mins...", message.threadID);
39+
this.group.storage.blockTime = Date.now() + 1000 * 60 * 5;
40+
this.groupStorage.spamScore = 0;
41+
}
42+
this.groupStorage.lastMessage = Date.now();
43+
},
44+
45+
onCall: async function(api, message) {
46+
}
47+
};

main/utils/COMMON.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ const execShellCommand = cmd => {
4747
});
4848
});
4949
};
50+
/**
51+
* Ràng buộc 1 giá trị trong khoảng từ [left; right]
52+
* @param {Number} value Giá trị vào
53+
* @param {Number} left ràng buộc trái
54+
* @param {Number} right ràng buộc phải
55+
* @return {Number} Giá trị trong khoảng [left; right]
56+
* @example
57+
* console.log(kb2abot.utils.constrain(5, 1, 10);
58+
* // 5
59+
* console.log(kb2abot.utils.constrain(-1, 1, 10);
60+
* // 1
61+
*/
62+
const constrain = (value, left, right) => {
63+
return value >= left ? (value <= right ? value : right) : left;
64+
};
5065
/**
5166
* Làm tròn đến chữ số thập phân x
5267
* @param {Number} number Số bạn muốn làm tròn
@@ -300,6 +315,7 @@ module.exports = {
300315
extend,
301316
subname,
302317
parseArgs,
318+
constrain,
303319
parseJSON,
304320
asyncWait,
305321
execShellCommand,

0 commit comments

Comments
 (0)