Created
October 8, 2018 08:04
-
-
Save DominikAngerer/998addddaf457b443e0724d89ad29aa5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var https = require('https') | |
| var querystring = require('querystring') | |
| var AWS = require('aws-sdk') | |
| var responseHeaders = { | |
| 'Access-Control-Allow-Origin': '*', | |
| 'Access-Control-Allow-Credentials': true | |
| } | |
| var storyblokApiRequest = function(inputData, success, error) { | |
| var timestamp = Math.floor(Date.now() / 1000) | |
| var postData = JSON.stringify({ | |
| story: { | |
| name: 'Contact entry - ' + timestamp, | |
| parent_id: ${StoryblokParentFolderId}, | |
| content: { | |
| component: 'form_entry', | |
| name: inputData.name, | |
| message: inputData.message | |
| } | |
| } | |
| }) | |
| var options = { | |
| hostname: 'api.storyblok.com', | |
| port: 443, | |
| path: '/v1/spaces/${StoryblokSpaceId}/stories', | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Content-Length': Buffer.byteLength(postData), | |
| 'Authorization': '${StoryblokOauthToken}' | |
| } | |
| } | |
| var req = https.request(options, function(res) { | |
| res.setEncoding('utf8') | |
| res.on('data', function(chunk) { | |
| success(null, { | |
| statusCode: '200', | |
| headers: responseHeaders, | |
| body: '{"success": true}' | |
| }) | |
| }) | |
| }) | |
| req.on('error', function(e) { | |
| error(null, { | |
| statusCode: '500', | |
| headers: responseHeaders, | |
| body: JSON.stringify({message: e.message}) | |
| }) | |
| }) | |
| req.write(postData) | |
| req.end() | |
| } | |
| var googleApiRequest = function(captchaResponse, success, error) { | |
| var postData = querystring.stringify({ | |
| 'secret': '${ReCaptchaSecret}', | |
| 'response': captchaResponse | |
| }) | |
| var options = { | |
| hostname: 'www.google.com', | |
| port: 443, | |
| path: '/recaptcha/api/siteverify', | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| 'Content-Length': Buffer.byteLength(postData) | |
| } | |
| } | |
| var req = https.request(options, function(res) { | |
| res.setEncoding('utf8') | |
| res.on('data', function(chunk) { | |
| var captchaResponse = JSON.parse(chunk) | |
| if (captchaResponse.success) { | |
| success() | |
| } else { | |
| error(null, { | |
| statusCode: '500', | |
| headers: responseHeaders, | |
| body: JSON.stringify({message: 'Invalid recaptcha'}) | |
| }) | |
| } | |
| }) | |
| }) | |
| req.on('error', function(e) { | |
| error(null, { | |
| statusCode: '500', | |
| headers: responseHeaders, | |
| body: JSON.stringify({message: e.message}) | |
| }) | |
| }) | |
| req.write(postData) | |
| req.end() | |
| } | |
| var sesApiRequest = function(inputData, success, error) { | |
| AWS.config.update({region: 'us-east-1'}) | |
| var ses = new AWS.SES() | |
| ses.sendEmail({ | |
| Destination: { | |
| ToAddresses: ['${ToEmailAddress}'] | |
| }, | |
| Message: { | |
| Body: { | |
| Text: { | |
| Data: JSON.stringify(inputData, null, 3), | |
| Charset: 'UTF-8' | |
| } | |
| }, | |
| Subject: { | |
| Data: 'Contact Form inquiry', | |
| Charset: 'UTF-8' | |
| } | |
| }, | |
| Source: '${ToEmailAddress}' | |
| }, function (err, response) { | |
| if (err !== null) { | |
| return error(err) | |
| } | |
| success(null, { | |
| statusCode: '200', | |
| headers: responseHeaders, | |
| body: JSON.stringify(response) | |
| }) | |
| }) | |
| } | |
| exports.handler = function (event, context, callback) { | |
| var inputData = JSON.parse(event.body) | |
| googleApiRequest(inputData['g-recaptcha-response'], function() { | |
| delete inputData['g-recaptcha-response'] | |
| sesApiRequest(inputData, function() { | |
| storyblokApiRequest(inputData, callback, callback) | |
| }, callback) | |
| }, callback) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment