繁体   English   中英

通过JS发送whatsapp消息

[英]Send whatsapp message via JS

我想直接向特定号码发送消息。 我尝试了几种方法,但没有成功。

第一种方法它打开 Whatsapp web。 我想要它直接到移动whatsapp号码。

function send_handle(){

  let num=document.getElementById("number").value;

  let msg= document.getElementById("msg").value;

    let name= document.getElementById("name").value;
  
  var win = window.open(`https://wa.me/${num}?text=I%27m%20api%20msg%20hello%20${name}%20friend%20${msg}`, '_blank');
 // win.focus();
}
        <div>
          <h3>whatsapp send app </h3>
            <h6>add number without space like 17272912606 not <strike>+1 (727) 2912606 </strike></h6>
  <input id="number" type="numric" placeholder="phone 966506666666" >
   <input id="name" type="text" placeholder="name" >
   <input id="msg" type="text" placeholder="type msg" >
<button onclick="send_handle()">send</button>
</div>

第二种方法:whatsappWebJs不显示任何内容

import * as whatsappWebJs from "https://cdn.skypack.dev/whatsapp-web.js";
const { Client } = whatsappWebJs;
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!');

  // Number where you want to send the message.
 const number = "+91********";

  // 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);
});

似乎您忘记使用 Whats App Js 初始化您的客户端。 此外,我建议使用 qr-terminal 以获得良好的 QR 格式。

 const qrcode = require("qrcode-terminal"); // Add this for QR format in terminal client.on('qr', qr => { qrcode.generate(qr, { small: true }); }); // rest of your code //.... // Use initialize to start with the client client.initialize();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM