-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformpost.js
More file actions
68 lines (48 loc) · 1.76 KB
/
formpost.js
File metadata and controls
68 lines (48 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as http from 'http';
import { WebflowClient } from "webflow-api";
const WEBHOOK_TOKEN = process.env.WEBHOOK_TOKEN;
const webflowClient = new WebflowClient();
async function handler(req, res) {
if(req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', async () => {
const isValidRequest = await webflowClient.webhooks.verifySignature({
headers: req.headers,
body: body,
secret: WEBHOOK_TOKEN,
});
if(!isValidRequest) {
res.writeHead(500, 'Invalid signature');
res.end();
return;
}
body = JSON.parse(body);
// Uncomment to see the whole body
//console.log('BODY:\n', JSON.stringify(body, null, '\t'));
const formDetails = {
fromName: body.payload.data.Name,
fromEmail: body.payload.data.Email,
comments: body.payload.data.Comments
}
/*
Imaginary routing system:
based on comment text, go to one of three email addresses
*/
let contactEmail = 'generic@webflow.com';
if(formDetails.comments.toLowerCase().indexOf('cat') >= 0) {
contactEmail = 'cat_support@webflow.com';
} else if(formDetails.comments.toLowerCase().indexOf('dog') >= 0) {
contactEmail = 'dog_support@webflow.com';
}
console.log(`Info on this form submission will be sent to: ${contactEmail}`);
res.writeHead(204);
res.end();
});
}
}
const server = http.createServer(handler);
server.listen(3000);
console.log('Listening on port 3000');