Modulate bot with some tweaks

This commit is contained in:
Lucas Gabriel 2024-05-24 18:39:12 -03:00
parent 92b6afe340
commit c67df9f865
No known key found for this signature in database
GPG key ID: D9B075FC6DC93985
5 changed files with 33 additions and 15 deletions

6
src/commands/start.js Normal file
View file

@ -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.")
}

View file

@ -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");
}
});

24
src/main.js Normal file
View file

@ -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.")