mirror of
https://github.com/abocn/TelegramBot.git
synced 2025-03-10 21:00:03 +00:00
Implemented SpamWatch blocklist
This commit is contained in:
parent
1bc99afe87
commit
0cdcc28d3e
3
.gitignore
vendored
3
.gitignore
vendored
@ -2,4 +2,5 @@
|
||||
*.gpg
|
||||
*.asc
|
||||
*.txt
|
||||
node_modules
|
||||
node_modules
|
||||
__pycache__
|
@ -25,9 +25,11 @@ Next, go to the repository directory, create a ``config.env`` file and put the c
|
||||
# get it with @BotFather on Telegram
|
||||
TGBOT_TOKEN="0000000000:AAAaaAAaaaaAaAaaAAAaaaAaaaaAAAAAaaa"
|
||||
TGBOT_ADMINS=[0000000000, 1111111111, 2222222222]
|
||||
SW_KEY="aAaAAaaAAaAA_AAAAAaaAAaaAAaaAAAAAAaaAaaAaaAAaaAAaAaAAaaAAaaAAaAaA"
|
||||
```
|
||||
- **TGBOT_TOKEN**: Put your bot token that you created at [@BotFather](https://t.me/botfather) at the variable ``TGBOT_TOKEN`` (as the example above).
|
||||
- **TGBOT_ADMINS**: Put the ID of the people responsible for managing the bot (as the example above). They can use some administrative + exclusive commands on any group.
|
||||
- **SW_KEY**: A API key to make a blocklist to banned SpamWatch users. You can refer to SpamWatch docs to create a API key for yourself.
|
||||
|
||||
After editing the file, save all changes and run the bot with ``npm start``.
|
||||
|
||||
|
3
main.js
3
main.js
@ -4,6 +4,7 @@ const path = require('path');
|
||||
const token = process.env.TGBOT_TOKEN;
|
||||
const bot = new TelegramBot(token, { polling: true });
|
||||
const { isBlocked } = require('./blocklist');
|
||||
const { isOnSpamWatch } = require('./blocklist');
|
||||
require('./logger');
|
||||
|
||||
const commandsPath = path.join(__dirname, 'commands');
|
||||
@ -21,7 +22,7 @@ bot.on('message', (msg) => {
|
||||
const messageText = msg.text;
|
||||
|
||||
if (msg.chat.type == 'private') {
|
||||
if (isBlocked(userId)) {
|
||||
if (isBlocked(userId) || isOnSpamWatch(userId)) {
|
||||
console.log(`WARN: Blocked user ${userName}, ${userId} tried to access the bot with the command or message "${messageText}".\n`);
|
||||
return;
|
||||
}
|
||||
|
28
spamwatch.js
Normal file
28
spamwatch.js
Normal file
@ -0,0 +1,28 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const blocklistPath = path.join(__dirname, 'sw_blocklist.txt');
|
||||
|
||||
let blocklist = [];
|
||||
|
||||
const readBlocklist = () => {
|
||||
try {
|
||||
const data = fs.readFileSync(blocklistPath, 'utf8');
|
||||
blocklist = data.split('\n').map(id => id.trim()).filter(id => id !== '');
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') {
|
||||
console.log('WARN: SpamWatch blocklist file not found. Creating a new, blank one.');
|
||||
fs.writeFileSync(blocklistPath, '');
|
||||
} else {
|
||||
console.error('WARN: Error reading SpamWatch blocklist:', error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const isOnSpamWatch = (userId) => {
|
||||
return blocklist.includes(String(userId));
|
||||
};
|
||||
|
||||
readBlocklist();
|
||||
|
||||
module.exports = { isOnSpamWatch };
|
Loading…
x
Reference in New Issue
Block a user