|
const fetch = require('node-fetch'); |
|
const htmlToText = require('html-to-text'); |
|
const Busboy = require('busboy'); |
|
// const inspect = require('util').inspect; |
|
const webhookUrl = process.env.WEBHOOK_DISCORD; |
|
|
|
function create_message(fields){ |
|
message = "From: "+ fields.from + "\n" |
|
+ "To: "+ fields.to + "\n" |
|
+ "Subject: "+ fields.subject + "\n" |
|
+ "Body: "+ (fields.text || htmlToText.fromString(fields.html)) + "\n"; |
|
return message |
|
} |
|
|
|
async function send_message(fields){ |
|
const url = webhookUrl; |
|
const body = { |
|
content: create_message(fields) |
|
}; |
|
const options = { |
|
method: 'POST', |
|
body: JSON.stringify(body), |
|
headers: { 'Content-Type': 'application/json' }, |
|
}; |
|
return await fetch(url, options); |
|
} |
|
|
|
/** |
|
* HTTP Cloud Function that makes an HTTP request |
|
* |
|
* @param {Object} req Cloud Function request context. |
|
* @param {Object} res Cloud Function response context. |
|
*/ |
|
exports.makeRequest = async (req, res) => { |
|
if (req.method != 'POST') return res.sendStatus(405); // method not allowed |
|
// console.log("Mail coming"); |
|
var fields = {}; |
|
var busboy = new Busboy({ headers: req.headers }); |
|
busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) { |
|
// console.log('Field [' + fieldname + ']: value: ' + inspect(val)); |
|
fields[fieldname] = val; |
|
}); |
|
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { |
|
// console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype); |
|
file.on('data', function(data) { |
|
// console.log('File [' + fieldname + '] got ' + data.length + ' bytes'); |
|
}); |
|
file.on('end', function() { |
|
// console.log('File [' + fieldname + '] Finished'); |
|
}); |
|
}); |
|
busboy.on('finish', async () => { |
|
// console.log('Done parsing form!'); |
|
const externalRes = await send_message(fields); |
|
res.sendStatus(externalRes.ok ? 200 : 500); |
|
}); |
|
busboy.end(req.rawBody); |
|
}; |