Last active
April 24, 2018 10:02
-
-
Save trinhvanhuy/6f48612c49f1110b6bd267abac6d97a8 to your computer and use it in GitHub Desktop.
test carmoov
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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