7

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中将新消息发送到手机号码?

4

4 回答 4

8

因此,您想直接使用 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);
});

随心所欲地使用它。

于 2020-12-07T14:20:45.100 回答
6

这是我发送消息的方式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");
    }
});

于 2021-07-07T12:29:38.600 回答
3

首先确认号码已在 WhatsApp 注册,如果为真,则仅使用sendMessage方法发送消息。

以下是工作代码 -

client.isRegisteredUser("911234567890@c.us").then(function(isRegistered) {
    if(isRegistered) {
        client.sendMessage("911234567890@c.us", "hello");
    }
})  
于 2021-06-08T21:32:58.890 回答
1

如果您有联系人姓名,则可以避免chatId手动创建并从联系人对象中获取它,方法是获取所有联系人并通过nameor找到正确的一个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')
于 2021-08-27T15:48:35.787 回答