-
-
Notifications
You must be signed in to change notification settings - Fork 854
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #886 from C4rlos-Mor4n/main
fix(bot): 🔥 se incorpora la funcionalidad de blacklist dinámica
- Loading branch information
Showing
6 changed files
with
258 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
class DynamicBlacklist { | ||
#blacklist = new Set() | ||
|
||
/** | ||
* Constructor para inicializar la lista negra. | ||
* @param {Array<string>} initialNumbers - Lista inicial de números a bloquear. | ||
*/ | ||
constructor(initialNumbers = []) { | ||
this.addToBlacklist(initialNumbers) | ||
} | ||
|
||
/** | ||
* Excepción lanzada cuando un número ya existe en la lista negra. | ||
*/ | ||
static PhoneNumberAlreadyExistsError = class extends Error { | ||
constructor(phoneNumber) { | ||
super(`El número de teléfono ${phoneNumber} ya está en la lista negra.`) | ||
this.name = 'PhoneNumberAlreadyExistsError' | ||
} | ||
} | ||
|
||
/** | ||
* Excepción lanzada cuando un número no se encuentra en la lista negra. | ||
*/ | ||
static PhoneNumberNotFoundError = class extends Error { | ||
constructor(phoneNumber) { | ||
super(`El número de teléfono ${phoneNumber} no está en la lista negra.`) | ||
this.name = 'PhoneNumberNotFoundError' | ||
} | ||
} | ||
|
||
/** | ||
* Añade uno o varios números de teléfono a la lista negra. | ||
* @param {string | Array<string>} phoneNumbers - Número o números a añadir. | ||
* @returns {Array<string>} - Devuelve una lista de mensajes indicando el resultado de añadir cada número. | ||
*/ | ||
addToBlacklist(...phoneNumbers) { | ||
const responseMessages = [] | ||
|
||
phoneNumbers.flat().forEach((number) => { | ||
if (this.#blacklist.has(number)) { | ||
responseMessages.push(`El número de teléfono ${number} ya está en la lista negra.`) | ||
} else { | ||
this.#blacklist.add(number) | ||
responseMessages.push(`Número ${number} añadido exitosamente.`) | ||
} | ||
}) | ||
|
||
return responseMessages | ||
} | ||
|
||
/** | ||
* Elimina un número de teléfono de la lista negra. | ||
* @param {string} phoneNumber - El número a eliminar. | ||
*/ | ||
removeFromBlacklist(phoneNumber) { | ||
if (!this.#blacklist.has(phoneNumber)) { | ||
throw new DynamicBlacklist.PhoneNumberNotFoundError(phoneNumber) | ||
} | ||
this.#blacklist.delete(phoneNumber) | ||
} | ||
|
||
/** | ||
* Verifica si un número de teléfono está en la lista negra. | ||
* @param {string} phoneNumber - El número a verificar. | ||
* @returns {boolean} - Verdadero si está en la lista, falso en caso contrario. | ||
*/ | ||
isInBlacklist(phoneNumber) { | ||
return this.#blacklist.has(phoneNumber) | ||
} | ||
|
||
/** | ||
* Proporciona una copia de la lista negra actual. | ||
* @returns {Array<string>} - Los números de teléfono en la lista negra. | ||
*/ | ||
getBlacklistSnapshot() { | ||
return [...this.#blacklist] | ||
} | ||
} | ||
module.exports = DynamicBlacklist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Navigation from '../../../components/widgets/Navigation' | ||
|
||
# idle | ||
|
||
Esta funcionalidad te permite dada una cantidad de tiempo de inactividad en el chat, ejecutar una acción. | ||
|
||
## Ejemplo | ||
|
||
En el siguiente ejemplo, si el usuario no responde en 2 segundos, se ejecutará la acción de cancelar el flujo. | ||
|
||
```js | ||
const { addKeyword } = require('@bot-whatsapp/bot') | ||
|
||
const flujoFinal = addKeyword(EVENTS.ACTION).addAnswer('Se canceló por inactividad') | ||
|
||
const flujoPrincipal = addKeyword(['hola']) | ||
.addAnswer( | ||
'Debes de responder antes de que transcurran 2 segundos', | ||
{ capture: true, idle: 2000 }, // idle: 2000 = 2 segundos | ||
async (ctx, { gotoFlow, inRef }) => { | ||
if (ctx?.idleFallBack) { | ||
return gotoFlow(flujoFinal) | ||
} | ||
} | ||
) | ||
.addAnswer('gracias!') | ||
|
||
``` | ||
## API | ||
### Propiedades | ||
- **idle** - Tiempo de inactividad en milisegundos | ||
- **idleFallBack** - Si se ejecutó la acción por inactividad | ||
--- | ||
<Navigation | ||
pages={[ | ||
{ name: 'Instalación', link: '/docs/install' }, | ||
{ name: 'Conceptos', link: '/docs/essential' }, | ||
]} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.