mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-19 20:19:15 +00:00
fix(bot): ⚡ flow improvement + add utils
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
const { createWriteStream } = require('fs')
|
||||
const qr = require('qr-image')
|
||||
const { tmpdir } = require('os')
|
||||
const http = require('http')
|
||||
const https = require('https')
|
||||
|
||||
const cleanNumber = (number, full = false) => {
|
||||
number = number.replace('@c.us', '')
|
||||
@@ -18,4 +21,33 @@ const isValidNumber = (rawNumber) => {
|
||||
return !exist
|
||||
}
|
||||
|
||||
module.exports = { cleanNumber, generateImage, isValidNumber }
|
||||
/**
|
||||
* Incompleta
|
||||
* Descargar archivo multimedia para enviar
|
||||
* @param {*} url
|
||||
* @returns
|
||||
*/
|
||||
const downloadMedia = (url) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const ext = url.split('.').pop()
|
||||
const checkProtocol = url.includes('https:')
|
||||
const handleHttp = checkProtocol ? https : http
|
||||
const name = `tmp-${Date.now()}.${ext}`
|
||||
const fullPath = `${tmpdir()}/${name}`
|
||||
const file = createWriteStream(fullPath)
|
||||
handleHttp.get(url, function (response) {
|
||||
response.pipe(file)
|
||||
file.on('finish', function () {
|
||||
file.close()
|
||||
resolve(fullPath)
|
||||
})
|
||||
file.on('error', function () {
|
||||
console.log('errro')
|
||||
file.close()
|
||||
reject(null)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { cleanNumber, generateImage, isValidNumber, downloadMedia }
|
||||
|
||||
Reference in New Issue
Block a user