mirror of
https://github.com/abocn/TelegramBot.git
synced 2025-04-29 09:25:58 +00:00
Modulate bot with some tweaks
This commit is contained in:
parent
92b6afe340
commit
c67df9f865
@ -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``.
|
At last, run the bot with ``npm start``.
|
||||||
|
|
||||||
## Notes
|
## 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
|
## About/License
|
||||||
MIT - 2024 Lucas Gabriel (lucmsilva).
|
MIT - 2024 Lucas Gabriel (lucmsilva).
|
@ -2,10 +2,10 @@
|
|||||||
"name": "lynx",
|
"name": "lynx",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "A simple Telegram bot made in Node.js",
|
"description": "A simple Telegram bot made in Node.js",
|
||||||
"main": "src/lynx_main.js",
|
"main": "src/main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"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": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
6
src/commands/start.js
Normal file
6
src/commands/start.js
Normal 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.")
|
||||||
|
}
|
@ -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
24
src/main.js
Normal 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.")
|
Loading…
x
Reference in New Issue
Block a user