This repository has been archived by the owner on Apr 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
136 lines (129 loc) · 2.84 KB
/
app.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var express = require("express");
var app = express();
require("dotenv").config();
var cors = require("cors");
var axios = require("axios");
var validator = require("validator");
var bodyParser = require("body-parser");
app.use(cors());
app.use(bodyParser.json());
app.get("/", (req, res) => {
res.json("Hello world!");
});
app.post("/subscribe", (req, res) => {
//send request to mailchimp api
if (
req.body != undefined &&
req.body.email != undefined &&
req.body.email.length > 5 &&
validator.isEmail(req.body.email)
) {
axios
.post(
process.env.MAILCHIMP_SUBSCRIBE_URL,
{
email_address: req.body.email,
status: "subscribed"
},
{
auth: {
username: "node",
password: process.env.MAILCHIMP_API_KEY
}
}
)
.then(response => {
res.json({
success: true
});
})
.catch(error => {
console.log(error);
res
.status(500)
.json({
success: false,
error: {
code: 500,
message: "Error while requesting the mailchimp api"
}
})
.end();
});
} else {
res
.status(400)
.json({
success: false,
error: {
code: 400,
message: "The email is invalid or not provided"
}
})
.end();
}
});
//for mailchimp validator
app.get("/event/subscribe", (req, res) => {
res.json(true);
});
//for actual wh
app.post("/event/subscribe", (req, res) => {
if (
req.body != undefined &&
req.body.type == "subscribe"
) {
//send request to discord api via vh
axios
.post(
process.env.DISCORD_WH_URL,
{
embeds: [
{
title: "New subscriber!",
fields: [
{
name: 'Email',
value: req.body['data[email]']
},
{
name: 'Ip',
value: req.body['data[ip_opt]']
}
]
}
]
}
)
.then(response => {
res.json({
success: true
});
})
.catch(error => {
console.log(error);
res
.status(500)
.json({
success: false,
error: {
code: 500,
message: "Error while requesting the discord api"
}
})
.end();
});
} else {
res
.status(400)
.json({
success: false,
error: {
code: 400
}
})
.end();
}
});
console.log("The newsletter api server listen on " + process.env.LISTEN_HOST + ":" + process.env.LISTEN_PORT);
app.listen(process.env.LISTEN_PORT, process.env.LISTEN_HOST);