WhatsApp Web API JS
This library allows you to make a WhatsApp bot in JS or TS. The library implements the WhatsApp Web API and therefore doesn't use puppeteer, selenium, etc.
The library is based on Sigalor's WhatsApp Web Python library.
Installation
npm install -S @noamalffasy/js-whatsapp
or
yarn add @noamalffasy/js-whatsapp
Usage
Setting up
import Whatsapp from "@noamalffasy/js-whatsapp";
const wap = new Whatsapp();
Login
By using the code shown above, a QR Code should be generated automatically and put in the current directory.
Once scanned, your keys will be put in the same directory and saved for the next session.
Auto login is supported, in order to login automatically you need to change:
const wap = new Whatsapp();
to:
const wap = new Whatsapp(true);
Handle messages
As of now, 2 events are supported: ready
and message
.
import Whatsapp from "@noamalffasy/js-whatsapp";
const wap = new Whatsapp();
wap.on("ready", () => {
// Your code goes here
});
wap.on("message", msg => {
// Your code goes here
});
Sending text messages
WhatsApp internally uses IDs called Jids:
- Chats: [country code][phone number]@s.whatsapp.net
- Groups: [country code][phone number of creator]-[timestamp of group creation]@g.us
- Broadcast Channels: [timestamp of group creation]@broadcast
If you'd like to see your contacts' and chats' Jids you can access the contactList
or the chatList
of the wap
class.
Once you've got your Jids you can send messages like so:
import Whatsapp from "@noamalffasy/js-whatsapp";
const wap = new Whatsapp();
wap.sendTextMessage("[text to send]", "[jid]");
Sending quoted messages
As of now, the library supports only text messages so the example is only for text.
In order to quote a message you need to get its ID, the sender's Jid and the message's text.
import Whatsapp from "@noamalffasy/js-whatsapp";
const wap = new Whatsapp();
wap.sendQuotedTextMessage(
"[text to send]",
"[jid of group or contact to send the message to]",
"[the jid of the message's sender]",
"[the quoted message's content]",
"[the quoted message's ID]"
);
Legal
This code is in no way affiliated with, authorized, maintained, sponsored or endorsed by WhatsApp or any of its affiliates or subsidiaries. This is an independent and unofficial software. Use at your own risk.