From 0cdcc28d3eb244e4d38be2ecdec67d1eeb71d86e Mon Sep 17 00:00:00 2001 From: Lucas Gabriel <90426410+lucmsilva651@users.noreply.github.com> Date: Sun, 2 Jun 2024 22:01:52 +0000 Subject: [PATCH] Implemented SpamWatch blocklist --- .gitignore | 3 ++- README.md | 2 ++ main.js | 3 ++- spamwatch.js | 28 ++++++++++++++++++++++++++++ sw_api.py | 12 ++++++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 spamwatch.js create mode 100644 sw_api.py diff --git a/.gitignore b/.gitignore index 778eb0d..9d0ee7d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.gpg *.asc *.txt -node_modules \ No newline at end of file +node_modules +__pycache__ \ No newline at end of file diff --git a/README.md b/README.md index 7bab3ab..7eaf7eb 100644 --- a/README.md +++ b/README.md @@ -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``. diff --git a/main.js b/main.js index a4d7b90..f88e0e5 100644 --- a/main.js +++ b/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; } diff --git a/spamwatch.js b/spamwatch.js new file mode 100644 index 0000000..961b354 --- /dev/null +++ b/spamwatch.js @@ -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 }; diff --git a/sw_api.py b/sw_api.py new file mode 100644 index 0000000..105e5b0 --- /dev/null +++ b/sw_api.py @@ -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')