I am using whatsapp-web.js
to send and reply message. https://github.com/pedroslopez/whatsapp-web.js
I can connect and reply message using following code:
const { Client } = require('whatsapp-web.js');
const client = new Client();
client.on('qr', (qr) => {
// Generate and scan this code with your phone
console.log('QR RECEIVED', qr);
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('message', msg => {
if (msg.body == '!ping') {
msg.reply('pong');
}
});
client.initialize();
how can I send new message in whatsapp to mobile number??
Tap on the “Message” button and You will be taken to the WhatsApp app with a chat being open for the said contact. You can also add a predefined message to begin the chat with the same number. For that, you must add the text using the format below: https://wa.me/whatsappphonenumber/?text=urlencodedtext.
Install the WhatsApp Business API Client — Install your API client. Once your client is working, you can update your application settings. Start using the client — Register your phone number with an API call to /account and send a test message with a call to /messages .
js works by running WhatsApp Web in the background and automating its interaction, you'll need to authorize the client by scanning a QR code from WhatsApp on your phone. Copied! And now we'll modify our code to use this new module: const qrcode = require('qrcode-terminal'); const { Client } = require('whatsapp-web.
So, you want to send a message to mobile number directly using whatsapp-web.js
Fortunately whatsapp-web.js allow us to do that. Simply you can follow the instruction.
client.on('ready', () => {
console.log('Client is ready!');
// Number where you want to send the message.
const number = "+911234567890";
// Your message.
const text = "Hey john";
// Getting chatId from the number.
// we have to delete "+" from the beginning and add "@c.us" at the end of the number.
const chatId = number.substring(1) + "@c.us";
// Sending message.
client.sendMessage(chatId, text);
});
use it as you wish.
Here is how I send messages in whatsapp-web.js
.
const number = "9830121234";
const sanitized_number = number.toString().replace(/[- )(]/g, ""); // remove unnecessary chars from the number
const final_number = `91${sanitized_number.substring(sanitized_number.length - 10)}`; // add 91 before the number here 91 is country code of India
const number_details = await client.getNumberId(final_number); // get mobile number details
if (number_details) {
const sendMessageData = await client.sendMessage(number_details._serialized, sendData.message); // send message
} else {
console.log(final_number, "Mobile number is not registered");
}
.
.
update (for await is valid inside only async error):
if you are using this method in any event you should put it inside an async function
on.any_kind_of_event(async function () {
const number = "9830121234";
const sanitized_number = number.toString().replace(/[- )(]/g, ""); // remove unnecessary chars from the number
const final_number = `91${sanitized_number.substring(sanitized_number.length - 10)}`; // add 91 before the number here 91 is country code of India
const number_details = await client.getNumberId(final_number); // get mobile number details
if (number_details) {
const sendMessageData = await client.sendMessage(number_details._serialized, sendData.message); // send message
} else {
console.log(final_number, "Mobile number is not registered");
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With