How to send whatsapp message via Javascript

Viewed 72956

Hi I would like to know if there is something to send a whatsapp message using javascript or something I was searching but I did not find any new post. This is not for any special purpose. I found this but it only works if you have whatsapp web. I was thinking on clicking on a link to send a default message to a default number

<a href="https://api.whatsapp.com/send?phone=1111111111">Send Message</a>
6 Answers

Just make use of this function in web browser to get it running as you want.

Note: You need to run the code manually through browser console with an opened conversation in WhatsApp (web version).

function sendMessage (message) {
  window.InputEvent = window.Event || window.InputEvent;

  var event = new InputEvent('input', {
    bubbles: true
  });

  var textbox = document.querySelector('div._2S1VP');

  textbox.textContent = message;
  textbox.dispatchEvent(event);

  document.querySelector("button._35EW6").click();
}

My gist

Original code

Thanks to Gabriel!

Important: If you're trying to send a message directly from URL (i.e using API), you cant. Its impossible to send WhatsApp messages without user interaction.

I find this way is a better way to send a msg to WhatsApp unknowing number.

// github: omar-bakhsh
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>

You can do this:

<a href="https://api.whatsapp.com/send?phone=1111111111&text=Hi">Send Message</a>

On the WhatsApp developers hub, they demonstrated a walkthrough guide on how to opt-in for a developer account on meta, and create an app so can make api calls if you want to use WhatsApp programmatically,

Short version:

  • Register as a Meta Developer
  • Enable two-factor authentication for your account
  • Create a Meta App: Go to developers.facebook.com > My Apps > Create App.
  • Select the "Business" type and follow the prompts on your screen.
  • Scroll down to find the "WhatsApp" product and click Set up.
  • Select an existing Business Manager, if you don't the wizard will guide you to create one.
  • Next screen will show you a demo.
  • If you need a listener when your number receive a message or reply, you should register webhooks.
  • Add your phone number to use it programmatically. and it shouldn't be connected with WhatsApp at all, neither personal or business.

for more details follow the getting started guide:

https://business.whatsapp.com/developers/developer-hub

It's impossible to do it with HTML link. Whatsapp has no official API. But you can find (or write it by yourself) some script to emulate user actions on the site web.whatsapp.com. For example, this one (I did not test it).

Related