From dfb862614c02efd07b267d375d2b8f2749a49e23 Mon Sep 17 00:00:00 2001 From: purpshell Date: Tue, 23 Aug 2022 20:43:15 +0300 Subject: [PATCH] Types, disabling of templates and example update --- example.js | 9 ++---- index.d.ts | 5 ++-- src/structures/Buttons.js | 14 +++++---- src/structures/List.js | 63 ++++++++++++++++++++++++++++----------- 4 files changed, 58 insertions(+), 33 deletions(-) diff --git a/example.js b/example.js index ab8c740..eb89222 100644 --- a/example.js +++ b/example.js @@ -192,20 +192,15 @@ client.on('message', async msg => { let button = new Buttons( 'Button body', [ - { body: 'whatsapp-web.js', url: 'https://wwebjs.dev/' }, - { body: 'Call me', number: '+1 (805) 457-4992' }, - { body: 'third special button', number: '+1 (202) 968-6161' },// Limited to 2 especial buttons, this one will be ignored { body: 'Some text' }, - { body: 'Another text' }, - { body: 'Another another text' }, - { body: 'Fourth button' }// Limited to 3 regular buttons, this one will be ignored + { body: 'Try clicking me (id:test)', id: 'test'}, ], 'title', 'footer' ); client.sendMessage(msg.from, button); } else if (msg.body === '!list') { - let sections = [{title:'sectionTitle',rows:[{title:'ListItem1', description: 'desc'},{title:'ListItem2'}]}]; + let sections = [{title:'sectionTitle',rows:[{title:'ListItem1', description: 'desc'},{title: 'Try clicking me (id: test)', }]}]; let list = new List('List body','btnText',sections,'Title','footer'); client.sendMessage(msg.from, list); } else if (msg.body === '!reaction') { diff --git a/index.d.ts b/index.d.ts index edf4684..f02acc9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,6 +2,7 @@ import { EventEmitter } from 'events' import { RequestInit } from 'node-fetch' import puppeteer from 'puppeteer' +import { ButtonSpec, FormattedButtonSpec } from './src/structures/Buttons' declare namespace WAWebJS { @@ -1342,11 +1343,11 @@ declare namespace WAWebJS { /** Message type Buttons */ export class Buttons { body: string | MessageMedia - buttons: Array<{ buttonId: string; buttonText: {displayText: string}; type: number }> + buttons: FormattedButtonSpec title?: string | null footer?: string | null - constructor(body: string, buttons: Array<{ id?: string; body: string }>, title?: string | null, footer?: string | null) + constructor(body: string, buttons: Array, title?: string | null, footer?: string | null) } /** Message type Reaction */ diff --git a/src/structures/Buttons.js b/src/structures/Buttons.js index e35e609..06bfa3d 100644 --- a/src/structures/Buttons.js +++ b/src/structures/Buttons.js @@ -7,8 +7,8 @@ const MessageMedia = require('./MessageMedia'); * @typedef {Object} ButtonSpec * @property {string} body - The text to show on the button. * @property {string=} id - Custom ID to set on the button. A random one will be generated if one is not passed. - * @property {string=} url - Custom URL to set on the button. Optional and will change the type of the button - * @property {string=} number - Custom URL to set on the button. Optional and will change the type of the button + * @ property {string=} url - Custom URL to set on the button. Optional and will change the type of the button + * @ property {string=} number - Custom URL to set on the button. Optional and will change the type of the button */ /** @@ -78,21 +78,23 @@ class Buttons { return buttons.map((button, index) => { if (button.url && button.number && button.id) throw 'Only pick one of the following (url/number/id)'; if (button.number) { - return { + throw 'number buttons are not supported yet'; + /* return { index, callButton: { displayText: button.body, phoneNumber: button.number || '' } - }; + }; */ } else if (button.url) { - return { + throw 'URL buttons are not supported yet'; + /* return { index, urlButton: { displayText: button.body, url: button.url || '' } - }; + }; */ } else { return { index, diff --git a/src/structures/List.js b/src/structures/List.js index 9f096ab..75abff9 100644 --- a/src/structures/List.js +++ b/src/structures/List.js @@ -2,16 +2,38 @@ const Util = require('../util/Util'); +/** + * Section spec used in List constructor + * @typedef {Object} SectionSpec + * @property {string=} title - The title of the section, can be empty on the first section only. + * @property {RowSpec[]} rows - The rows of the section. + */ + +/** + * Row spec used in List constructor + * @typedef {Object} RowSpec + * @property {string} title - The text to show on the row. + * @property {string=} id - Custom ID to set on the row. A random one will be generated if one is not passed. + * @property {string=} description - Custom description for the row, will appear after clicked in the list response message (appended) + */ + +/** + * Formatted section spec + * @typedef {Object} FormattedSectionSpec + * @property {string} title + * @property {{rowId: string; title: string; description: string}[]} rows + */ + /** * Message type List */ class List { /** - * @param {string} body - * @param {string} buttonText - * @param {Array} sections - * @param {string?} title - * @param {string?} footer + * @param {string} body - A text body, no media. + * @param {string} buttonText - The text to put on the click to open button. + * @param {Array} sections - The sections of the list + * @param {string?} title - Custom boldfaced title property + * @param {string?} footer - Custom footer added in a small font to the end of the message */ constructor(body, buttonText, sections, title, footer) { /** @@ -49,23 +71,28 @@ class List { /** * Creates section array from simple array - * @param {Array} sections - * @returns {Array} - * @example - * Input: [{title:'sectionTitle',rows:[{id:'customId', title:'ListItem2', description: 'desc'},{title:'ListItem2'}]}}] - * Returns: [{'title':'sectionTitle','rows':[{'rowId':'customId','title':'ListItem1','description':'desc'},{'rowId':'oGSRoD','title':'ListItem2','description':''}]}] + * @param {Array} sections + * @returns {Array} */ - _format(sections){ - if(!sections.length){throw '[LT02] List without sections';} - if(sections.length > 1 && sections.filter(s => typeof s.title == 'undefined').length > 1){throw '[LT05] You can\'t have more than one empty title.';} - return sections.map( (section) =>{ - if(!section.rows.length){throw '[LT03] Section without rows';} + _format(sections) { + if(!sections.length) { + throw '[LT02] List without sections'; + } + if(sections.length > 1 && sections.filter(section => (typeof section.title == 'undefined' )|| section.title == '' ).length > 1) { + throw '[LT05] You can\'t have more than one empty title.'; + } + return sections.map((section, index) => { + if(!section.rows.length) { + throw '[LT03] Section without rows'; + } return { title: section.title ? section.title : undefined, - rows: section.rows.map( (row) => { - if(!row.title){throw '[LT04] Row without title';} + rows: section.rows.map((row, rowIndex) => { + if (!row.title) { + throw `[LT04] Row without title at section index ${index} and row index ${rowIndex}`; + } return { - rowId: row.id ? row.id : Util.generateHash(6), + rowId: row.id ? row.id : Util.generateHash(8), title: row.title, description: row.description ? row.description : '' };