whatsapp-api-js
Advanced tools
Comparing version 0.4.3-beta.2 to 0.5.0-beta.0
10
fetch.js
@@ -20,8 +20,15 @@ const { Contacts } = require('./types/contacts'); | ||
* @param {(Text|Audio|Document|Image|Sticker|Video|Location|Contacts|Interactive|Template)} object Each type of message requires a specific type of object, for example, the "image" type requires an url and optionally captions. Use the constructors for each specific type of message (contacts, interactive, location, media, template, text) | ||
* @param {String} context The message id to reply to | ||
* @returns {Promise} The fetch promise | ||
*/ | ||
function sendMessage(token, v, phoneID, to, object) { | ||
function sendMessage(token, v, phoneID, to, object, message_id) { | ||
const type = object._; | ||
delete object._; | ||
const reply = message_id ? { | ||
context: { | ||
message_id, | ||
} | ||
} : {}; | ||
const body = JSON.stringify({ | ||
@@ -31,2 +38,3 @@ messaging_product: "whatsapp", | ||
to, | ||
...reply, | ||
// If the object contains its name as a property, it means it's an array, use it, else use the class | ||
@@ -33,0 +41,0 @@ // This horrible thing comes from Contacts, the only API element which must be an array instead of an object... |
11
index.js
@@ -21,6 +21,6 @@ const { Contacts } = require('./types/contacts'); | ||
* @param {String} token The API token, given at setup. It can be either a temporal token or a permanent one. | ||
* @param {String} v The version of the API, defaults to v13.0 | ||
* @param {String} v The version of the API, defaults to v14.0 | ||
* @throws {Error} If token is not specified | ||
*/ | ||
constructor(token, v = "v13.0") { | ||
constructor(token, v = "v14.0") { | ||
if (!token) throw new Error("Token must be specified"); | ||
@@ -37,2 +37,3 @@ this.token = token; | ||
* @param {(Text|Audio|Document|Image|Sticker|Video|Location|Contacts|Interactive|Template)} object A Whatsapp component, built using the corresponding module for each type of message. | ||
* @param {String} [context] The message ID of the message to reply to | ||
* @returns {Promise} The fetch promise | ||
@@ -42,10 +43,8 @@ * @throws {Error} If phoneID is not specified | ||
* @throws {Error} If object is not specified | ||
* @throws {Error} If object is not a valid whatsapp-api-js@0.0.4 component | ||
*/ | ||
sendMessage(phoneID, to, object) { | ||
sendMessage(phoneID, to, object, context = "") { | ||
if (!phoneID) throw new Error("Phone ID must be specified"); | ||
if (!to) throw new Error("To must be specified"); | ||
if (!object) throw new Error("Message must have a message object"); | ||
if (!object._) throw new Error("There has been a breaking update in whatsapp-api-js@0.0.4 and @0.1.0, please check the documentation for more information on how to use the new version, or downgrade using 'npm i whatsapp-api-js@0.0.3'. Sorry for any inconvenience :/"); | ||
return fetch.sendMessage(this.token, this.v, phoneID, to, object); | ||
return fetch.sendMessage(this.token, this.v, phoneID, to, object, context); | ||
} | ||
@@ -52,0 +51,0 @@ |
{ | ||
"name": "whatsapp-api-js", | ||
"version": "0.4.3-beta.2", | ||
"version": "0.5.0-beta.0", | ||
"author": "Secreto31126", | ||
@@ -5,0 +5,0 @@ "description": "A Whatsapp Official API helper for Node.js", |
@@ -91,3 +91,3 @@ # whatsapp-api-js | ||
Since @0.4.0, the module will check if fetch is available, and fallback to "cross-fetch" if not. | ||
Since @0.4.2, the module will check if fetch is available, and fallback to "cross-fetch" if not. | ||
This will allow the same script to be run in many different enviroments, such as a web browser, Deno, | ||
@@ -117,2 +117,8 @@ and maybe even TypeScript, idk about this last one ¯\\\_(ツ)\_/¯. | ||
## Beta releases | ||
Install the latest beta realease with `npm install whatsapp-api-js@beta`. | ||
As any beta, it is 110% likely to break. I also use this tag to test npm releases. | ||
Use it at your own risk. | ||
## Comments | ||
@@ -119,0 +125,0 @@ |
@@ -13,11 +13,11 @@ /** | ||
* @param {String} body The text of the text message which can contain formatting and URLs which begin with http:// or https:// | ||
* @param {Boolean} preview_url By default, WhatsApp recognizes URLs and makes them clickable, but you can also include a preview box with more information about the link. Set this field to true if you want to include a URL preview box. Defaults to false. | ||
* @param {Boolean} preview_url By default, WhatsApp recognizes URLs and makes them clickable, but you can also include a preview box with more information about the link. Set this field to true if you want to include a URL preview box. | ||
* @throws {Error} If body is not provided | ||
* @throws {Error} If body is over 4096 characters | ||
*/ | ||
constructor(body, preview_url = false) { | ||
constructor(body, preview_url) { | ||
if (!body) throw new Error("Text must have a body object"); | ||
if (body.length > 4096) throw new Error("Text body must be less than 4096 characters"); | ||
this.body = body; | ||
if (preview_url) this.preview_url = preview_url; | ||
if (preview_url !== undefined) this.preview_url = preview_url; | ||
this._ = "text"; | ||
@@ -24,0 +24,0 @@ } |
60632
1316
128