Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
whatsapp-api-js
Advanced tools
A Whatsapp's Official API helper for Node.js (WIP)
Whatsapp's Official API is currently on beta acccess. To participate, you can fill this form.
This project is a work in progress. Breaking changes are expected from mid-version to mid-version until it hits version 1.0.0.
To know what changes between updates, check out the releases on Github.
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, Handlers, Types } = require("whatsapp-api-js");
const { Text, Media, Contacts } = Types;
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 Handlers.post(JSON.parse(e.data), onMessage);
}
function onMessage(phoneID, phone, message, name, raw_data) {
let promise;
if (message.type === "text") promise = Whatsapp.sendMessage(phoneID, phone, new Text(`*${name}* said:\n\n${message.text.body}`));
if (message.type === "image") promise = Whatsapp.sendMessage(phoneID, phone, new Media.Image(message.image.id, true, `Nice photo, ${name}`));
if (message.type === "document") promise = Whatsapp.sendMessage(phoneID, phone, new Media.Document(message.document.id, true, undefined, "Our document"));
if (message.type === "contacts") promise = Whatsapp.sendMessage(phoneID, phone, new Contacts.Contacts(
[
new Contacts.Name(name, "First name", "Last name"),
new Contacts.Phone(phone),
new Contacts.Birthday("2022", "04", "25"),
],
[
new Contacts.Name("John", "First name", "Last name"),
new Contacts.Organization("Company", "Department", "Title"),
new Contacts.Url("https://www.google.com", "WORK"),
]
));
if (promise) promise.then(res => res.json()).then(console.log);
Whatsapp.markAsRead(phoneID, message.id);
}
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 { Handlers } = require("whatsapp-api-js");
// Assuming get is called on a GET request to your server
function get(e) {
// The Handlers work with any middleware, as long as you pass the correct data
return Handlers.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 package documentation is available in whatsappapijs.web.app.
Even though the code already supports all the message types, there's still a long way to go. I will keep updating it until I like how it works.
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
The npm package whatsapp-api-js receives a total of 0 weekly downloads. As such, whatsapp-api-js popularity was classified as not popular.
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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.