Skip to content

Commit eb06781

Browse files
committed
create a netlify function for a slack invite
1 parent c11fcaf commit eb06781

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

netlify/functions/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "twc-berlin-functions",
3+
"dependencies": {
4+
"@netlify/functions": "^1.4.0",
5+
"axios": "^1.2.2",
6+
"dotenv": "^16.0.3",
7+
"envalid": "^7.3.1"
8+
}
9+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import dotenv from 'dotenv';
2+
import axios from 'axios';
3+
import { cleanEnv, str, url } from 'envalid';
4+
5+
dotenv.config();
6+
7+
cleanEnv(process.env, {
8+
SLACK_TOKEN: str({ desc: 'Slack authentication token' }),
9+
});
10+
11+
/**
12+
* @type import('@netlify/functions').Handler
13+
*/
14+
export async function handler(event, context, callback) {
15+
try {
16+
const payload = JSON.parse(event.body);
17+
console.log(payload)
18+
const email = encodeURIComponent(payload.payload.email.trim());
19+
const SLACK_TOKEN = process.env.SLACK_TOKEN;
20+
const SLACK_INVITE_ENDPOINT = 'https://slack.com/api/users.admin.invite';
21+
const toSlack = `email=${email}&token=${SLACK_TOKEN}&set_active=true`;
22+
await axios
23+
.get(`${SLACK_INVITE_ENDPOINT}?${toSlack}`)
24+
.then((response) => {
25+
if (response.status >= 200 && response.status < 400) {
26+
callback(null, {
27+
statusCode: 200,
28+
body: JSON.stringify({
29+
message: 'success',
30+
}),
31+
});
32+
}
33+
else {
34+
callback('Invalid response')
35+
}
36+
})
37+
.catch((error) => {
38+
console.log(error);
39+
});
40+
}
41+
catch (err) {
42+
console.log(err)
43+
callback('another error')
44+
}
45+
}

netlify/functions/yarn.lock

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@netlify/functions@^1.4.0":
6+
version "1.4.0"
7+
resolved "https://registry.yarnpkg.com/@netlify/functions/-/functions-1.4.0.tgz#027a2e5d54df5519ccbd14cf450231e97bbbf93a"
8+
integrity sha512-gy7ULTIRroc2/jyFVGx1djCmmBMVisIwrvkqggq5B6iDcInRSy2Tpkm+V5C63hKJVkNRskKWtLQKm9ecCaQTjA==
9+
dependencies:
10+
is-promise "^4.0.0"
11+
12+
asynckit@^0.4.0:
13+
version "0.4.0"
14+
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
15+
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==
16+
17+
axios@^1.2.2:
18+
version "1.2.2"
19+
resolved "https://registry.yarnpkg.com/axios/-/axios-1.2.2.tgz#72681724c6e6a43a9fea860fc558127dbe32f9f1"
20+
integrity sha512-bz/J4gS2S3I7mpN/YZfGFTqhXTYzRho8Ay38w2otuuDR322KzFIWm/4W2K6gIwvWaws5n+mnb7D1lN9uD+QH6Q==
21+
dependencies:
22+
follow-redirects "^1.15.0"
23+
form-data "^4.0.0"
24+
proxy-from-env "^1.1.0"
25+
26+
combined-stream@^1.0.8:
27+
version "1.0.8"
28+
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
29+
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
30+
dependencies:
31+
delayed-stream "~1.0.0"
32+
33+
delayed-stream@~1.0.0:
34+
version "1.0.0"
35+
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
36+
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==
37+
38+
dotenv@^16.0.3:
39+
version "16.0.3"
40+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.3.tgz#115aec42bac5053db3c456db30cc243a5a836a07"
41+
integrity sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==
42+
43+
envalid@^7.3.1:
44+
version "7.3.1"
45+
resolved "https://registry.yarnpkg.com/envalid/-/envalid-7.3.1.tgz#5bf6bbb4effab2d64a1991d8078b4ae38924f0d2"
46+
integrity sha512-KL1YRwn8WcoF/Ty7t+yLLtZol01xr9ZJMTjzoGRM8NaSU+nQQjSWOQKKJhJP2P57bpdakJ9jbxqQX4fGTOicZg==
47+
dependencies:
48+
tslib "2.3.1"
49+
50+
follow-redirects@^1.15.0:
51+
version "1.15.2"
52+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
53+
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
54+
55+
form-data@^4.0.0:
56+
version "4.0.0"
57+
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
58+
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
59+
dependencies:
60+
asynckit "^0.4.0"
61+
combined-stream "^1.0.8"
62+
mime-types "^2.1.12"
63+
64+
is-promise@^4.0.0:
65+
version "4.0.0"
66+
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-4.0.0.tgz#42ff9f84206c1991d26debf520dd5c01042dd2f3"
67+
integrity sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==
68+
69+
70+
version "1.52.0"
71+
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
72+
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==
73+
74+
mime-types@^2.1.12:
75+
version "2.1.35"
76+
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
77+
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
78+
dependencies:
79+
mime-db "1.52.0"
80+
81+
proxy-from-env@^1.1.0:
82+
version "1.1.0"
83+
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
84+
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
85+
86+
87+
version "2.3.1"
88+
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
89+
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==

0 commit comments

Comments
 (0)