
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
whatsapp-api-js
Advanced tools
A Whatsapp Official API helper for Node.js (WIP)
Whatsapp's Official API is currently on beta acccess. To participate, you can fill this form.
First, you need a Facebook app with the Whatsapp API activated. You can create your first app following this steps. Get the API token, either a temporal or a permanent one.
In your server you can install the module using npm:
npm install whatsapp-api-js
Now you can write code like this:
const WhatsAppAPI = require("whatsapp-api-js").WhatsApp;
const Handler = require("whatsapp-api-js").Handlers;
const { name, phone, birthday } = require("whatsapp-api-js").Contacts;
const Token = "YOUR_TOKEN";
const Whatsapp = new WhatsAppAPI(Token);
// Assuming post is called on a POST request to your server
function post(e) {
// The Handlers work with any middleware, as long as you pass the correct data
return Handler.post(JSON.parse(e.data), onMessage);
}
function onMessage(phoneID, phone, message, name, raw_data) {
if (message.type === "text") Whatsapp.sendTextMessage(phoneID, phone, `*${name}* said:\n\n${message.text.body}`);
if (message.type === "image") Whatsapp.sendImageMessage(phoneID, phone, message.image.id, true, `Nice photo, ${name}`);
if (message.type === "document") Whatsapp.sendDocumentMessage(phoneID, phone, message.document.id, true, undefined, "Our document");
if (message.type === "video") Whatsapp.sendVideoMessage(phoneID, phone, "a_video_url_goes_here");
if (message.type === "sticker") Whatsapp.sendStickerMessage(phoneID, phone, "a_sticker_url_goes_here");
if (message.type === "audio") Whatsapp.sendAudioMessage(phoneID, phone, message.audio.id, true);
if (message.type === "location") Whatsapp.sendLocationMessage(phoneID, phone, 0, 0);
if (message.type === "contacts") Whatsapp.sendContactMessage(phoneID, phone, [
new name(name, "First name", "Last name"),
new phone(phone),
new birthday("2022", "04", "25"),
]);
}
To recieve the post requests on message, you must setup the webhook at your Facebook app. While setting up, you will be asked a Verify Token. This can be any string you want.
The app also has a GET wizard for the webhook authentication:
const Handler = require("whatsapp-api-js").Handlers;
// Assuming get is called on a GET request to your server
function get(e) {
return Whatsapp.get(JSON.parse(e.params), "your_verify_token");
}
Once you are done, click administrate, and set the webhook to subscribe to messages only. There might be a future update to support the other types of subscriptions.
And that's it! Now you have a functioning Whatsapp Bot connected to your server. The code still doesn't support 100% of the functionalities, like interactive and template messages, but I am working on adding them ASAP.
Also, if you are interested in Google App Script support, check out Secreto31126/whatsapp-api-google-app.script.
FAQs
A TypeScript server agnostic Whatsapp's Official API framework
We found that whatsapp-api-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.