Created
June 22, 2025 02:40
-
-
Save LozanoMatheus/c50bf8bff21b398b23af6ae06e62a466 to your computer and use it in GitHub Desktop.
Send WhatsApp message via Terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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