diff --git a/package.json b/package.json index d811f1237..9adb8b1e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bot-whatsapp/root", - "version": "0.1.26", + "version": "0.1.27", "description": "Bot de wahtsapp open source para MVP o pequeños negocios", "main": "app.js", "private": true, diff --git a/packages/docs/src/routes/docs/providers/index.mdx b/packages/docs/src/routes/docs/providers/index.mdx index f2ede101f..5d66e8b6c 100644 --- a/packages/docs/src/routes/docs/providers/index.mdx +++ b/packages/docs/src/routes/docs/providers/index.mdx @@ -22,6 +22,8 @@ Los proveedores disponibles hasta el momento son los siguientes: [whatsapp-web.js](https://github.com/pedroslopez/whatsapp-web.js) `require('@bot-whatsapp/provider/web-whatsapp')` +[WPPConnect](https://github.com/wppconnect-team/wppconnect) `require('@bot-whatsapp/provider/wppconnect')` + [Venom](https://github.com/orkestral/venom) `require('@bot-whatsapp/provider/venom')` [Baileys](https://github.com/whiskeysockets/Baileys) `require('@bot-whatsapp/provider/baileys')` diff --git a/packages/portal/src/routes/index.tsx b/packages/portal/src/routes/index.tsx index 2a2630770..bd92bd5d5 100644 --- a/packages/portal/src/routes/index.tsx +++ b/packages/portal/src/routes/index.tsx @@ -27,7 +27,7 @@ export default component$(() => { Ver videos diff --git a/packages/provider/package.json b/packages/provider/package.json index 011fb39b9..173fa22c5 100644 --- a/packages/provider/package.json +++ b/packages/provider/package.json @@ -12,12 +12,13 @@ "dependencies": { "@bot-whatsapp/bot": "*", "@ffmpeg-installer/ffmpeg": "^1.1.0", + "axios": "^1.4.0", "combine-image": "^1.0.3", "fluent-ffmpeg": "^2.1.2", + "follow-redirects": "^1.15.2", "qr-image": "^3.2.0", "rimraf": "^3.0.2", - "sharp": "^0.30.5", - "follow-redirects": "^1.15.2" + "sharp": "^0.30.5" }, "exports": { "./mock": "./lib/mock/index.cjs", diff --git a/packages/provider/src/baileys/package.json b/packages/provider/src/baileys/package.json index dae158b62..918a8e97d 100644 --- a/packages/provider/src/baileys/package.json +++ b/packages/provider/src/baileys/package.json @@ -1,6 +1,6 @@ { "dependencies": { - "@adiwajshing/baileys": "github:WhiskeySockets/Baileys", + "@adiwajshing/baileys": "4.4.0", "wa-sticker-formatter": "4.3.2" } } diff --git a/packages/provider/src/meta/index.js b/packages/provider/src/meta/index.js index feae4affd..653b2b12b 100644 --- a/packages/provider/src/meta/index.js +++ b/packages/provider/src/meta/index.js @@ -25,7 +25,7 @@ class MetaProvider extends ProviderClass { this.jwtToken = jwtToken this.numberId = numberId this.version = version - this.metHook = new MetaWebHookServer(verifyToken, port) + this.metHook = new MetaWebHookServer(jwtToken, numberId, version, verifyToken, port) this.metHook.start() const listEvents = this.busEvents() diff --git a/packages/provider/src/meta/server.js b/packages/provider/src/meta/server.js index 482e6c5d6..88bd02f6c 100644 --- a/packages/provider/src/meta/server.js +++ b/packages/provider/src/meta/server.js @@ -2,13 +2,18 @@ const { EventEmitter } = require('node:events') const polka = require('polka') const { urlencoded, json } = require('body-parser') const { generateRefprovider } = require('../../common/hash') +const { getMediaUrl } = require('./utils') class MetaWebHookServer extends EventEmitter { - constructor(token, metaPort = 3000) { + constructor(jwtToken, numberId, version, token, metaPort = 3000) { super() this.metaServer = polka() this.metaPort = metaPort this.token = token + + this.jwtToken = jwtToken + this.numberId = numberId + this.version = version this.buildHTTPServer() } @@ -18,7 +23,7 @@ class MetaWebHookServer extends EventEmitter { * @param {*} req * @param {*} res */ - incomingMsg = (req, res) => { + incomingMsg = async (req, res) => { const { body } = req const messages = body?.entry?.[0]?.changes?.[0]?.value?.messages @@ -32,53 +37,64 @@ class MetaWebHookServer extends EventEmitter { const to = body.entry[0].changes[0].value?.metadata?.display_phone_number if (message.type === 'text') { - // Si es un mensaje de texto, extrae el cuerpo del mensaje const body = message.text?.body - // Luego, crea un objeto con los datos que deseas enviar al cuerpo de la respuesta const responseObj = { type: message.type, from: message.from, to, body, } - // Finalmente, envía el objeto como respuesta utilizando el evento 'message' this.emit('message', responseObj) - } else if (message.type === 'image') { - const body = generateRefprovider('_event_image_') + } + if (message.type === 'image') { + const body = generateRefprovider('_event_image_') + const idUrl = message.image?.id + const resolvedUrl = await getMediaUrl(this.version, idUrl, this.numberId, this.jwtToken) const responseObj = { type: message.type, from: message.from, + url: resolvedUrl, to, - id: message.image.id, body, } this.emit('message', responseObj) - } else if (message.type === 'document') { - const body = generateRefprovider('_event_document_') + } + if (message.type === 'document') { + const body = generateRefprovider('_event_document_') + const idUrl = message.document?.id + const resolvedUrl = await getMediaUrl(this.version, idUrl, this.numberId, this.jwtToken) const responseObj = { type: message.type, from: message.from, + url: resolvedUrl, // Utilizar el valor resuelto de la promesa to, - id: message.document.id, body, } + this.emit('message', responseObj) - } else if (message.type === 'video') { + } + + if (message.type === 'video') { const body = generateRefprovider('_event_video_') + const idUrl = message.video?.id + + const resolvedUrl = await getMediaUrl(this.version, idUrl, this.numberId, this.jwtToken) const responseObj = { type: message.type, from: message.from, + url: resolvedUrl, // Utilizar el valor resuelto de la promesa to, - id: message.video.id, body, } this.emit('message', responseObj) - } else if (message.type === 'location') { + } + + if (message.type === 'location') { const body = generateRefprovider('_event_location_') const responseObj = { @@ -91,19 +107,24 @@ class MetaWebHookServer extends EventEmitter { } this.emit('message', responseObj) - } else if (message.type === 'audio') { - const body = generateRefprovider('_event_audio_') + } + if (message.type === 'audio') { + const body = generateRefprovider('_event_audio_') + const idUrl = message.audio?.id + const resolvedUrl = await getMediaUrl(this.version, idUrl, this.numberId, this.jwtToken) const responseObj = { type: message.type, from: message.from, + url: resolvedUrl, // Utilizar el valor resuelto de la promesa to, - id: message.audio.id, body, } this.emit('message', responseObj) - } else if (message.type === 'sticker') { + } + + if (message.type === 'sticker') { const body = generateRefprovider('_event_sticker_') const responseObj = { @@ -115,7 +136,9 @@ class MetaWebHookServer extends EventEmitter { } this.emit('message', responseObj) - } else if (message.type === 'contacts') { + } + + if (message.type === 'contacts') { const body = generateRefprovider('_event_contacts_') const responseObj = { @@ -171,7 +194,7 @@ class MetaWebHookServer extends EventEmitter { res.end('Invalid token!') } - emptyCtrl = (req, res) => { + emptyCtrl = (_, res) => { res.end('') } @@ -192,7 +215,7 @@ class MetaWebHookServer extends EventEmitter { */ start() { this.metaServer.listen(this.metaPort, () => { - console.log(`[meta]: Agregar esta url "WHEN A MESSAGE COMES IN"`) + console.log(`[meta]: Agregar esta url "Webhook"`) console.log(`[meta]: POST http://localhost:${this.metaPort}/webhook`) console.log(`[meta]: Más información en la documentación`) }) diff --git a/packages/provider/src/meta/utils.js b/packages/provider/src/meta/utils.js new file mode 100644 index 000000000..d428fdcfb --- /dev/null +++ b/packages/provider/src/meta/utils.js @@ -0,0 +1,22 @@ +const axios = require('axios') + +async function getMediaUrl(version, IdMedia, numberId, Token) { + try { + const response = await axios.get( + `https://graph.facebook.com/${version}/${IdMedia}?phone_number_id=${numberId}`, + { + headers: { + Authorization: `Bearer ${Token}`, + }, + maxBodyLength: Infinity, + } + ) + return response.data?.url + } catch (error) { + console.log(error) + } +} + +module.exports = { + getMediaUrl, +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c2d71919b..640dbc96e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -330,6 +330,9 @@ importers: '@ffmpeg-installer/ffmpeg': specifier: ^1.1.0 version: 1.1.0 + axios: + specifier: ^1.4.0 + version: 1.4.0 combine-image: specifier: ^1.0.3 version: 1.0.3 @@ -5845,6 +5848,19 @@ packages: } dev: false + /axios@1.4.0: + resolution: + { + integrity: sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==, + } + dependencies: + follow-redirects: 1.15.2(debug@4.3.4) + form-data: 4.0.0 + proxy-from-env: 1.1.0 + transitivePeerDependencies: + - debug + dev: false + /babel-extract-comments@1.0.0: resolution: { @@ -10895,6 +10911,18 @@ packages: mime-types: 2.1.35 dev: true + /form-data@4.0.0: + resolution: + { + integrity: sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==, + } + engines: { node: '>= 6' } + dependencies: + asynckit: 0.4.0 + combined-stream: 1.0.8 + mime-types: 2.1.35 + dev: false + /formdata-polyfill@4.0.10: resolution: { @@ -17899,7 +17927,6 @@ packages: { integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==, } - dev: true /ps-list@8.1.1: resolution: diff --git a/starters/apps/base-baileys-json/package.json b/starters/apps/base-baileys-json/package.json index a68a337b7..4c9047206 100644 --- a/starters/apps/base-baileys-json/package.json +++ b/starters/apps/base-baileys-json/package.json @@ -14,7 +14,7 @@ "@bot-whatsapp/database": "latest", "@bot-whatsapp/provider": "latest", "@bot-whatsapp/portal": "latest", - "@adiwajshing/baileys": "github:WhiskeySockets/Baileys", + "@adiwajshing/baileys": "4.4.0", "mime-types": "2.1.35", "wa-sticker-formatter": "4.3.2" }, diff --git a/starters/apps/base-baileys-memory/package.json b/starters/apps/base-baileys-memory/package.json index 484a809dc..f300b755a 100644 --- a/starters/apps/base-baileys-memory/package.json +++ b/starters/apps/base-baileys-memory/package.json @@ -14,7 +14,7 @@ "@bot-whatsapp/database": "latest", "@bot-whatsapp/provider": "latest", "@bot-whatsapp/portal": "latest", - "@adiwajshing/baileys": "github:WhiskeySockets/Baileys", + "@adiwajshing/baileys": "4.4.0", "mime-types": "2.1.35", "wa-sticker-formatter": "4.3.2" }, diff --git a/starters/apps/base-baileys-mongo/package.json b/starters/apps/base-baileys-mongo/package.json index 0ba422121..03f337fa6 100644 --- a/starters/apps/base-baileys-mongo/package.json +++ b/starters/apps/base-baileys-mongo/package.json @@ -14,7 +14,7 @@ "@bot-whatsapp/database": "latest", "@bot-whatsapp/provider": "latest", "@bot-whatsapp/portal": "latest", - "@adiwajshing/baileys": "github:WhiskeySockets/Baileys", + "@adiwajshing/baileys": "4.4.0", "mime-types": "2.1.35", "mongodb": "^4.12.1", "wa-sticker-formatter": "4.3.2" diff --git a/starters/apps/base-baileys-mysql/package.json b/starters/apps/base-baileys-mysql/package.json index f175643fc..ae46a0fee 100644 --- a/starters/apps/base-baileys-mysql/package.json +++ b/starters/apps/base-baileys-mysql/package.json @@ -14,7 +14,7 @@ "@bot-whatsapp/database": "latest", "@bot-whatsapp/provider": "latest", "@bot-whatsapp/portal": "latest", - "@adiwajshing/baileys": "github:WhiskeySockets/Baileys", + "@adiwajshing/baileys": "4.4.0", "mime-types": "2.1.35", "mysql2": "^2.3.3", "wa-sticker-formatter": "4.3.2"