Skip to content

Instantly share code, notes, and snippets.

@trinhvanhuy
Last active April 24, 2018 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trinhvanhuy/6f48612c49f1110b6bd267abac6d97a8 to your computer and use it in GitHub Desktop.
Save trinhvanhuy/6f48612c49f1110b6bd267abac6d97a8 to your computer and use it in GitHub Desktop.
test carmoov
'use latest';
const request = require('request');
module.exports = function(context, cb, res) {
const nodemailer = require('nodemailer');
const smtpConfig = {
host: 'SSL0.OVH.NET',
port: 465,
secure: true,
auth: {
user: 'test@ant-tech.eu',
pass: 'Test123456@'
}
};
const transporter = nodemailer.createTransport(smtpConfig);
const mailOptions = {
from: 'test@ant-tech.eu',
to: context.query.receiver,
subject: 'Random Gif',
html: ''
};
if (!mailOptions.to || mailOptions.to === '') {
res.writeHead(500, { 'Content-Type': 'text/html' });
return res.end('The receiver is required !');
}
const limit = 100;
const giphyToken = 'WW71QxTe9gqdLdyWF0dFlLvOdioDKANP';
let gifQuery = context.query.gifQuery;
if (!gifQuery || gifQuery === '') {
gifQuery = 'random';
}
request.get(
`http://api.giphy.com/v1/gifs/search?q=${gifQuery}&limit=${limit}&api_key=${giphyToken}`,
(error, response, body) => {
let giphyImg = '';
if (error) {
res.writeHead(500, { 'Content-Type': 'text/html' });
cb(error, null);
return res.end(JSON.stringify(error));
}
try {
const arrayImages = JSON.parse(body).data;
const randomInt = Math.floor((Math.random() * arrayImages.length) + 0);
giphyImg = arrayImages[randomInt].images.original.url;
mailOptions.html = `<img src='${giphyImg}' />`;
transporter.sendMail(mailOptions, (err, info) => {
transporter.close();
if (err) {
res.writeHead(500, { 'Content-Type': 'text/html' });
return res.end(JSON.stringify(err));
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
return res.end(JSON.stringify(mailOptions));
}
});
} catch (err) {
res.writeHead(500, { 'Content-Type': 'text/html' });
return res.end(JSON.stringify(err));
}
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment