Skip to content

Instantly share code, notes, and snippets.

@LozanoMatheus
Created June 22, 2025 02:40
Show Gist options
  • Select an option

  • Save LozanoMatheus/c50bf8bff21b398b23af6ae06e62a466 to your computer and use it in GitHub Desktop.

Select an option

Save LozanoMatheus/c50bf8bff21b398b23af6ae06e62a466 to your computer and use it in GitHub Desktop.
Send WhatsApp message via Terminal
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const [, , jid, message] = process.argv;
if (!jid || !message) {
console.error('❌ Usage: node send.js <jid> "<message>"');
process.exit(1);
}
const client = new Client({
authStrategy: new LocalAuth({
clientId: 'default',
dataPath: './.wwebjs_auth'
}),
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox']
}
});
let qrCodeGenerated = false;
client.on('qr', (qr) => {
qrCodeGenerated = true;
console.log('\nπŸ“± QR code required. Please scan it using your phone.\n');
console.log('QR Code:', qr);
qrcode.generate(qr, { small: true });
});
client.on('authenticated', () => {
console.log('πŸ” Session authenticated.');
});
client.on('auth_failure', (msg) => {
console.error('❌ Authentication failed:', msg);
});
client.on('ready', async () => {
if (qrCodeGenerated) {
console.log('⏳ Waiting for the session to be saved...');
await new Promise(resolve => setTimeout(resolve, 65000));
console.log('βœ… Session saved.');
}
console.log(`βœ… WhatsApp client ready. Sending message to ${jid}...`);
try {
const msg = await client.sendMessage(jid, message);
console.log(`βœ… Message ${msg.id.id} sent to ${msg.to} with content: "${msg.body}"`);
} catch (err) {
console.error('❌ Failed to send message:', err);
process.exit(1);
}
await new Promise(resolve => setTimeout(resolve, 3000));
await client.destroy();
console.log('πŸ‘‹ Client destroyed. Exiting.');
process.exit(0);
});
client.initialize().catch((err) => {
console.error('❌ Failed to initialize client:', err);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment