如何在whatsapp-web.js中发送whatsapp消息

我正在使用 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-web.js中发送whatsapp消息的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>