Types, disabling of templates and example update

This commit is contained in:
purpshell
2022-08-23 20:43:15 +03:00
parent c32348d3cc
commit dfb862614c
4 changed files with 58 additions and 33 deletions

View File

@@ -192,20 +192,15 @@ client.on('message', async msg => {
let button = new Buttons( let button = new Buttons(
'Button body', '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: 'Some text' },
{ body: 'Another text' }, { body: 'Try clicking me (id:test)', id: 'test'},
{ body: 'Another another text' },
{ body: 'Fourth button' }// Limited to 3 regular buttons, this one will be ignored
], ],
'title', 'title',
'footer' 'footer'
); );
client.sendMessage(msg.from, button); client.sendMessage(msg.from, button);
} else if (msg.body === '!list') { } 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'); let list = new List('List body','btnText',sections,'Title','footer');
client.sendMessage(msg.from, list); client.sendMessage(msg.from, list);
} else if (msg.body === '!reaction') { } else if (msg.body === '!reaction') {

5
index.d.ts vendored
View File

@@ -2,6 +2,7 @@
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { RequestInit } from 'node-fetch' import { RequestInit } from 'node-fetch'
import puppeteer from 'puppeteer' import puppeteer from 'puppeteer'
import { ButtonSpec, FormattedButtonSpec } from './src/structures/Buttons'
declare namespace WAWebJS { declare namespace WAWebJS {
@@ -1342,11 +1343,11 @@ declare namespace WAWebJS {
/** Message type Buttons */ /** Message type Buttons */
export class Buttons { export class Buttons {
body: string | MessageMedia body: string | MessageMedia
buttons: Array<{ buttonId: string; buttonText: {displayText: string}; type: number }> buttons: FormattedButtonSpec
title?: string | null title?: string | null
footer?: 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<ButtonSpec>, title?: string | null, footer?: string | null)
} }
/** Message type Reaction */ /** Message type Reaction */

View File

@@ -7,8 +7,8 @@ const MessageMedia = require('./MessageMedia');
* @typedef {Object} ButtonSpec * @typedef {Object} ButtonSpec
* @property {string} body - The text to show on the button. * @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=} 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=} 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=} 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) => { return buttons.map((button, index) => {
if (button.url && button.number && button.id) throw 'Only pick one of the following (url/number/id)'; if (button.url && button.number && button.id) throw 'Only pick one of the following (url/number/id)';
if (button.number) { if (button.number) {
return { throw 'number buttons are not supported yet';
/* return {
index, index,
callButton: { callButton: {
displayText: button.body, displayText: button.body,
phoneNumber: button.number || '' phoneNumber: button.number || ''
} }
}; }; */
} else if (button.url) { } else if (button.url) {
return { throw 'URL buttons are not supported yet';
/* return {
index, index,
urlButton: { urlButton: {
displayText: button.body, displayText: button.body,
url: button.url || '' url: button.url || ''
} }
}; }; */
} else { } else {
return { return {
index, index,

View File

@@ -2,16 +2,38 @@
const Util = require('../util/Util'); 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 * Message type List
*/ */
class List { class List {
/** /**
* @param {string} body * @param {string} body - A text body, no media.
* @param {string} buttonText * @param {string} buttonText - The text to put on the click to open button.
* @param {Array<any>} sections * @param {Array<SectionSpec>} sections - The sections of the list
* @param {string?} title * @param {string?} title - Custom boldfaced title property
* @param {string?} footer * @param {string?} footer - Custom footer added in a small font to the end of the message
*/ */
constructor(body, buttonText, sections, title, footer) { constructor(body, buttonText, sections, title, footer) {
/** /**
@@ -49,23 +71,28 @@ class List {
/** /**
* Creates section array from simple array * Creates section array from simple array
* @param {Array<any>} sections * @param {Array<SectionSpec>} sections
* @returns {Array<any>} * @returns {Array<FormattedSectionSpec>}
* @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':''}]}]
*/ */
_format(sections){ _format(sections) {
if(!sections.length){throw '[LT02] List without sections';} if(!sections.length) {
if(sections.length > 1 && sections.filter(s => typeof s.title == 'undefined').length > 1){throw '[LT05] You can\'t have more than one empty title.';} throw '[LT02] List without sections';
return sections.map( (section) =>{ }
if(!section.rows.length){throw '[LT03] Section without rows';} 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 { return {
title: section.title ? section.title : undefined, title: section.title ? section.title : undefined,
rows: section.rows.map( (row) => { rows: section.rows.map((row, rowIndex) => {
if(!row.title){throw '[LT04] Row without title';} if (!row.title) {
throw `[LT04] Row without title at section index ${index} and row index ${rowIndex}`;
}
return { return {
rowId: row.id ? row.id : Util.generateHash(6), rowId: row.id ? row.id : Util.generateHash(8),
title: row.title, title: row.title,
description: row.description ? row.description : '' description: row.description ? row.description : ''
}; };