Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
whatsapp-business
Advanced tools
Readme
Node.js connector for WhatsApp Business Cloud API, with TypeScript support.
This project offers a solution to easily interact with WhatsApp Business Cloud API with Heavy integration testing with real API calls to support implementation stability. Built with Axios and no other extra dependency!
The connector is fully typed, tested and documented!
npm install whatsapp-business
yarn add whatsapp-business
Most methods accept JS objects. These can be populated using parameters specified by WhatsApp's API documentation or following the typescript schema.
import { WABAClient, WABAErrorAPI } from "whatsapp-business";
//You cant get it from the meta for developers app administration
const client = new WABAClient({
accountId: "<YOUR_ACCOUNT_ID>",
apiToken: "<YOUR_API_TOKEN>",
phoneId: "<YOUR_BUSINESS_PHONE_ID>",
});
const foo = async () => {
try {
const res = await client.getBusinessPhoneNumbers();
console.log(res);
} catch (err) {
const error: WABAErrorAPI = err;
console.error(error.message);
}
};
foo();
You can send a text message
const sendTextMessage = async (body: string, to: string) => {
try {
const res = await client.sendMessage({ to, type: "text", text: { body } });
console.log(res);
} catch (err) {
const error: WABAErrorAPI = err;
console.error(error.message);
}
};
or an image
const sendPictureMessage = async ({ link, caption }: MediaObject, to: string) => {
try {
const res = await client.sendMessage({ to, type: "image", image: { link, caption } });
console.log(res);
} catch (err) {
const error: WABAErrorAPI = err;
console.error(error.message);
}
};
sendPictureMessage(
{ link: "<url_link_to_your_image>", caption: "<image_description>" },
"<PHONE_NUMBER>"
);
The webhook client will handle the subscription and setup for the webhooks. You must have an HTTPS connection and add the server URL in your application management.
For more info, checks the docs here.
import { WebhookClient, WABAClient } from "./index";
//The token and path must match the values you set on the application management
const webhookClient = new WebhookClient({
token: "<YOUR_VALIDATION_TOKEN>",
path: "/whatsapp/webhook",
port: 8080,
});
const wabaClient = new WABAClient({
accountId: "<ACCOUNT_ID>",
phoneId: "<PHONE_ID>",
apiToken: "<API_TOKEN>",
});
//Starts a server and triggers the received functions based on the webhook event type
webhookClient.initWebhook({
onStartListening: () => {
console.log("Server started listening");
},
onTextMessageReceived: async (payload, contact) => {
try {
const messageId = payload.id.toString();
const contactNumber = contact.wa_id;
//Mark message as read
await wabaClient.markMessageAsRead(messageId);
//React to message
await wabaClient.sendMessage({
to: contactNumber,
type: "reaction",
reaction: { message_id: messageId, emoji: "😄" },
});
//Respond to message
await wabaClient.sendMessage({
type: "text",
to: contactNumber,
text: { body: "Ok!" },
//This is optional, it enables reply-to feature
context: {
message_id: messageId,
},
});
} catch (err) {
console.log(err);
}
},
});
You can also provide your own express app:
import { WebhookClient } from "./index";
import express from "express";
const myApp = express();
const webhookClient = new WebhookClient({
token: "<YOUR_VALIDATION_TOKEN>",
path: "/whatsapp/webhook",
expressApp: {
//Set to false if you want to initialize the server yourself
//Otherwise, it will start listening when firing initWebhook()
shouldStartListening: false,
app: myApp,
},
});
myApp.listen(8080, () => {
console.log("My server nows listens to whatsapp webhooks");
});
If you don't provide a express app the client will create a default app on its own, which you can later access:
import { WebhookClient } from "./index";
const webhookClient = new WebhookClient({
token: "<YOUR_VALIDATION_TOKEN>",
path: "/whatsapp/webhook",
port: 8080,
});
const app = webhookClient.expressApp.app;
//Your configuration...
app.set("trust proxy", true);
webhookClient.initWebhook({
onStartListening: () => {
console.log("Server started listening");
},
});
Cloud API |
---|
|
|
|
|
|
|
Webhooks |
---|
|
|
Business Management API |
---|
Currently working on |
Analytics API |
---|
Planning to add future support |
This project uses typescript. Resources are stored in 2 key structures:
Contributions are encouraged, I will review any incoming pull requests.
If you found this project interesting or useful, you would help so much by giving this project a star. Thank you!
FAQs
Node.js connector for the WhatsApp Business APIs with TypeScript support, integration tests and more.
The npm package whatsapp-business receives a total of 164 weekly downloads. As such, whatsapp-business popularity was classified as not popular.
We found that whatsapp-business demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.