From 68766d5a6d1b4a993073dece7b8336f9b1686669 Mon Sep 17 00:00:00 2001 From: GiovaniFZ Date: Sun, 6 Apr 2025 12:30:51 -0300 Subject: [PATCH 01/21] fix: fix for username with html tags --- src/commands/gsmarena.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/commands/gsmarena.js b/src/commands/gsmarena.js index f6fce9c..db9ce08 100644 --- a/src/commands/gsmarena.js +++ b/src/commands/gsmarena.js @@ -196,7 +196,11 @@ function extractMetaData(meta, key) { module.exports = (bot) => { bot.command(['d', 'device'], spamwatchMiddleware, async (ctx) => { const userId = ctx.from.id; - const userName = ctx.from.first_name; + let userName = String(ctx.from.first_name); + + if(userName.includes("<") && userName.includes(">")) { + userName = userName.replace("<", "").replace(">", ""); + } const phone = ctx.message.text.split(" ").slice(1).join(" "); if (!phone) { From 1ddb00abfd81a2433fe7827d8c3ac19d961f1878 Mon Sep 17 00:00:00 2001 From: Luquinhas Date: Fri, 11 Apr 2025 11:02:14 -0300 Subject: [PATCH 02/21] Update gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index d1151eb..ba85ba8 100644 --- a/.gitignore +++ b/.gitignore @@ -139,7 +139,6 @@ package-lock.json bun.lock bun.lockb tmp/ -plugins/ # Executables *.exe From b80cbc0ee09eda65104a51989189e8b0ed581ab3 Mon Sep 17 00:00:00 2001 From: Luquinhas Date: Fri, 11 Apr 2025 11:33:04 -0300 Subject: [PATCH 03/21] Add workflow to update AUTHORS file at every commit --- .github/workflows/update-authors.yml | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 .github/workflows/update-authors.yml diff --git a/.github/workflows/update-authors.yml b/.github/workflows/update-authors.yml new file mode 100644 index 0000000..79e8f27 --- /dev/null +++ b/.github/workflows/update-authors.yml @@ -0,0 +1,36 @@ +name: Update AUTHORS File + +on: + push: + branches: + - main + +jobs: + update-authors: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Git config + run: | + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + + - name: Generate AUTHORS file (Name format) + run: | + git log --format='%aN <%aE>' | sort -u > AUTHORS + + - name: Check if AUTHORS file changed + run: | + if git diff --quiet AUTHORS; then + echo "No changes in AUTHORS file." + exit 0 + fi + + - name: Commit and push changes + run: | + git add AUTHORS + git commit -m "Update AUTHORS file automatically" + git push From 906722e3d93e323794e0b5b7b570d41190c81572 Mon Sep 17 00:00:00 2001 From: Luquinhas Date: Fri, 11 Apr 2025 11:35:11 -0300 Subject: [PATCH 04/21] Force use of PAT on workflow --- .github/workflows/update-authors.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/update-authors.yml b/.github/workflows/update-authors.yml index 79e8f27..2d9a65e 100644 --- a/.github/workflows/update-authors.yml +++ b/.github/workflows/update-authors.yml @@ -33,4 +33,4 @@ jobs: run: | git add AUTHORS git commit -m "Update AUTHORS file automatically" - git push + git push https://x-access-token:${{ secrets.GH_TOKEN }}@github.com/${{ github.repository }} HEAD:main From 90388f845cfce675534bb5a18b05b53826be86c9 Mon Sep 17 00:00:00 2001 From: Luquinhas Date: Fri, 11 Apr 2025 11:54:36 -0300 Subject: [PATCH 05/21] Update workflow again --- .github/workflows/update-authors.yml | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/.github/workflows/update-authors.yml b/.github/workflows/update-authors.yml index 2d9a65e..0263eae 100644 --- a/.github/workflows/update-authors.yml +++ b/.github/workflows/update-authors.yml @@ -13,11 +13,6 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - - name: Set up Git config - run: | - git config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" - - name: Generate AUTHORS file (Name format) run: | git log --format='%aN <%aE>' | sort -u > AUTHORS @@ -30,7 +25,7 @@ jobs: fi - name: Commit and push changes - run: | - git add AUTHORS - git commit -m "Update AUTHORS file automatically" - git push https://x-access-token:${{ secrets.GH_TOKEN }}@github.com/${{ github.repository }} HEAD:main + uses: EndBug/add-and-commit@v9.1.4 + add: "AUTHORS" + default_author: github_actions + message: "Update AUTHORS file automatically" From 7c41f3f83fc9f76d9e39d554677a189a9c568f66 Mon Sep 17 00:00:00 2001 From: Luquinhas Date: Fri, 11 Apr 2025 11:55:51 -0300 Subject: [PATCH 06/21] Fix workflow #2 --- .github/workflows/update-authors.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/update-authors.yml b/.github/workflows/update-authors.yml index 0263eae..8956d41 100644 --- a/.github/workflows/update-authors.yml +++ b/.github/workflows/update-authors.yml @@ -25,7 +25,8 @@ jobs: fi - name: Commit and push changes - uses: EndBug/add-and-commit@v9.1.4 - add: "AUTHORS" - default_author: github_actions - message: "Update AUTHORS file automatically" + with: + uses: EndBug/add-and-commit@v9.1.4 + add: "AUTHORS" + default_author: github_actions + message: "Update AUTHORS file automatically" From ce5a0e4752216d897b43178578d46f421da29730 Mon Sep 17 00:00:00 2001 From: Luquinhas Date: Fri, 11 Apr 2025 11:56:28 -0300 Subject: [PATCH 07/21] Fix semantics --- .github/workflows/update-authors.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/update-authors.yml b/.github/workflows/update-authors.yml index 8956d41..246da04 100644 --- a/.github/workflows/update-authors.yml +++ b/.github/workflows/update-authors.yml @@ -25,8 +25,8 @@ jobs: fi - name: Commit and push changes + uses: EndBug/add-and-commit@v9.1.4 with: - uses: EndBug/add-and-commit@v9.1.4 add: "AUTHORS" default_author: github_actions message: "Update AUTHORS file automatically" From 35ba88a852ccfcf4a47dbd9bac6a56c5d34bd5c2 Mon Sep 17 00:00:00 2001 From: Luquinhas Date: Fri, 11 Apr 2025 12:03:50 -0300 Subject: [PATCH 08/21] Add write perms --- .github/workflows/update-authors.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/update-authors.yml b/.github/workflows/update-authors.yml index 246da04..a740588 100644 --- a/.github/workflows/update-authors.yml +++ b/.github/workflows/update-authors.yml @@ -8,6 +8,8 @@ on: jobs: update-authors: runs-on: ubuntu-latest + permissions: + contents: write steps: - name: Checkout repository @@ -27,6 +29,7 @@ jobs: - name: Commit and push changes uses: EndBug/add-and-commit@v9.1.4 with: + push: true add: "AUTHORS" default_author: github_actions message: "Update AUTHORS file automatically" From 91484732ee5ab7abc3e21f3939faa52588e6b7a2 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 11 Apr 2025 15:04:08 +0000 Subject: [PATCH 09/21] Update AUTHORS file automatically --- AUTHORS | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/AUTHORS b/AUTHORS index 1348bed..a5c75e1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,14 +1 @@ -A Bunch of Computer Nerds -A Bunch of Computer Nerds -A Bunch of Computer Nerds -DaviDev <97841570+DaviisDev@users.noreply.github.com> -dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> -fossabot -Giovani Finazzi <53719063+GiovaniFZ@users.noreply.github.com> -GiovaniFZ -Lucas Gabriel <90426410+lucmsilva651@users.noreply.github.com> -Lucas Gabriel -lucmsilva651 -Luquinhas Luquinhas -mthlma <156229140+mthlma@users.noreply.github.com> From d4692d45cbc306bcd1cab502fa4f02cf694ec7d2 Mon Sep 17 00:00:00 2001 From: Luquinhas Date: Fri, 11 Apr 2025 12:05:58 -0300 Subject: [PATCH 10/21] Pick all depths for checkout --- .github/workflows/update-authors.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/update-authors.yml b/.github/workflows/update-authors.yml index a740588..ae7f058 100644 --- a/.github/workflows/update-authors.yml +++ b/.github/workflows/update-authors.yml @@ -14,6 +14,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v3 + with: + fetch-depth: 0 - name: Generate AUTHORS file (Name format) run: | From 7750b522abedad172030542dc1033418c618cb5f Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 11 Apr 2025 15:06:14 +0000 Subject: [PATCH 11/21] Update AUTHORS file automatically --- AUTHORS | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/AUTHORS b/AUTHORS index a5c75e1..352a121 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1 +1,15 @@ +A Bunch of Computer Nerds +A Bunch of Computer Nerds +A Bunch of Computer Nerds +DaviDev <97841570+DaviisDev@users.noreply.github.com> +Giovani Finazzi <53719063+GiovaniFZ@users.noreply.github.com> +GiovaniFZ +Lucas Gabriel <90426410+lucmsilva651@users.noreply.github.com> +Lucas Gabriel +Luquinhas Luquinhas +dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> +fossabot +github-actions <41898282+github-actions[bot]@users.noreply.github.com> +lucmsilva651 +mthlma <156229140+mthlma@users.noreply.github.com> From 3f71d0c8ca5dea2eeb682c4e0d5751acd1697e1f Mon Sep 17 00:00:00 2001 From: Aidan Date: Mon, 14 Apr 2025 00:57:04 -0400 Subject: [PATCH 12/21] docs: linting and convert bullet point to tip --- README.md | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 25ffa5b..1ba7feb 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,40 @@ # Kowalski (Node.js Telegram Bot) + [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md) ![GitHub License](https://img.shields.io/github/license/ABOCN/TelegramBot) Kowalski is a a simple Telegram bot made in Node.js. - - You can find Kowalski at [@KowalskiNodeBot](https://t.me/KowalskiNodeBot) on Telegram. + +- You can find Kowalski at [@KowalskiNodeBot](https://t.me/KowalskiNodeBot) on Telegram. ## Self-host requirements - - Node.js 20 or newer (you can also use Bun) - - A Telegram bot (create one at [@BotFather](https://t.me/botfather)) - - Latest version of Node.js - - FFmpeg (only for the /yt command) + +- Node.js 20 or newer (you can also use Bun) +- A Telegram bot (create one at [@BotFather](https://t.me/botfather)) +- Latest version of Node.js +- FFmpeg (only for the `/yt` command) ## Run it yourself, develop or contribute with Kowalski + First, clone the repo with Git: -``` + +```bash git clone https://github.com/ABOCN/TelegramBot ``` + And now, init the submodules with these commands (this is very important): -``` + +```bash cd TelegramBot git submodule update --init --recursive ``` + Next, inside the repository directory, create a `config.env` file with some content, which you can see the [example .env file](config.env.example) to fill info with. To see the meaning of each one, see [the Functions section](#configenv-functions). After editing the file, save all changes and run the bot with ``npm start``. -- To deal with dependencies, just run ``npm install`` or ``npm i`` at any moment to install all of them. + +> [!TIP] +> To deal with dependencies, just run ``npm install`` or ``npm i`` at any moment to install all of them. ## config.env Functions - **botSource**: Put the link to your bot source code. @@ -34,7 +44,9 @@ After editing the file, save all changes and run the bot with ``npm start``. - **weatherKey**: Weather.com API key, used for the `/weather` command. ## Note + - Take care of your ``config.env`` file, as it is so much important and needs to be secret (like your passwords), as anyone can do whatever they want to the bot with this token! ## About/License + BSD-3-Clause - 2024 Lucas Gabriel (lucmsilva). From 44b57bbafcd6b0b8ec789809d62d2e188c800315 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 14 Apr 2025 04:57:29 +0000 Subject: [PATCH 13/21] Update AUTHORS file automatically --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 352a121..6f138dd 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,6 +1,7 @@ A Bunch of Computer Nerds A Bunch of Computer Nerds A Bunch of Computer Nerds +Aidan DaviDev <97841570+DaviisDev@users.noreply.github.com> Giovani Finazzi <53719063+GiovaniFZ@users.noreply.github.com> GiovaniFZ From ff9260bebcd5895e1c25bfc14f51351ffd864b15 Mon Sep 17 00:00:00 2001 From: Aidan Date: Mon, 14 Apr 2025 00:58:40 -0400 Subject: [PATCH 14/21] [m] docs: addl. lint --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1ba7feb..e2e35ff 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ After editing the file, save all changes and run the bot with ``npm start``. > To deal with dependencies, just run ``npm install`` or ``npm i`` at any moment to install all of them. ## config.env Functions + - **botSource**: Put the link to your bot source code. - **botToken**: Put your bot token that you created at [@BotFather](https://t.me/botfather). - **botAdmins**: Put the ID of the people responsible for managing the bot. They can use some administrative + exclusive commands on any group. From b619892c1c0e1b82870ba23f747178a3cccddbdc Mon Sep 17 00:00:00 2001 From: Aidan Date: Mon, 14 Apr 2025 19:30:01 -0400 Subject: [PATCH 15/21] translations: change wording to enforce privacy policy --- src/locales/english.json | 2 +- src/locales/portuguese.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/locales/english.json b/src/locales/english.json index 0a4291b..b2fb10a 100644 --- a/src/locales/english.json +++ b/src/locales/english.json @@ -1,5 +1,5 @@ { - "botWelcome": "*Hello! I am {botName}!*\nI was made with love by some nerds who really love programming!\n\n*Before using, you need to read the privacy policy (/privacy) to understand where your data goes when using this bot.*\n\nAlso, you can use /help to see the bot commands!", + "botWelcome": "*Hello! I'm {botName}!*\nI was made with love by some nerds who really love programming!\n\n*By using {botName}, you affirm that you have read to and agree with the privacy policy (/privacy). This helps you understand where your data goes when using this bot.*\n\nAlso, you can use /help to see the bot commands!", "botHelp": "*Hey, I'm {botName}, a simple bot made entirely from scratch in Telegraf and Node.js by some nerds who really love programming.*\n\nClick on the buttons below to see which commands you can use!\n", "botPrivacy": "Check out [this link](https://blog.lucmsilva.com/posts/lynx-privacy-policy) to read the bot's privacy policy.", "botAbout": "*About the bot*\n\nThe bot base was originally created by [Lucas Gabriel (lucmsilva)](https://github.com/lucmsilva651), now maintained by several people.\n\nThe bot's purpose is to bring fun to your groups here on Telegram in a relaxed and simple way. The bot also features some very useful commands, which you can see using the help command (/help).\n\nSpecial thanks to @givfnz2 for his many contributions to the bot!\n\nSee the source code: [Click here to go to GitHub]({sourceLink})", diff --git a/src/locales/portuguese.json b/src/locales/portuguese.json index 570f308..6b492f2 100644 --- a/src/locales/portuguese.json +++ b/src/locales/portuguese.json @@ -1,5 +1,5 @@ { - "botWelcome": "*Olá! Eu sou o {botName}!*\n\n*Antes de usar, você precisa ler a política de privacidade (/privacy) para entender onde seus dados vão ao usar este bot.*\n\nAlém disso, você pode usar /help para ver os meus comandos!", + "botWelcome": "*Olá! Eu sou o {botName}!*\n\n*Ao usar o {botName}, você afirma que leu e concorda com a política de privacidade (/privacy). Isso ajuda você a entender onde seus dados vão ao usar este bot.*\n\nAlém disso, você pode usar /help para ver os meus comandos!", "botHelp": "*Oi, eu sou o {botName}, um bot simples feito do zero em Telegraf e Node.js por uns nerds que gostam de programação.*\n\nVeja o código fonte: [Clique aqui para ir ao GitHub]({sourceLink})\n\nClique nos botões abaixo para ver quais comandos você pode usar!\n", "botPrivacy": "Acesse [este link](https://blog.lucmsilva.com/posts/lynx-privacy-policy) para ler a política de privacidade do bot.", "botAbout": "*Sobre o bot*\n\nA base deste bot foi feita originalmente por [Lucas Gabriel (lucmsilva)](https://github.com/lucmsilva651), agora sendo mantido por várias pessoas.\n\nA intenção do bot é trazer diversão para os seus grupos aqui no Telegram de uma maneira bem descontraida e simples. O bot também conta com alguns comandos bem úteis, que você consegue ver com o comando de ajuda (/help).\n\nAgradecimento especial ao @givfnz2 pelas suas várias contribuições ao bot!\n\nVeja o código fonte: [Clique aqui para ir ao GitHub]({sourceLink})", From 935385eb4f481f3352a9f97a1fdec86809b88bba Mon Sep 17 00:00:00 2001 From: Aidan Date: Mon, 14 Apr 2025 19:37:43 -0400 Subject: [PATCH 16/21] hf: use global regex instead --- src/commands/crew.js | 16 ++++++++-------- src/commands/help.js | 6 +++--- src/commands/main.js | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/commands/crew.js b/src/commands/crew.js index 18b0eb9..52a3703 100644 --- a/src/commands/crew.js +++ b/src/commands/crew.js @@ -62,7 +62,7 @@ async function handleAdminCommand(ctx, action, successMessage, errorMessage) { reply_to_message_id: ctx.message.message_id }); } catch (error) { - ctx.reply(errorMessage.replace('{error}', error.message), { + ctx.reply(errorMessage.replace(/{error}/g, error.message), { parse_mode: 'Markdown', reply_to_message_id: ctx.message.message_id }); @@ -91,12 +91,12 @@ module.exports = (bot) => { handleAdminCommand(ctx, async () => { try { const commitHash = await getGitCommitHash(); - await ctx.reply(Strings.gitCurrentCommit.replace('{commitHash}', commitHash), { + await ctx.reply(Strings.gitCurrentCommit.replace(/{commitHash}/g, commitHash), { parse_mode: 'Markdown', reply_to_message_id: ctx.message.message_id }); } catch (error) { - ctx.reply(Strings.gitErrRetrievingCommit.replace('{error}', error), { + ctx.reply(Strings.gitErrRetrievingCommit.replace(/{error}/g, error), { parse_mode: 'Markdown', reply_to_message_id: ctx.message.message_id }); @@ -109,12 +109,12 @@ module.exports = (bot) => { handleAdminCommand(ctx, async () => { try { const result = await updateBot(); - await ctx.reply(Strings.botUpdated.replace('{result}', result), { + await ctx.reply(Strings.botUpdated.replace(/{result}/g, result), { parse_mode: 'Markdown', reply_to_message_id: ctx.message.message_id }); } catch (error) { - ctx.reply(Strings.errorUpdatingBot.replace('{error}', error), { + ctx.reply(Strings.errorUpdatingBot.replace(/{error}/g, error), { parse_mode: 'Markdown', reply_to_message_id: ctx.message.message_id }); @@ -127,7 +127,7 @@ module.exports = (bot) => { const botName = ctx.message.text.split(' ').slice(1).join(' '); handleAdminCommand(ctx, async () => { await ctx.telegram.setMyName(botName); - }, Strings.botNameChanged.replace('{botName}', botName), Strings.botNameErr.replace('{error}', error)); + }, Strings.botNameChanged.replace(/{botName}/g, botName), Strings.botNameErr.replace(/{error}/g, error)); }); bot.command('setbotdesc', spamwatchMiddleware, async (ctx) => { @@ -135,7 +135,7 @@ module.exports = (bot) => { const botDesc = ctx.message.text.split(' ').slice(1).join(' '); handleAdminCommand(ctx, async () => { await ctx.telegram.setMyDescription(botDesc); - }, Strings.botDescChanged.replace('{botDesc}', botDesc), Strings.botDescErr.replace('{error}', error)); + }, Strings.botDescChanged.replace(/{botDesc}/g, botDesc), Strings.botDescErr.replace(/{error}/g, error)); }); bot.command('botkickme', spamwatchMiddleware, async (ctx) => { @@ -159,7 +159,7 @@ module.exports = (bot) => { caption: botFile }); } catch (error) { - ctx.reply(Strings.unexpectedErr.replace('{error}', error.message), { + ctx.reply(Strings.unexpectedErr.replace(/{error}/g, error.message), { parse_mode: 'Markdown', reply_to_message_id: ctx.message.message_id }); diff --git a/src/commands/help.js b/src/commands/help.js index 58dfa86..ab9e601 100644 --- a/src/commands/help.js +++ b/src/commands/help.js @@ -6,8 +6,8 @@ async function sendHelpMessage(ctx, isEditing) { const Strings = getStrings(ctx.from.language_code); const botInfo = await ctx.telegram.getMe(); const helpText = Strings.botHelp - .replace('{botName}', botInfo.first_name) - .replace("{sourceLink}", process.env.botSource); + .replace(/{botName}/g, botInfo.first_name) + .replace(/{sourceLink}/g, process.env.botSource); const options = { parse_mode: 'Markdown', disable_web_page_preview: true, @@ -35,7 +35,7 @@ module.exports = (bot) => { bot.command("about", spamwatchMiddleware, async (ctx) => { const Strings = getStrings(ctx.from.language_code); - const aboutMsg = Strings.botAbout.replace("{sourceLink}", `${process.env.botSource}`); + const aboutMsg = Strings.botAbout.replace(/{sourceLink}/g, `${process.env.botSource}`); ctx.reply(aboutMsg, { parse_mode: 'Markdown', diff --git a/src/commands/main.js b/src/commands/main.js index c111603..6333296 100644 --- a/src/commands/main.js +++ b/src/commands/main.js @@ -6,7 +6,7 @@ module.exports = (bot) => { bot.start(spamwatchMiddleware, async (ctx) => { const Strings = getStrings(ctx.from.language_code); const botInfo = await ctx.telegram.getMe(); - const startMsg = Strings.botWelcome.replace('{botName}', botInfo.first_name); + const startMsg = Strings.botWelcome.replace(/{botName}/g, botInfo.first_name); ctx.reply(startMsg, { parse_mode: 'Markdown', From 62e7a1393b01f09c1c95f83ce9b87508427b22f2 Mon Sep 17 00:00:00 2001 From: Aidan Date: Mon, 14 Apr 2025 19:58:36 -0400 Subject: [PATCH 17/21] feat: better error handling and fixed file size checking for /yt --- src/bot.js | 6 ++++++ src/commands/youtube.js | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/bot.js b/src/bot.js index ad909db..472bc10 100644 --- a/src/bot.js +++ b/src/bot.js @@ -6,6 +6,12 @@ require('@dotenvx/dotenvx').config({ path: "config.env" }); require('./plugins/ytdlp-wrapper.js'); // require('./plugins/termlogger.js'); +// Ensures bot token is set, and not default value +if (!process.env.botToken || process.env.botToken === 'InsertYourBotTokenHere') { + console.error('Bot token is not set. Please set the bot token in the config.env file.') + process.exit(1) +} + const bot = new Telegraf(process.env.botToken); const maxRetries = process.env.maxRetries || 5; let restartCount = 0; diff --git a/src/commands/youtube.js b/src/commands/youtube.js index 76b0d29..316385b 100644 --- a/src/commands/youtube.js +++ b/src/commands/youtube.js @@ -94,6 +94,20 @@ module.exports = (bot) => { getApproxSize(ytDlpPath, videoUrl), ]); + if (approxSizeInMB > 50) { + await ctx.telegram.editMessageText( + ctx.chat.id, + downloadingMessage.message_id, + null, + Strings.ytDownload.uploadLimit, { + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id, + }, + ); + + return; + } + await ctx.telegram.editMessageText( ctx.chat.id, downloadingMessage.message_id, @@ -134,7 +148,7 @@ module.exports = (bot) => { fs.unlinkSync(mp4File); } catch (error) { - if (toString(error).includes("Request Entity Too Large")) { + if (error.response.description.includes("Request Entity Too Large")) { await ctx.telegram.editMessageText( ctx.chat.id, downloadingMessage.message_id, From 429e89bf1d35b5f7246419a4ae77fb55a0cee2ef Mon Sep 17 00:00:00 2001 From: Aidan Date: Mon, 14 Apr 2025 20:09:13 -0400 Subject: [PATCH 18/21] docker/docs: dockerize and add+modify documentation --- .dockerignore | 8 ++++++++ Dockerfile | 18 ++++++++++++++++++ README.md | 45 +++++++++++++++++++++++++++++++++++++++++++-- docker-compose.yml | 9 +++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ba231c9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +node_modules +npm-debug.log +.git +.gitignore +.env +config.env +*.md +!README.md \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..56b5b32 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM node:20-slim + +# Install ffmpeg and other deps +RUN apt-get update && apt-get install -y ffmpeg && apt-get clean && rm -rf /var/lib/apt/lists/* + +WORKDIR /usr/src/app + +COPY package*.json ./ + +RUN npm install + +COPY . . + +RUN chmod +x /usr/src/app/src/plugins/yt-dlp/yt-dlp + +VOLUME /usr/src/app/config.env + +CMD ["npm", "start"] \ No newline at end of file diff --git a/README.md b/README.md index e2e35ff..6b4fe46 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,10 @@ Kowalski is a a simple Telegram bot made in Node.js. ## Self-host requirements -- Node.js 20 or newer (you can also use Bun) +- Node.js 20 or newer (you can also use [Bun](https://bun.sh)) - A Telegram bot (create one at [@BotFather](https://t.me/botfather)) -- Latest version of Node.js - FFmpeg (only for the `/yt` command) +- Docker and Docker Compose (only required for Docker setup) ## Run it yourself, develop or contribute with Kowalski @@ -36,6 +36,47 @@ After editing the file, save all changes and run the bot with ``npm start``. > [!TIP] > To deal with dependencies, just run ``npm install`` or ``npm i`` at any moment to install all of them. +## Running with Docker + +> [!IMPORTANT] +> Please complete the above steps to prepare your local copy for building. You do not need to install FFmpeg on your host system. + +You can also run Kowalski using Docker, which simplifies the setup process. Make sure you have Docker and Docker Compose installed. + +### Using Docker Compose + +1. **Make sure to setup your `config.env` file first!** + +2. **Run the container** + + ```bash + docker compose up -d + ``` + + > [!NOTE] + > The `-d` flag causes the bot to run in the background. If you're just playing around, you may not want to use this flag. + +### Using Docker Run + +If you prefer to use Docker directly, you can use these instructions instead. + +1. **Make sure to setup your `config.env` file first!** + +2. **Build the image** + + ```bash + docker build -t kowalski . + ``` + +3. **Run the container** + + ```bash + docker run -d --name kowalski --restart unless-stopped -v $(pwd)/config.env:/usr/src/app/config.env:ro kowalski + ``` + + > [!NOTE] + > The `-d` flag causes Kowalski to run in the background. If you're just playing around, you may not want to use this flag. + ## config.env Functions - **botSource**: Put the link to your bot source code. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..981d90a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +services: + kowalski: + build: . + container_name: kowalski + restart: unless-stopped + volumes: + - ./config.env:/usr/src/app/config.env:ro + environment: + - NODE_ENV=production \ No newline at end of file From 95114dc918a86f7e59589840c8b95a27ff7a7924 Mon Sep 17 00:00:00 2001 From: Aidan Date: Mon, 14 Apr 2025 20:11:57 -0400 Subject: [PATCH 19/21] docs: minor lint/fix --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6b4fe46..370f7d7 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,8 @@ You can also run Kowalski using Docker, which simplifies the setup process. Make docker compose up -d ``` - > [!NOTE] - > The `-d` flag causes the bot to run in the background. If you're just playing around, you may not want to use this flag. +> [!NOTE] +> The `-d` flag causes Kowalski to run in the background. If you're just playing around, you may not want to use this flag. ### Using Docker Run @@ -74,8 +74,8 @@ If you prefer to use Docker directly, you can use these instructions instead. docker run -d --name kowalski --restart unless-stopped -v $(pwd)/config.env:/usr/src/app/config.env:ro kowalski ``` - > [!NOTE] - > The `-d` flag causes Kowalski to run in the background. If you're just playing around, you may not want to use this flag. +> [!NOTE] +> The `-d` flag causes Kowalski to run in the background. If you're just playing around, you may not want to use this flag. ## config.env Functions From c6114c9f625e2b3bbfba3e9e3b449047a708bbba Mon Sep 17 00:00:00 2001 From: Aidan Date: Mon, 14 Apr 2025 20:18:44 -0400 Subject: [PATCH 20/21] docs: add troubleshooting section for yt commands --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 370f7d7..a154166 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,18 @@ If you prefer to use Docker directly, you can use these instructions instead. - Take care of your ``config.env`` file, as it is so much important and needs to be secret (like your passwords), as anyone can do whatever they want to the bot with this token! +## Troubleshooting + +### YouTube Downloading + +**Q:** I get a "Permission denied (EACCES)" error in the console when running the `/yt` command + +**A:** Make sure `src/plugins/yt-dlp/yt-dlp` is executable. You can do this on Linux like so: + +```bash +chmod +x src/plugins/yt-dlp/yt-dlp +``` + ## About/License BSD-3-Clause - 2024 Lucas Gabriel (lucmsilva). From 7120644945a3ad4b5f89f017b3d09d7bf3ce536a Mon Sep 17 00:00:00 2001 From: Giovani Finazzi <53719063+GiovaniFZ@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:59:11 -0300 Subject: [PATCH 21/21] fix: yt: set cookies for check_size args (#35) Co-authored-by: Aidan --- src/commands/youtube.js | 51 ++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/commands/youtube.js b/src/commands/youtube.js index 316385b..2442f95 100644 --- a/src/commands/youtube.js +++ b/src/commands/youtube.js @@ -41,7 +41,12 @@ const downloadFromYoutube = async (command, args) => { }; const getApproxSize = async (command, videoUrl) => { - const args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx']; + let args = []; + if (fs.existsSync(path.resolve(__dirname, "../props/cookies.txt"))) { + args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx', '--cookies', path.resolve(__dirname, "../props/cookies.txt")]; + } else { + args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx']; + } try { const { stdout } = await downloadFromYoutube(command, args); const sizeInBytes = parseInt(stdout.trim(), 10); @@ -113,9 +118,9 @@ module.exports = (bot) => { downloadingMessage.message_id, null, Strings.ytDownload.downloadingVid, { - parse_mode: 'Markdown', - reply_to_message_id: ctx.message.message_id, - }, + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id, + }, ); const dlpArgs = [videoUrl, ...cmdArgs.split(' '), mp4File]; @@ -126,12 +131,12 @@ module.exports = (bot) => { downloadingMessage.message_id, null, Strings.ytDownload.uploadingVid, { - parse_mode: 'Markdown', - reply_to_message_id: ctx.message.message_id, - }, + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id, + }, ); - if(fs.existsSync(tempMp4File)){ + if (fs.existsSync(tempMp4File)) { await downloadFromYoutube(ffmpegPath, ffmpegArgs); } @@ -140,12 +145,13 @@ module.exports = (bot) => { try { await ctx.replyWithVideo({ - source: mp4File }, { + source: mp4File + }, { caption: message, parse_mode: 'Markdown', reply_to_message_id: ctx.message.message_id, }); - + fs.unlinkSync(mp4File); } catch (error) { if (error.response.description.includes("Request Entity Too Large")) { @@ -154,9 +160,9 @@ module.exports = (bot) => { downloadingMessage.message_id, null, Strings.ytDownload.uploadLimit, { - parse_mode: 'Markdown', - reply_to_message_id: ctx.message.message_id, - }, + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id, + }, ); } else { const errMsg = Strings.ytDownload.uploadErr.replace("{error}", error) @@ -165,9 +171,9 @@ module.exports = (bot) => { downloadingMessage.message_id, null, errMsg, { - parse_mode: 'Markdown', - reply_to_message_id: ctx.message.message_id, - }, + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id, + }, ); }; @@ -185,22 +191,21 @@ module.exports = (bot) => { downloadingMessage.message_id, null, Strings.ytDownload.libNotFound, { - parse_mode: 'Markdown', - reply_to_message_id: ctx.message.message_id, - }, + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id, + }, ); } } catch (error) { - console.error(error); const errMsg = Strings.ytDownload.uploadErr.replace("{error}", error) await ctx.telegram.editMessageText( ctx.chat.id, downloadingMessage.message_id, null, errMsg, { - parse_mode: 'Markdown', - reply_to_message_id: ctx.message.message_id, - }, + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id, + }, ); } });