Skip to content

Commit

Permalink
Merge pull request #754 from codigoencasa/fix/addAction-donwload
Browse files Browse the repository at this point in the history
chore: next-version
  • Loading branch information
leifermendez authored May 29, 2023
2 parents 72eddca + 6be8ac1 commit bc10b3e
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 259 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.1.27](https://github.com/leifermendez/bot-whatsapp/compare/v0.1.25...v0.1.27) (2023-05-28)


### Features

* **adapter:** :zap: added that it automatically generates the date when the data is added to the db ([889cbfa](https://github.com/leifermendez/bot-whatsapp/commit/889cbfab8abfb76ed8e111e702bdad93483ae611))
* **provider:** :sparkles: the image, video, document, stickers, location and vcard events are added ([0705aeb](https://github.com/leifermendez/bot-whatsapp/commit/0705aeb6077fd151ccf7a37582f7afe64e089b36))


### Bug Fixes

* **bot:** :zap: hot fix `fotoFlow` and download ([dced080](https://github.com/leifermendez/bot-whatsapp/commit/dced08017c823791131a29ef4a5e81090800794c))
* **provider:** :zap: version is added ([d4818f2](https://github.com/leifermendez/bot-whatsapp/commit/d4818f289d7e559fa46076c34c6ddfb1deaf427d))
* **provider:** meta improved 🔥 ([2f21dfd](https://github.com/leifermendez/bot-whatsapp/commit/2f21dfd2b9092736a190a0a1ce4a4fa3762999e0))

### [0.1.25](https://github.com/leifermendez/bot-whatsapp/compare/v0.1.23...v0.1.25) (2023-04-07)


Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bot-whatsapp/root",
"version": "0.1.25",
"version": "0.1.27",
"description": "Bot de wahtsapp open source para MVP o pequeños negocios",
"main": "app.js",
"private": true,
Expand Down Expand Up @@ -84,12 +84,12 @@
"standard-version": "^9.5.0",
"uvu": "^0.5.6"
},
"packageManager": "pnpm@7.25.1",
"packageManager": "pnpm@8.5.0",
"engines": {
"node": ">=16",
"npm": "please-use-pnpm",
"yarn": "please-use-pnpm",
"pnpm": ">=7.18.0"
"pnpm": ">=8.5.0"
},
"author": "Leifer Mendez <[email protected]>"
}
4 changes: 3 additions & 1 deletion packages/bot/core/core.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ class CoreClass {
const parseListMsg = await this.flowClass.find(flowParentId?.ref, true, flowTree)
if (endFlowFlag) return
for (const msg of parseListMsg) {
await this.sendProviderAndSave(from, msg)
const msgParse = this.flowClass.findSerializeByRef(msg?.ref)
const ctxMessage = { ...msgParse, ...msg }
await this.sendProviderAndSave(from, ctxMessage).then(() => resolveCbEveryCtx(ctxMessage))
}
await endFlow(flag)()
return
Expand Down
2 changes: 2 additions & 0 deletions packages/bot/io/flow.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class FlowClass {

findIndexByRef = (ref) => this.flowSerialize.findIndex((r) => r.ref === ref)

findSerializeByRef = (ref) => this.flowSerialize.find((r) => r.ref === ref)

getRefToContinueChild = (keyword) => {
try {
const flowChilds = this.flowSerialize
Expand Down
1 change: 0 additions & 1 deletion packages/database/src/mongo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class MongoAdapter {
}

save = async (ctx) => {

const ctxWithDate = {
...ctx,
date: new Date(),
Expand Down
2 changes: 1 addition & 1 deletion packages/portal/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default component$(() => {
<a
class={'btn-link '}
target="_blank"
href="https://www.youtube.com/watch?v=1u0TTbjK5bo&list=PL_WGMLcL4jzViIlmyDZPnhAdkc8RXGkFh"
href="https://www.youtube.com/playlist?list=PL_WGMLcL4jzWPhdhcUyhbFU6bC0oJd2BR"
>
Ver videos
</a>
Expand Down
2 changes: 1 addition & 1 deletion packages/provider/common/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const generalDownload = async (url) => {

const handleFile = (pathInput, ext) =>
new Promise((resolve, reject) => {
const fullPath = `${pathInput}.${ext}`
const fullPath = checkIsLocal ? `${pathInput}` : `${pathInput}.${ext}`
rename(pathInput, fullPath, (err) => {
if (err) reject(null)
resolve(fullPath)
Expand Down
5 changes: 3 additions & 2 deletions packages/provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/provider/src/baileys/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"@adiwajshing/baileys": "github:WhiskeySockets/Baileys",
"@adiwajshing/baileys": "4.4.0",
"wa-sticker-formatter": "4.3.2"
}
}
14 changes: 7 additions & 7 deletions packages/provider/src/meta/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -121,7 +121,7 @@ class MetaProvider extends ProviderClass {
}
return this.sendMessageMeta(body)
}

/**
* Enviar listas alternativo
* @param {*} number
Expand All @@ -135,11 +135,11 @@ class MetaProvider extends ProviderClass {
sendList = async (number, header, text, footer, button, list) => {
const parseList = list.map((list) => ({
title: list.title,
rows: list.rows.map(row => ({
rows: list.rows.map((row) => ({
id: row.id,
title: row.title,
description: row.description
}))
description: row.description,
})),
}))

const body = {
Expand All @@ -161,10 +161,10 @@ class MetaProvider extends ProviderClass {
},
action: {
button: button,
sections: parseList
sections: parseList,
},
},
};
}
return this.sendMessageMeta(body)
}
/**
Expand Down
65 changes: 44 additions & 21 deletions packages/provider/src/meta/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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

Expand All @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand Down Expand Up @@ -171,7 +194,7 @@ class MetaWebHookServer extends EventEmitter {
res.end('Invalid token!')
}

emptyCtrl = (req, res) => {
emptyCtrl = (_, res) => {
res.end('')
}

Expand All @@ -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`)
})
Expand Down
22 changes: 22 additions & 0 deletions packages/provider/src/meta/utils.js
Original file line number Diff line number Diff line change
@@ -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,
}
Loading

0 comments on commit bc10b3e

Please sign in to comment.