whatsapp-api-js
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -103,4 +103,3 @@ const { Contacts } = require('./types/contacts'); | ||
* @property {ButtonComponent} Types.Template.ButtonComponent The API ButtonComponent type object | ||
* @property {UrlButton} Types.Template.UrlButton The API UrlButton type object | ||
* @property {PayloadButton} Types.Template.PayloadButton The API PayloadButton type object | ||
* @property {ButtonParameter} Types.Template.ButtonParameter The API ButtonParameter type object | ||
* @property {HeaderComponent} Types.Template.HeaderComponent The API HeaderComponent type object | ||
@@ -107,0 +106,0 @@ * @property {BodyComponent} Types.Template.BodyComponent The API BodyComponent type object |
{ | ||
"name": "whatsapp-api-js", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "A Whatsapp Official API helper for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,4 +9,6 @@ # whatsapp-api-js | ||
2. This project is a work in progress. Breaking changes are expected from version to version until we hit version 1.0.0. | ||
2. This project is a work in progress. Breaking changes are expected from mid-version to mid-version until it hits version 1.0.0. | ||
3. To know what changes between updates, check out the [releases on Github](https://github.com/Secreto31126/whatsapp-api-js/releases). | ||
## Set up | ||
@@ -91,3 +93,2 @@ | ||
The package documentation is available in [whatsappapijs.web.app](https://whatsappapijs.web.app/). | ||
Contacts and Interactive are still missing some documentation, will be added soon. | ||
@@ -97,4 +98,4 @@ ## Comments | ||
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. The next step will be adding a documentation website. | ||
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. |
@@ -48,3 +48,12 @@ /** | ||
/** | ||
* Contacts Component | ||
* Address API object | ||
* | ||
* @property {String} [country] The country of the address | ||
* @property {String} [country_code] The country code of the address | ||
* @property {String} [state] The state of the address | ||
* @property {String} [city] The city of the address | ||
* @property {String} [street] The street of the address | ||
* @property {String} [zip] The zip code of the address | ||
* @property {String} [type] The type of the address | ||
* @property {String} _ The type of the object, for internal use only | ||
*/ | ||
@@ -54,3 +63,3 @@ class Address { | ||
* Builds an address object for a contact. | ||
* A contact can contain multiple addresses. | ||
* A contact can contain multiple addresses objects. | ||
* | ||
@@ -78,3 +87,6 @@ * @param {String} [country] Full country name | ||
/** | ||
* Contacts Component | ||
* Birthday API object | ||
* | ||
* @property {String} birthday The birthday of the contact | ||
* @property {String} _ The type of the object, for internal use only | ||
*/ | ||
@@ -100,3 +112,7 @@ class Birthday { | ||
/** | ||
* Contacts Component | ||
* Email API object | ||
* | ||
* @property {String} [email] The email of the contact | ||
* @property {String} [type] The type of the email | ||
* @property {String} _ The type of the object, for internal use only | ||
*/ | ||
@@ -106,3 +122,3 @@ class Email { | ||
* Builds an email object for a contact. | ||
* A contact can contain multiple emails. | ||
* A contact can contain multiple emails objects. | ||
* | ||
@@ -120,3 +136,11 @@ * @param {String} [email] Email address | ||
/** | ||
* Contacts Component | ||
* Name API object | ||
* | ||
* @property {String} formatted_name The formatted name of the contact | ||
* @property {String} [first_name] The first name of the contact | ||
* @property {String} [last_name] The last name of the contact | ||
* @property {String} [middle_name] The middle name of the contact | ||
* @property {String} [suffix] The suffix of the contact | ||
* @property {String} [prefix] The prefix of the contact | ||
* @property {String} _ The type of the object, for internal use only | ||
*/ | ||
@@ -153,3 +177,8 @@ class Name { | ||
/** | ||
* Contacts Component | ||
* Organization API object | ||
* | ||
* @property {String} [company] The company of the contact | ||
* @property {String} [department] The department of the contact | ||
* @property {String} [title] The title of the contact | ||
* @property {String} _ The type of the object, for internal use only | ||
*/ | ||
@@ -173,3 +202,8 @@ class Organization { | ||
/** | ||
* Contacts Component | ||
* Phone API object | ||
* | ||
* @property {String} [phone] The phone number of the contact | ||
* @property {String} [type] The type of the phone number | ||
* @property {String} [wa_id] The WhatsApp ID of the contact | ||
* @property {String} _ The type of the object, for internal use only | ||
*/ | ||
@@ -179,3 +213,3 @@ class Phone { | ||
* Builds a phone object for a contact. | ||
* A contact can contain multiple phones. | ||
* A contact can contain multiple phones objects. | ||
* | ||
@@ -195,3 +229,7 @@ * @param {String} [phone] Phone number, automatically populated with the wa_id value as a formatted phone number | ||
/** | ||
* Contacts Component | ||
* Url API object | ||
* | ||
* @property {String} [url] The URL of the contact | ||
* @property {String} [type] The type of the URL | ||
* @property {String} _ The type of the object, for internal use only | ||
*/ | ||
@@ -201,3 +239,3 @@ class Url { | ||
* Builds an url object for a contact. | ||
* A contact can contain multiple urls. | ||
* A contact can contain multiple urls objects. | ||
* | ||
@@ -204,0 +242,0 @@ * @param {String} [url] URL |
@@ -41,3 +41,5 @@ const Text = require("./text"); | ||
/** | ||
* Interactive Component | ||
* Body API object | ||
* | ||
* @property {String} text The text of the body | ||
*/ | ||
@@ -61,3 +63,5 @@ class Body { | ||
/** | ||
* Interactive Component | ||
* Footer API object | ||
* | ||
* @property {String} text The text of the body | ||
*/ | ||
@@ -81,3 +85,9 @@ class Footer { | ||
/** | ||
* Interactive Component | ||
* Header API object | ||
* | ||
* @property {String} type The type of the header | ||
* @property {String} [text] The text of the parameter | ||
* @property {Image} [image] The image of the parameter | ||
* @property {Document} [document] The document of the parameter | ||
* @property {Video} [video] The video of the parameter | ||
*/ | ||
@@ -104,3 +114,7 @@ class Header { | ||
/** | ||
* Interactive Component | ||
* Action API object | ||
* | ||
* @property {String} button The button text | ||
* @property {Array<Section>} sections The sections of the action | ||
* @property {String} _ The type of the action, for internal use only | ||
*/ | ||
@@ -130,3 +144,6 @@ class ActionList { | ||
/** | ||
* ActionList Component | ||
* Section API object | ||
* | ||
* @property {String} title The title of the section | ||
* @property {Array<Row>} rows The rows of the section | ||
*/ | ||
@@ -154,3 +171,7 @@ class Section { | ||
/** | ||
* Section Component | ||
* Row API object | ||
* | ||
* @property {String} id The id of the row | ||
* @property {String} title The title of the row | ||
* @property {String} [description] The description of the row | ||
*/ | ||
@@ -184,3 +205,6 @@ class Row { | ||
/** | ||
* Interactive Component | ||
* Action API object | ||
* | ||
* @property {Array<Button>} buttons The buttons of the action | ||
* @property {String} _ The type of the action, for internal use only | ||
*/ | ||
@@ -213,3 +237,7 @@ class ActionButtons { | ||
/** | ||
* ActionReplyButtons Component | ||
* Button API object | ||
* | ||
* @property {String} type The type of the button | ||
* @property {String} reply.id The id of the row | ||
* @property {String} reply.title The title of the row | ||
*/ | ||
@@ -216,0 +244,0 @@ class Button { |
@@ -26,4 +26,8 @@ const Text = require("./text"); | ||
const indexes = components.filter(e => e instanceof ButtonComponent).map(e => e.index); | ||
if (indexes.length !== new Set(indexes).size) throw new Error("ButtonComponents must have unique ids"); | ||
const temp = []; | ||
for (let component of components) { | ||
if (component instanceof ButtonComponent) temp.push(...component.build()); | ||
else temp.push(component); | ||
} | ||
components = temp; | ||
@@ -115,48 +119,38 @@ this.name = name; | ||
* @property {String} sub_type The subtype of the component | ||
* @property {String} index The index of the component | ||
* @property {Array<(UrlButton|PayloadButton)>} parameters The parameters of the component | ||
* @property {Array<ButtonParameter>} parameters The ButtonParameters to be used in the build function | ||
* @property {Function} build The function to build the component as a compatible API object | ||
*/ | ||
class ButtonComponent { | ||
/** | ||
* Builds a button component for a Template message | ||
* Builds a button component for a Template message. | ||
* The index of the buttons is defined by the order in which you add them to the Template parameters. | ||
* | ||
* @param {Number} index Position index of the button. You can have up to 3 buttons using index values of 0 to 2. | ||
* @param {String} sub_type Type of button to create. Can be either 'url' or 'quick_reply'. | ||
* @param {...(UrlButton|PayloadButton)} parameters Parameters of the button. | ||
* @param {...String} parameters Parameter for each button. The index of each parameter is defined by the order they are sent to the constructor. | ||
* @throws {Error} If sub_type is not either 'url' or 'quick_reply' | ||
* @throws {Error} If parameters is not provided | ||
* @throws {Error} If parameters has over 3 elements | ||
*/ | ||
constructor(index, sub_type, ...parameters) { | ||
if (!index?.toString()) throw new Error("ButtonComponent must have an index"); | ||
if (!index < 0 || index > 2) throw new Error("ButtonComponent index must be between 0 and 2"); | ||
if (!['quick_reply', 'url'].includes(sub_type)) throw new Error("ButtonComponent sub_type must be either 'quick_reply' or 'url'"); | ||
if (!parameters?.length) throw new Error("ButtonComponent must have at least one parameter"); | ||
constructor(sub_type, ...parameters) { | ||
if (!["url", "quick_reply"].includes(sub_type)) throw new Error("ButtonComponent sub_type must be either 'url' or 'quick_reply'"); | ||
if (!parameters?.length) throw new Error("ButtonComponent must have a parameter at least 1 parameter"); | ||
if (parameters.length > 3) throw new Error("ButtonComponent can only have up to 3 parameters"); | ||
for (const param of parameters) { | ||
if (sub_type === "quick_reply" && param instanceof UrlButton) throw new Error("ButtonComponent of type 'quick_replies' cannot have a UrlButton"); | ||
if (sub_type === "url" && param instanceof PayloadButton) throw new Error("ButtonComponent of type 'url' cannot have a PayloadButton"); | ||
} | ||
const buttonType = sub_type === "url" ? "text" : "payload"; | ||
parameters = parameters.map(e => new ButtonParameter(e, buttonType)); | ||
this.type = "button"; | ||
this.sub_type = sub_type; | ||
this.index = index.toString(); | ||
this.parameters = parameters; | ||
} | ||
} | ||
/** | ||
* Button Parameter API object | ||
* | ||
* @property {String} type The type of the button | ||
* @property {String} text The text of the button | ||
*/ | ||
class UrlButton { | ||
/** | ||
* Builds a url button object for a ButtonComponent | ||
* Generates the buttons components for a Template message. For internal use only. | ||
* | ||
* @param {String} text Developer-provided suffix that is appended to the predefined prefix URL in the template. | ||
* @throws {Error} If url is not provided | ||
* @returns {Array<{ type: String, sub_type: String, index: String, parameters: Array<ButtonParameter> }>} An array of API compatible buttons components | ||
*/ | ||
constructor(text) { | ||
if (!text) throw new Error("UrlButton must have a text"); | ||
this.type = "text"; | ||
this.text = text; | ||
build() { | ||
return this.parameters.map((p, i) => { | ||
return { type: this.type, sub_type: this.sub_type, index: i.toString(), parameters: [p] }; | ||
}); | ||
} | ||
@@ -169,15 +163,20 @@ } | ||
* @property {String} type The type of the button | ||
* @property {String} payload The payload of the button | ||
* @property {String} [text] The text of the button | ||
* @property {String} [payload] The payload of the button | ||
*/ | ||
class PayloadButton { | ||
class ButtonParameter { | ||
/** | ||
* Builds a payload button object for a ButtonComponent | ||
* Builds a button parameter for a ButtonComponent | ||
* | ||
* @param {String} payload Developer-defined payload that is returned when the button is clicked in addition to the display text on the button | ||
* @throws {Error} If payload is not provided | ||
* @param {String} param Developer-provided data that is used to fill in the template. | ||
* @param {String} type The type of the button. Can be either 'text' or 'payload'. | ||
* @throws {Error} If param is not provided | ||
* @throws {Error} If type is not either 'text' or 'payload' | ||
*/ | ||
constructor(payload) { | ||
if (!payload) throw new Error("PayloadButton must have a payload"); | ||
this.type = "payload"; | ||
this.payload = payload; | ||
constructor(param, type) { | ||
if (!param) throw new Error("UrlButton must have a param"); | ||
if (!["text", "payload"].includes(type)) throw new Error("UrlButton must be either 'text' or 'payload'"); | ||
this.type = type; | ||
this[type] = param; | ||
} | ||
@@ -196,3 +195,3 @@ } | ||
* | ||
* @param {...(Text|Currency|DateTime|Image|Document|Video)} parameters Parameters of the body component | ||
* @param {...(Text|Currency|DateTime|Image|Document|Video|Parameter)} parameters Parameters of the body component | ||
*/ | ||
@@ -215,7 +214,7 @@ constructor(...parameters) { | ||
* | ||
* @param {...(Text|Currency|DateTime|Image|Document|Video)} parameters Parameters of the body component | ||
* @param {...(Text|Currency|DateTime|Image|Document|Video|Parameter)} parameters Parameters of the body component | ||
*/ | ||
constructor(...parameters) { | ||
this.type = "body"; | ||
if (parameters) this.parameters = parameters.map(e => new Parameter(e)); | ||
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e)); | ||
} | ||
@@ -257,4 +256,3 @@ } | ||
ButtonComponent, | ||
UrlButton, | ||
PayloadButton, | ||
ButtonParameter, | ||
HeaderComponent, | ||
@@ -261,0 +259,0 @@ BodyComponent, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53089
1155
99