From c67df9f865c9ed6662f764b3b5e13fb8ea860a02 Mon Sep 17 00:00:00 2001 From: Lucas Gabriel <90426410+lucmsilva651@users.noreply.github.com> Date: Fri, 24 May 2024 18:39:12 -0300 Subject: [PATCH] Modulate bot with some tweaks --- README.md | 2 +- package.json | 4 ++-- src/commands/start.js | 6 ++++++ src/lynx_main.js | 12 ------------ src/main.js | 24 ++++++++++++++++++++++++ 5 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 src/commands/start.js delete mode 100644 src/lynx_main.js create mode 100644 src/main.js diff --git a/README.md b/README.md index e40080b..9c796c1 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Put your bot token that you created at [@BotFather](https://t.me/botfather) at t At last, run the bot with ``npm start``. ## Notes -The source code itself of the bot is at ``src/lynx_main.js``. You can rename this file and change the ``package.json`` file as your needs. +The source code itself of the bot is at ``src/main.js``, and the commands are in ``src/commands``. You can rename this file and change the ``package.json`` file as your needs. ## About/License MIT - 2024 Lucas Gabriel (lucmsilva). \ No newline at end of file diff --git a/package.json b/package.json index 2980123..0becc2c 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,10 @@ "name": "lynx", "version": "1.0.0", "description": "A simple Telegram bot made in Node.js", - "main": "src/lynx_main.js", + "main": "src/main.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "node --env-file=config.env src/lynx_main.js" + "start": "node --env-file=config.env src/main.js" }, "repository": { "type": "git", diff --git a/src/commands/start.js b/src/commands/start.js new file mode 100644 index 0000000..9d3e449 --- /dev/null +++ b/src/commands/start.js @@ -0,0 +1,6 @@ +// start command handler +module.exports = function(bot, msg) { + const chatId = msg.chat.id; + bot.sendMessage(chatId, "Welcome to Lynx!\n\nI was made with love by Lucas Gabriel (lucmsilva)!\n\nCheck out my source code:\nhttps://github.com/lucmsilva651/lynx") + console.log("INFO: /start executed.") +} \ No newline at end of file diff --git a/src/lynx_main.js b/src/lynx_main.js deleted file mode 100644 index f28e544..0000000 --- a/src/lynx_main.js +++ /dev/null @@ -1,12 +0,0 @@ -const TelegramBot = require('node-telegram-bot-api'); -const token = process.env.TGBOT_TOKEN; // config.env -const bot = new TelegramBot(token, { polling: true }); - -bot.on('message', (msg) => { - const chatId = msg.chat.id; - const messageText = msg.text; - - if (messageText === '/start') { - bot.sendMessage(chatId, "Welcome to Lynx!\n\nI was made with love by Lucas Gabriel (lucmsilva)!\n\nCheck out my source code:\nhttps://github.com/lucmsilva651/lynx"); - } -}); diff --git a/src/main.js b/src/main.js new file mode 100644 index 0000000..f27a9bf --- /dev/null +++ b/src/main.js @@ -0,0 +1,24 @@ +const TelegramBot = require('node-telegram-bot-api'); +const fs = require('fs'); +const path = require('path'); +const token = process.env.TGBOT_TOKEN; // config.env +const bot = new TelegramBot(token, { polling: true }); + +const commandsPath = path.join(__dirname, 'commands') +const commandHandlers = {}; + +// load all commands +fs.readdirSync(commandsPath).forEach(file => { + const command = `/${path.parse(file).name}`; + const handler = require(path.join(commandsPath,file)); + commandHandlers[command] = handler; +}) + +bot.on('message', (msg) => { + const messageText = msg.text; + if (commandHandlers[messageText]) { + commandHandlers[messageText](bot, msg); + } +}); + +console.log("INFO: Lynx started.") \ No newline at end of file