mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-17 19:26:23 +00:00
add voice note
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,4 +7,5 @@ media/*
|
||||
!media/.gitkeep
|
||||
mediaSend/*
|
||||
!mediaSend/.gitkeep
|
||||
!mediaSend/PTT-20220223-WA0000.opus
|
||||
.env
|
||||
42
README.md
42
README.md
@@ -27,18 +27,52 @@ El siguiente proyecto se realizó con fines educativos para el canal de [Youtube
|
||||
| QR Scan (route) | ✅ |
|
||||
| Easy deploy heroku | ✅ |
|
||||
| Buttons | ✅ |
|
||||
| Send Voice Note | ✅ |
|
||||
| Add support ubuntu/linux | ✅ |
|
||||
|
||||
### (Nuevo) Botones
|
||||
|
||||
[](https://youtu.be/5lEMCeWEJ8o)
|
||||
|
||||
## Requisitos
|
||||
- node v14 o superior
|
||||
- VSCode (Editor de codigo) [Descargar](https://code.visualstudio.com/download)
|
||||
- MySql (opcional) solo aplica si vas a usar el modo 'mysql' [sql-bot.sql migración](https://github.com/leifermendez/bot-whatsapp/blob/main/sql-bot.sql)
|
||||
- Dialogflow (opcional) solo aplica si vas a usar el modo 'dialogflow'
|
||||
|
||||
### (Nuevo) Botones
|
||||
|
||||
[](https://youtu.be/5lEMCeWEJ8o)
|
||||
|
||||
> Implementar los botones solo necesitas hacer uso del metodo __sendMessageButton__ que se encuentra dentro `./controllers/send` dejo un ejemplo de como usarlo.
|
||||
[Ver implementación](https://github.com/leifermendez/bot-whatsapp/blob/main/app.js#L123)
|
||||
|
||||
``` javascript
|
||||
const { sendMessageButton } = require('./controllers/send')
|
||||
|
||||
await sendMessageButton(
|
||||
{
|
||||
"title":"¿Que te interesa ver?",
|
||||
"message":"Recuerda todo este contenido es gratis y estaria genial que me siguas!",
|
||||
"footer":"Gracias",
|
||||
"buttons":[
|
||||
{"body":"😎 Cursos"},
|
||||
{"body":"👉 Youtube"},
|
||||
{"body":"😁 Telegram"}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
## Notas de Voz
|
||||
[](https://i.imgur.com/zq6xYDp.png)
|
||||
|
||||
> Se pueden enviar notas de voz con formato nativo para que no se vea como reenviado. En este ejemplo enviare el archivo __PTT-20220223-WA0000.opus__ que se encuentra dentro de la carpeta de __/mediaSend__
|
||||
|
||||
``` javascript
|
||||
const { sendMediaVoiceNote } = require('./controllers/send')
|
||||
|
||||
await sendMediaVoiceNote(client, from, 'PTT-20220223-WA0000.opus')
|
||||
|
||||
```
|
||||
|
||||
## Instruciones
|
||||
__Descargar o Clonar repositorio__
|
||||

|
||||
|
||||
4
app.js
4
app.js
@@ -13,7 +13,7 @@ const { generateImage, cleanNumber } = require('./controllers/handle')
|
||||
const { connectionReady, connectionLost } = require('./controllers/connection')
|
||||
const { saveMedia } = require('./controllers/save')
|
||||
const { getMessages, responseMessages, bothResponse } = require('./controllers/flows')
|
||||
const { sendMedia, sendMessage, lastTrigger, sendMessageButton, readChat } = require('./controllers/send')
|
||||
const { sendMedia, sendMessage, lastTrigger, sendMessageButton, readChat, sendMediaVoiceNote } = require('./controllers/send')
|
||||
const app = express();
|
||||
app.use(cors())
|
||||
app.use(express.json())
|
||||
@@ -54,6 +54,8 @@ const listenMessage = () => client.on('message', async msg => {
|
||||
console.log('BODY',message)
|
||||
const number = cleanNumber(from)
|
||||
await readChat(number, message)
|
||||
|
||||
await sendMediaVoiceNote(client, from, 'PTT-20220223-WA0000.opus')
|
||||
/**
|
||||
* Guardamos el archivo multimedia que envia
|
||||
*/
|
||||
|
||||
@@ -4,8 +4,8 @@ const moment = require('moment');
|
||||
const fs = require('fs');
|
||||
const { MessageMedia, Buttons } = require('whatsapp-web.js');
|
||||
const { cleanNumber } = require('./handle')
|
||||
const { saveMedia } = require('../controllers/save')
|
||||
const DELAY_TIME = 170; //ms
|
||||
const DIR_MEDIA = `${__dirname}/../mediaSend`;
|
||||
|
||||
/**
|
||||
* Enviamos archivos multimedia a nuestro cliente
|
||||
@@ -14,14 +14,29 @@ const DELAY_TIME = 170; //ms
|
||||
*/
|
||||
|
||||
const sendMedia = (client, number, fileName) => {
|
||||
const dirMedia = `${__dirname}/../mediaSend/${fileName}`;
|
||||
number = cleanNumber(number)
|
||||
if (fs.existsSync(dirMedia)) {
|
||||
const media = MessageMedia.fromFilePath(dirMedia);
|
||||
const file = `${DIR_MEDIA}/${fileName}`;
|
||||
if (fs.existsSync(file)) {
|
||||
const media = MessageMedia.fromFilePath(file);
|
||||
client.sendMessage(number, media);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviamos archivos como notas de voz
|
||||
* @param {*} number
|
||||
* @param {*} fileName
|
||||
*/
|
||||
|
||||
const sendMediaVoiceNote = (client, number, fileName) => {
|
||||
number = cleanNumber(number)
|
||||
const file = `${DIR_MEDIA}/${fileName}`;
|
||||
if (fs.existsSync(file)) {
|
||||
const media = MessageMedia.fromFilePath(file);
|
||||
client.sendMessage(number, media ,{ sendAudioAsVoice: true });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviamos un mensaje simple (texto) a nuestro cliente
|
||||
* @param {*} number
|
||||
@@ -123,4 +138,4 @@ const readChat = async (number, message, trigger = null) => {
|
||||
}, 150)
|
||||
}
|
||||
|
||||
module.exports = { sendMessage, sendMedia, lastTrigger, sendMessageButton, readChat }
|
||||
module.exports = { sendMessage, sendMedia, lastTrigger, sendMessageButton, readChat, sendMediaVoiceNote }
|
||||
BIN
mediaSend/PTT-20220223-WA0000.opus
Normal file
BIN
mediaSend/PTT-20220223-WA0000.opus
Normal file
Binary file not shown.
Reference in New Issue
Block a user