mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 20:49:15 +00:00
feat(provider): ⚡ bailey add send file video audio
This commit is contained in:
65
packages/provider/common/download.js
Normal file
65
packages/provider/common/download.js
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
const mimeDep = require('mime-types')
|
||||||
|
const { tmpdir } = require('os')
|
||||||
|
const http = require('http')
|
||||||
|
const https = require('https')
|
||||||
|
const { rename, createWriteStream } = require('fs')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extrar el mimetype from buffer
|
||||||
|
* @param {string} response
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const fileTypeFromFile = async (response) => {
|
||||||
|
const type = response.headers['content-type'] ?? null
|
||||||
|
const ext = mimeDep.extension(type)
|
||||||
|
return {
|
||||||
|
type,
|
||||||
|
ext,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Descargar archivo binay en tmp
|
||||||
|
* @param {*} url
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const generalDownload = async (url) => {
|
||||||
|
const handleDownload = () => {
|
||||||
|
const checkProtocol = url.includes('https:')
|
||||||
|
const handleHttp = checkProtocol ? https : http
|
||||||
|
const name = `tmp-${Date.now()}-dat`
|
||||||
|
const fullPath = `${tmpdir()}/${name}`
|
||||||
|
const file = createWriteStream(fullPath)
|
||||||
|
|
||||||
|
return new Promise((res, rej) => {
|
||||||
|
handleHttp.get(url, function (response) {
|
||||||
|
response.pipe(file)
|
||||||
|
file.on('finish', async function () {
|
||||||
|
file.close()
|
||||||
|
res({ response, fullPath })
|
||||||
|
})
|
||||||
|
file.on('error', function () {
|
||||||
|
file.close()
|
||||||
|
rej(null)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleFile = (pathInput, ext) =>
|
||||||
|
new Promise((resolve, reject) => {
|
||||||
|
const fullPath = `${pathInput}.${ext}`
|
||||||
|
rename(pathInput, fullPath, (err) => {
|
||||||
|
if (err) reject(null)
|
||||||
|
resolve(fullPath)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
const httpResponse = await handleDownload()
|
||||||
|
const { ext } = await fileTypeFromFile(httpResponse.response)
|
||||||
|
const getPath = await handleFile(httpResponse.fullPath, ext)
|
||||||
|
|
||||||
|
return getPath
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { generalDownload }
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
const mimeDep = require('mime-types')
|
|
||||||
/**
|
|
||||||
* Extrar el mimetype from buffer
|
|
||||||
* @param {string} response
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
const fileTypeFromFile = async (response) => {
|
|
||||||
const type = response.headers['content-type'] ?? null
|
|
||||||
const ext = mimeDep.extension(type)
|
|
||||||
return {
|
|
||||||
type,
|
|
||||||
ext,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { fileTypeFromFile }
|
|
||||||
@@ -4,7 +4,7 @@ const pino = require('pino')
|
|||||||
const rimraf = require('rimraf')
|
const rimraf = require('rimraf')
|
||||||
const mime = require('mime-types')
|
const mime = require('mime-types')
|
||||||
const { join } = require('path')
|
const { join } = require('path')
|
||||||
const { existsSync, createWriteStream, readFileSync } = require('fs')
|
const { createWriteStream, readFileSync } = require('fs')
|
||||||
const { Console } = require('console')
|
const { Console } = require('console')
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -18,9 +18,10 @@ const {
|
|||||||
baileyGenerateImage,
|
baileyGenerateImage,
|
||||||
baileyCleanNumber,
|
baileyCleanNumber,
|
||||||
baileyIsValidNumber,
|
baileyIsValidNumber,
|
||||||
baileyDownloadMedia,
|
|
||||||
} = require('./utils')
|
} = require('./utils')
|
||||||
|
|
||||||
|
const { generalDownload } = require('../../common/download')
|
||||||
|
|
||||||
const logger = new Console({
|
const logger = new Console({
|
||||||
stdout: createWriteStream(`${process.cwd()}/baileys.log`),
|
stdout: createWriteStream(`${process.cwd()}/baileys.log`),
|
||||||
})
|
})
|
||||||
@@ -170,7 +171,7 @@ class BaileysProvider extends ProviderClass {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
sendMedia = async (number, imageUrl, text) => {
|
sendMedia = async (number, imageUrl, text) => {
|
||||||
const fileDownloaded = await baileyDownloadMedia(imageUrl)
|
const fileDownloaded = await generalDownload(imageUrl)
|
||||||
const mimeType = mime.lookup(fileDownloaded)
|
const mimeType = mime.lookup(fileDownloaded)
|
||||||
|
|
||||||
if (mimeType.includes('image'))
|
if (mimeType.includes('image'))
|
||||||
@@ -180,7 +181,7 @@ class BaileysProvider extends ProviderClass {
|
|||||||
if (mimeType.includes('audio'))
|
if (mimeType.includes('audio'))
|
||||||
return this.sendAudio(number, fileDownloaded, text)
|
return this.sendAudio(number, fileDownloaded, text)
|
||||||
|
|
||||||
return this.sendFile()
|
return this.sendFile(number, fileDownloaded)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
const { createWriteStream, rename } = require('fs')
|
const { createWriteStream } = require('fs')
|
||||||
const combineImage = require('combine-image')
|
const combineImage = require('combine-image')
|
||||||
const qr = require('qr-image')
|
const qr = require('qr-image')
|
||||||
const { tmpdir } = require('os')
|
|
||||||
const http = require('http')
|
|
||||||
const https = require('https')
|
|
||||||
|
|
||||||
const { fileTypeFromFile } = require('../../common/fileType')
|
|
||||||
|
|
||||||
const baileyCleanNumber = (number, full = false) => {
|
const baileyCleanNumber = (number, full = false) => {
|
||||||
number = number.replace('@s.whatsapp.net', '')
|
number = number.replace('@s.whatsapp.net', '')
|
||||||
@@ -43,54 +38,8 @@ const baileyIsValidNumber = (rawNumber) => {
|
|||||||
return !exist
|
return !exist
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Incompleta
|
|
||||||
* Descargar archivo multimedia para enviar
|
|
||||||
* @param {*} url
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
const baileyDownloadMedia = async (url) => {
|
|
||||||
const handleDownload = () => {
|
|
||||||
const checkProtocol = url.includes('https:')
|
|
||||||
const handleHttp = checkProtocol ? https : http
|
|
||||||
const name = `tmp-${Date.now()}-dat`
|
|
||||||
const fullPath = `${tmpdir()}/${name}`
|
|
||||||
const file = createWriteStream(fullPath)
|
|
||||||
|
|
||||||
return new Promise((res, rej) => {
|
|
||||||
handleHttp.get(url, function (response) {
|
|
||||||
response.pipe(file)
|
|
||||||
file.on('finish', async function () {
|
|
||||||
file.close()
|
|
||||||
res({ response, fullPath })
|
|
||||||
})
|
|
||||||
file.on('error', function () {
|
|
||||||
file.close()
|
|
||||||
rej(null)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleFile = (pathInput, ext) =>
|
|
||||||
new Promise((resolve, reject) => {
|
|
||||||
const fullPath = `${pathInput}.${ext}`
|
|
||||||
rename(pathInput, fullPath, (err) => {
|
|
||||||
if (err) reject(null)
|
|
||||||
resolve(fullPath)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
const httpResponse = await handleDownload()
|
|
||||||
const { ext } = await fileTypeFromFile(httpResponse.response)
|
|
||||||
const getPath = await handleFile(httpResponse.fullPath, ext)
|
|
||||||
|
|
||||||
return getPath
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
baileyCleanNumber,
|
baileyCleanNumber,
|
||||||
baileyGenerateImage,
|
baileyGenerateImage,
|
||||||
baileyIsValidNumber,
|
baileyIsValidNumber,
|
||||||
baileyDownloadMedia,
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user