Skip to content

Instantly share code, notes, and snippets.

@ByteJoseph
Last active August 13, 2024 07:44
Show Gist options
  • Save ByteJoseph/81871fe6231c5f33a8229cf2506ce01b to your computer and use it in GitHub Desktop.
Save ByteJoseph/81871fe6231c5f33a8229cf2506ce01b to your computer and use it in GitHub Desktop.
Send whatsapp message to a number
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
// Initialize the WhatsApp client
const client = new Client({
authStrategy: new LocalAuth()
});
// Generate and display the QR code in the terminal
client.on('qr', (qr) => {
qrcode.generate(qr, { small: true });
});
// Log successful connection
client.on('ready', () => {
console.log('WhatsApp client is ready!');
// Replace with the target number and message
const targetNumber = '919876543210'; // Example: '1234567890' without the '+' sign
const message = 'Hello, this is a test message!';
sendMessageToNumber(targetNumber, message);
});
// Function to send a message to a specific number
function sendMessageToNumber(number, message) {
const chatId = `${number}@c.us`; // WhatsApp format for phone number
client.sendMessage(chatId, message).then(response => {
console.log('Message sent successfully:', response);
}).catch(err => {
console.error('Error sending message:', err);
});
}
// Start the client
client.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment