New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

whatsapp-api-js

Package Overview
Dependencies
Maintainers
1
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

whatsapp-api-js - npm Package Compare versions

Comparing version 0.5.0-beta.0 to 0.5.0-beta.1

82

fetch.js

@@ -11,2 +11,53 @@ const { Contacts } = require('./types/contacts');

/**
* Request API object
*
* @property {String} messaging_product The messaging product (always "whatsapp")
* @property {String} type The type of message
* @property {String} to The user's phone number
* @property {Object} [context] The message to reply to
* @property {String} context.message_id The message id to reply to
* @property {String} [text] The text object stringified to send
* @property {String} [audio] The audio object stringified to send
* @property {String} [document] The document object stringified to send
* @property {String} [image] The image object stringified to send
* @property {String} [sticker] The sticker object stringified to send
* @property {String} [video] The video object stringified to send
* @property {String} [location] The location object stringified to send
* @property {String} [contacts] The contacts object stringified to send
* @property {String} [interactive] The interactive object stringified to send
* @property {String} [template] The template object stringified to send
*/
class Request {
/**
* Create a Request object for the API
*
* @param {(Text|Audio|Document|Image|Sticker|Video|Location|Contacts|Interactive|Template)} object The object to send
* @param {String} to The user's phone number
* @param {String} context The message_id to reply to
*/
constructor(object, to, context) {
this.messaging_product = "whatsapp";
this.type = object._;
delete object._;
this.to = to;
if (context) this.context = { message_id: context };
// If the object contains its name as a property, it means it's an array, use it, else use the class
// This horrible thing comes from Contacts, the only API element which must be an array instead of an object...
this[this.type] = JSON.stringify(object[this.type] ?? object);
}
}
/**
* The sendMessage response object
*
* @package
* @ignore
* @typedef {Object} SendMessageResponse
* @property {Promise} promise The fetch promise
* @property {Request} request The request sent to the server
*/
/**
* Make a message post request to the API

@@ -22,26 +73,9 @@ *

* @param {String} context The message id to reply to
* @returns {Promise} The fetch promise
* @returns {SendMessageResponse} An object with the sent request and the fetch promise
*/
function sendMessage(token, v, phoneID, to, object, message_id) {
const type = object._;
delete object._;
function sendMessage(token, v, phoneID, to, object, context) {
const request = new Request(object, to, context);
const reply = message_id ? {
context: {
message_id,
}
} : {};
const body = JSON.stringify({
messaging_product: "whatsapp",
type,
to,
...reply,
// If the object contains its name as a property, it means it's an array, use it, else use the class
// This horrible thing comes from Contacts, the only API element which must be an array instead of an object...
[type]: JSON.stringify(object[type] ?? object),
});
// Make the post request
return req(`https://graph.facebook.com/${v}/${phoneID}/messages`, {
const promise = req(`https://graph.facebook.com/${v}/${phoneID}/messages`, {
method: "POST",

@@ -52,4 +86,6 @@ headers: {

},
body,
body: JSON.stringify(request),
});
return { promise, request };
}

@@ -169,2 +205,2 @@

module.exports = { sendMessage, readMessage, makeQR, getQR, updateQR, deleteQR };
module.exports = { sendMessage, readMessage, makeQR, getQR, updateQR, deleteQR, Request };

@@ -0,1 +1,3 @@

// Most of these imports are here only for types checks
const { Contacts } = require('./types/contacts');

@@ -8,3 +10,4 @@ const { Interactive } = require("./types/interactive");

const fetch = require('./fetch');
const api = require('./fetch');
const { Request } = api;

@@ -30,4 +33,25 @@ /**

}
/**
* Callback function after a sendMessage request is sent
*
* @callback Logger
* @param {Request} request The sent object to the server
* @param {String} phoneID The bot's phoneID from where the message was sent
*/
/**
* Set a callback function for sendMessage
*
* @param {Logger} callback The callback function to set
* @returns {WhatsAppAPI} The API object, for chaining
* @throws {Error} If callback is truthy and is not a function
*/
logSentMessages(callback) {
if (callback && typeof callback !== "function") throw new Error("Callback must be a function");
this._debug = callback;
return this;
}
/**
* Send a Whatsapp message

@@ -43,3 +67,3 @@ *

* @throws {Error} If object is not specified
*/
*/
sendMessage(phoneID, to, object, context = "") {

@@ -49,3 +73,6 @@ if (!phoneID) throw new Error("Phone ID must be specified");

if (!object) throw new Error("Message must have a message object");
return fetch.sendMessage(this.token, this.v, phoneID, to, object, context);
const data = api.sendMessage(this.token, this.v, phoneID, to, object, context);
if (this._debug) this._debug(data.request, phoneID);
return data.promise;
}

@@ -65,3 +92,3 @@

if (!messageId) throw new Error("To must be specified");
return fetch.readMessage(this.token, this.v, phoneID, messageId);
return api.readMessage(this.token, this.v, phoneID, messageId);
}

@@ -84,3 +111,3 @@

if (!["png", "svg"].includes(format)) throw new Error("Format must be either 'png' or 'svg'");
return fetch.makeQR(this.token, this.v, phoneID, message, format);
return api.makeQR(this.token, this.v, phoneID, message, format);
}

@@ -98,3 +125,3 @@

if (!phoneID) throw new Error("Phone ID must be specified");
return fetch.getQR(this.token, this.v, phoneID, id);
return api.getQR(this.token, this.v, phoneID, id);
}

@@ -117,3 +144,3 @@

if (!message) throw new Error("Message must be specified");
return fetch.updateQR(this.token, this.v, phoneID, id, message);
return api.updateQR(this.token, this.v, phoneID, id, message);
}

@@ -133,3 +160,3 @@

if (!id) throw new Error("ID must be specified");
return fetch.deleteQR(this.token, this.v, phoneID, id);
return api.deleteQR(this.token, this.v, phoneID, id);
}

@@ -136,0 +163,0 @@ }

{
"name": "whatsapp-api-js",
"version": "0.5.0-beta.0",
"version": "0.5.0-beta.1",
"author": "Secreto31126",

@@ -5,0 +5,0 @@ "description": "A Whatsapp Official API helper for Node.js",

@@ -35,2 +35,4 @@ # whatsapp-api-js

Whatsapp.logSentMessages((message, bot) => console.log(`Bot ${bot} just sent: ${JSON.stringify(message)}`));
// Assuming post is called on a POST request to your server

@@ -37,0 +39,0 @@ function post(e) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc