From c3b273843afa0141e78e662fe1534f83287bc6e6 Mon Sep 17 00:00:00 2001 From: yond1994 Date: Thu, 4 Jan 2024 20:32:20 +0100 Subject: [PATCH] fix: "provider twilio media & document " --- packages/provider/package.json | 2 +- packages/provider/src/meta/server.js | 2 +- packages/provider/src/twilio/server.js | 35 ++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/packages/provider/package.json b/packages/provider/package.json index 988374f99..336a73926 100644 --- a/packages/provider/package.json +++ b/packages/provider/package.json @@ -1,6 +1,6 @@ { "name": "@bot-whatsapp/provider", - "version": "0.0.142-alpha.0", + "version": "0.0.161-alpha.0", "description": "Esto es el conector a Twilio, Meta, etc...", "main": "./lib/mock/index.cjs", "keywords": [], diff --git a/packages/provider/src/meta/server.js b/packages/provider/src/meta/server.js index 2502da3cf..6e03dfff7 100644 --- a/packages/provider/src/meta/server.js +++ b/packages/provider/src/meta/server.js @@ -141,7 +141,7 @@ class MetaWebHookServer extends EventEmitter { from: message.from, url: audioUrl, to, - body: generateRefprovider('_event_audio_'), + body: generateRefprovider('_event_voice_note_'), pushName, } break diff --git a/packages/provider/src/twilio/server.js b/packages/provider/src/twilio/server.js index 5906dafda..9181f47ea 100644 --- a/packages/provider/src/twilio/server.js +++ b/packages/provider/src/twilio/server.js @@ -4,6 +4,7 @@ const mime = require('mime-types') const polka = require('polka') const { urlencoded, json } = require('body-parser') const { parseNumber } = require('./utils') +const { generateRefprovider } = require('../../common/hash') /** * Encargado de levantar un servidor HTTP con una hook url @@ -26,12 +27,42 @@ class TwilioWebHookServer extends EventEmitter { */ incomingMsg = (req, res) => { const { body } = req - this.emit('message', { + const payload = { ...body, from: parseNumber(body.From), to: parseNumber(body.To), body: body.Body, - }) + } + if (body.NumMedia !== '0' && body.MediaContentType0) { + const type = body.MediaContentType0.split('/')[0] + switch (type) { + case 'audio': { + payload.body = generateRefprovider('_event_voice_note_') + break + } + case 'image': + case 'video': { + payload.body = generateRefprovider('_event_media_') + break + } + case 'application': { + payload.body = generateRefprovider('_event_document_') + break + } + case 'text': { + payload.body = generateRefprovider('_event_contacts_') + break + } + default: + // Lógica para manejar tipos de mensajes no reconocidos + break + } + } else { + if (body.Latitude && body.Longitude) { + payload.body = generateRefprovider('_event_location_') + } + } + this.emit('message', payload) const json = JSON.stringify({ body }) res.end(json) }