Skip to content

Commit

Permalink
(#18) added spam checking
Browse files Browse the repository at this point in the history
  • Loading branch information
khoakomlem committed Jan 9, 2021
1 parent 0c29f9f commit 2648f3a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 8 deletions.
20 changes: 15 additions & 5 deletions main/deploy/kb2abot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const os = require("os");
const login = require("facebook-chat-api");
const storageModel = require("./storage-model");

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

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

if (!group.storage.prefix) group.storage.prefix = "/";
group.storage = Object.assign(storageModel.group, group.storage);
member.storage = Object.assign(storageModel.member, member.storage);

console.log(group.storage);

if (Date.now() <= group.storage.blockTime)
return;

if (message.body.toLowerCase() == "prefix") { // kiem tra prefix
return api.sendMessage(
Expand Down Expand Up @@ -60,8 +68,8 @@ const fn = async function(err, message) {
message
);
} catch (e) {
console.newLogger.error(e);
api.sendMessage("Lỗi: " + e.message, message.threadID);
console.newLogger.error(e.stack);
api.sendMessage(e.stack, message.threadID);
}
};

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

module.exports = appState => {
login({appState}, function(err, api) {
if (err)
return console.newLogger.error(JSON.stringify(err));
if (err) {
console.newLogger.error(JSON.stringify(err));
process.exit();
}
api.listenMqtt(
fn.bind({
api
Expand Down
12 changes: 12 additions & 0 deletions main/deploy/storage-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
account: {

},
group: {
prefix: "/",
blockTime: 0
},
member: {

}
};
22 changes: 19 additions & 3 deletions main/plugins/autoreply.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {botengines, fixEngineName} = kb2abot.utils.autoreply;
const {getParam} = kb2abot.utils;
const {getParam, constrain} = kb2abot.utils;
const keywords = ["autoreply", "auto"];

module.exports = {
Expand All @@ -24,8 +24,24 @@ module.exports = {
},

onMessage: async function(api, message) {
this.groupStorage.engine &&
botengines[this.groupStorage.engine](api, message);
if (!this.groupStorage.engine) return;
if (!this.groupStorage.spamScore)
this.groupStorage.spamScore = 0;
if (!this.groupStorage.lastMessage)
this.groupStorage.lastMessage = 0;
const diff = Date.now() - this.groupStorage.lastMessage;
if (diff <= 1500)
this.groupStorage.spamScore += (2000 - diff)/1000;
else
this.groupStorage.spamScore -= (diff - 800)/1000;
this.groupStorage.spamScore = constrain(this.groupStorage.spamScore, 0, 10);
if (this.groupStorage.spamScore >= 10) {
api.sendMessage("Too frequent! Blocking group for 5 mins...", message.threadID);
this.group.storage.blockTime = Date.now() + 1000 * 60 * 5;
this.groupStorage.spamScore = 0;
}
this.groupStorage.lastMessage = Date.now();
botengines[this.groupStorage.engine](api, message);
},

onCall: async function(api, message) {
Expand Down
47 changes: 47 additions & 0 deletions main/plugins/spamdetect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { constrain } = kb2abot.utils;

module.exports = {
authorDetails: {
name: "khoakomlem",
contact: "fb.com/khoakomlem"
},

friendlyName: "Kiểm tra spam",

keywords: ["spamdetect"],

description: "Block những group đang spam tin nhắn tới bot",

extendedDescription: "",

hideFromHelp: true,

disable: false,

onLoad: async function() {
},

onMessage: async function(api, message) {
if (message.body.indexOf(this.group.storage.prefix) != 0)
return;
if (!this.groupStorage.spamScore)
this.groupStorage.spamScore = 0;
if (!this.groupStorage.lastMessage)
this.groupStorage.lastMessage = 0;
const diff = Date.now() - this.groupStorage.lastMessage;
if (diff <= 1500)
this.groupStorage.spamScore += (2000 - diff)/1000;
else
this.groupStorage.spamScore -= (diff - 800)/1000;
this.groupStorage.spamScore = constrain(this.groupStorage.spamScore, 0, 10);
if (this.groupStorage.spamScore >= 10) {
api.sendMessage("Too frequent! Blocking group for 5 mins...", message.threadID);
this.group.storage.blockTime = Date.now() + 1000 * 60 * 5;
this.groupStorage.spamScore = 0;
}
this.groupStorage.lastMessage = Date.now();
},

onCall: async function(api, message) {
}
};
16 changes: 16 additions & 0 deletions main/utils/COMMON.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ const execShellCommand = cmd => {
});
});
};
/**
* Ràng buộc 1 giá trị trong khoảng từ [left; right]
* @param {Number} value Giá trị vào
* @param {Number} left ràng buộc trái
* @param {Number} right ràng buộc phải
* @return {Number} Giá trị trong khoảng [left; right]
* @example
* console.log(kb2abot.utils.constrain(5, 1, 10);
* // 5
* console.log(kb2abot.utils.constrain(-1, 1, 10);
* // 1
*/
const constrain = (value, left, right) => {
return value >= left ? (value <= right ? value : right) : left;
};
/**
* Làm tròn đến chữ số thập phân x
* @param {Number} number Số bạn muốn làm tròn
Expand Down Expand Up @@ -300,6 +315,7 @@ module.exports = {
extend,
subname,
parseArgs,
constrain,
parseJSON,
asyncWait,
execShellCommand,
Expand Down

0 comments on commit 2648f3a

Please sign in to comment.