Implemented SpamWatch blocklist

This commit is contained in:
Lucas Gabriel 2024-06-02 22:01:52 +00:00
parent 1bc99afe87
commit 0cdcc28d3e
5 changed files with 46 additions and 2 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
*.gpg
*.asc
*.txt
node_modules
node_modules
__pycache__

View File

@ -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``.

View File

@ -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
View 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 };

12
sw_api.py Normal file
View File

@ -0,0 +1,12 @@
import os
from dotenv import load_dotenv
import spamwatch
load_dotenv("config.env")
client = spamwatch.Client(os.getenv('SW_KEY'))
bans = client.get_bans_min()
with open('sw_blocklist.txt', 'w') as file:
for ban in bans:
file.write(f'{ban}\n')