Skip to content

Commit

Permalink
console: log: beautify logging when start bot and receiving messages
Browse files Browse the repository at this point in the history
Signed-off-by: Salman Wahib <[email protected]>
  • Loading branch information
sxlmnwb committed Aug 13, 2023
1 parent 7de45b9 commit eda6eb4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

require('dotenv').config();
const chalk = require('chalk');
const { useMainnet, useTestnet, usePing, useCommand, startWhatsapp } = require('./src');
const { startMainnetLoop, startTestnetLoop, stopLoop } = require('./src/useLoopReq');

Expand All @@ -11,7 +12,22 @@ const start = async () => {

client.on('message', async msg => {

console.log('received message: ' + msg.body, '| fromID: ' + msg.from);
const user = await msg.getContact();
const userName = user.pushname || user.verifiedName || user.formattedName;
const time = new Date().toLocaleString().split(', ')[1];
const idMessage = msg.id._serialized.split('_').pop();
const number = msg.from.replace('@c.us', '');

console.log(chalk.white.bgGreenBright.bold('RECEIVED') +
" [" + chalk.blueBright(`${msg.body}`) + "] " +
chalk.green('ID') +
" [" + chalk.blueBright(`${idMessage}`) + "] " +
chalk.green('SENDER') +
" [" + chalk.blueBright(`${userName}`) + "] " +
chalk.green('NUMBER') +
" [" + chalk.blueBright(`+${number}`) + "] " +
chalk.green('TIME') +
" [" + chalk.blueBright(`${time}`) + "]");

const chat = await msg.getChat();

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "sxlmnwb",
"dependencies": {
"axios": "^1.4.0",
"chalk": "^4.1.2",
"dotenv": "^16.3.1",
"is-root": "^3.0.0",
"ping": "^0.4.4",
Expand Down
12 changes: 7 additions & 5 deletions src/startWhatsapp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const useHelpCommand = require('./useHelpCommand');
const chalk = require('chalk');

module.exports = async function startWhatsapp() {
const isRoot = (await import('is-root')).default;
Expand All @@ -19,28 +20,29 @@ module.exports = async function startWhatsapp() {
});

client.on('qr', async (qr) => {
console.log('QR Code received, scan please ...');
console.log(chalk.white.bgYellowBright.bold('QR CODE') + ' received, scan please ...');
qrcode.generate(qr, {small: true});
});

client.on('ready', async () => {
console.log('cosmos_wabot client is ready and connected !!!');
console.log(chalk.blueBright('[cosmos_wabot]') + ' client is ready and ' + chalk.white.bgGreenBright.bold('CONNECTED'));
const groupId = process.env.GROUP_ID;
const text = useHelpCommand();
if (groupId) {
await client.sendMessage(groupId, text);
} else {
console.log('No GROUP_ID set in .env');
console.log(chalk.white.bgRedBright.bold('NO GROUP_ID') + ' set in .env');
}
console.log();
});

client.on('disconnected', (reason) => {
console.log('cosmos_wabot was disconnected, reason: ', reason);
console.log(chalk.yellowBright('[cosmos_wabot]') + ' was ' + chalk.white.bgRedBright.bold('DISCONNECTED') + chalk.white(', reason: ') + chalk.white.bgRedBright.bold(`${reason}`));
client.initialize();
});

client.on('connected', () => {
console.log('cosmos_wabot are connected !!!');
console.log(chalk.blueBright('[cosmos_wabot]') + ' are ' + chalk.white.bgGreenBright.bold('CONNECTED'));
});

return client;
Expand Down

0 comments on commit eda6eb4

Please sign in to comment.