【问题标题】:How to send whatsapp message in whatsapp-web.js如何在 whatsapp-web.js 中发送 whatsapp 消息
【发布时间】:2021-03-17 07:22:25
【问题描述】:

我正在使用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中向手机号码发送新消息??

【问题讨论】:

    标签: javascript node.js whatsapp


    【解决方案1】:

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

    随心所欲地使用它。

    【讨论】:

      【解决方案2】:

      这是我在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");
          }
      });
      
      

      【讨论】:

      • SyntaxError: await 仅在异步函数和模块的顶层主体中有效
      • 您应该将这些代码放在async 函数中。我已经更新了答案
      • 抱歉这么晚才问,但你能告诉我为什么我收到错误“end_number is not defined”吗?
      • @Izlia 很抱歉,你给出的错误真的很模糊,你能否将你的代码添加到“新的stackoverflow问题”中,以便我们能够清楚地理解并回答?!!跨度>
      【解决方案3】:

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

      以下是工作代码-

      client.isRegisteredUser("911234567890@c.us").then(function(isRegistered) {
          if(isRegistered) {
              client.sendMessage("911234567890@c.us", "hello");
          }
      })  
      

      【讨论】:

        【解决方案4】:

        如果您有联系人姓名,您可以避免手动创建chatId,而是从联系人对象中获取它,方法是获取所有联系人并通过namepushname 找到正确的一个。

        
          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')
        

        【讨论】:

          猜你喜欢
          • 2023-01-15
          • 2021-08-26
          • 1970-01-01
          • 1970-01-01
          • 2018-09-01
          • 2019-11-13
          • 2018-11-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多