如何在 whatsapp 中发送 whatsapp 消息-web.js
How to send whatsapp message in whatsapp-web.js
我正在使用 whatsapp-web.js
发送和回复消息。 https://github.com/pedroslopez/whatsapp-web.js
我可以使用以下代码连接并回复消息:
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();
如何在whatsapp中发送新消息到手机号码??
所以,你想用whatsapp直接发信息到手机号码-web.js
幸运的是 whatsapp-web.js 允许我们这样做。只需按照说明操作即可。
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);
});
想怎么用就怎么用。
首先确认号码已在WhatsApp注册,如果是则仅使用sendMessage方法发送消息。
以下是工作代码-
client.isRegisteredUser("911234567890@c.us").then(function(isRegistered) {
if(isRegistered) {
client.sendMessage("911234567890@c.us", "hello");
}
})
以下是我在 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");
}
.
.
更新(等待仅在异步错误中有效):
如果你在任何情况下使用这个方法,你应该把它放在一个异步函数中
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");
}
});
如果您有联系人姓名,您可以避免手动创建 chatId
并从联系人对象中获取它,方法是获取所有联系人并通过 name
或 pushname
找到合适的联系人。
const contacts = await client.getContacts()
const contact = contacts.find(({ name }) => name === CONTACT_DISPLAY_NAME)
const { id: { _serialized: chatId } } = contact
await client.sendMessage(chatId, 'Message Text')
我正在使用 whatsapp-web.js
发送和回复消息。 https://github.com/pedroslopez/whatsapp-web.js
我可以使用以下代码连接并回复消息:
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();
如何在whatsapp中发送新消息到手机号码??
所以,你想用whatsapp直接发信息到手机号码-web.js
幸运的是 whatsapp-web.js 允许我们这样做。只需按照说明操作即可。
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);
});
想怎么用就怎么用。
首先确认号码已在WhatsApp注册,如果是则仅使用sendMessage方法发送消息。
以下是工作代码-
client.isRegisteredUser("911234567890@c.us").then(function(isRegistered) {
if(isRegistered) {
client.sendMessage("911234567890@c.us", "hello");
}
})
以下是我在 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");
}
.
.
更新(等待仅在异步错误中有效):
如果你在任何情况下使用这个方法,你应该把它放在一个异步函数中
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");
}
});
如果您有联系人姓名,您可以避免手动创建 chatId
并从联系人对象中获取它,方法是获取所有联系人并通过 name
或 pushname
找到合适的联系人。
const contacts = await client.getContacts()
const contact = contacts.find(({ name }) => name === CONTACT_DISPLAY_NAME)
const { id: { _serialized: chatId } } = contact
await client.sendMessage(chatId, 'Message Text')