diff --git a/.gitignore b/.gitignore index f0c9e6639e0..505c0fd84d5 100644 --- a/.gitignore +++ b/.gitignore @@ -105,4 +105,5 @@ public # Build timestamp build-timestamp -.now \ No newline at end of file +.now +.vercel \ No newline at end of file diff --git a/api/survey.ts b/api/survey.ts index ddd3aa890e5..ddcd2a412c1 100644 --- a/api/survey.ts +++ b/api/survey.ts @@ -1,6 +1,8 @@ import { NowRequest, NowResponse } from '@now/node'; import fetch, { FetchError } from 'node-fetch'; +import { WebClient } from '@slack/web-api'; + require('dotenv').config(); const permittedOrigins = [ @@ -17,6 +19,13 @@ module.exports = async (req: NowRequest, res: NowResponse) => { return res.json({ error: 'Invalid SurveyGizmo request uri.' }); } + if (!process.env.SLACK_TOKEN || !process.env.SLACK_CHANNEL) { + return res.json({ error: 'Invalid Slack token or channel.' }); + } + + // Initialize Slack web client + const slack = new WebClient(process.env.SLACK_TOKEN); + // Only allow requests from specified urls const { origin } = req.headers; if (!origin || Array.isArray(origin) || !permittedOrigins.includes(origin)) { @@ -43,6 +52,37 @@ module.exports = async (req: NowRequest, res: NowResponse) => { }, }).catch((error: FetchError) => res.json({ error: error.message })); + // Prepare data for Slack + const emoji = + experience === 'Negative' + ? ':disappointed:' + : experience === 'Neutral' + ? ':neutral_face:' + : ':grinning:'; + + const blocks = [ + { + type: 'section', + text: { + type: 'mrkdwn', + text: `${emoji} *${experience}* web page experience ${emoji}`, + }, + }, + ]; + + if (path) { + blocks[0].text.text += `\n>*Path:*\n>${path}`; + } + + if (comment) { + blocks[0].text.text += `\n>*Comment:*\n>${comment}`; + } + + await slack.chat.postMessage({ + blocks, + channel: process.env.SLACK_CHANNEL, + } as any); + // Set a "recently submitted" cookie that expires after one minute to mitigate spam res.setHeader( 'Set-Cookie', diff --git a/package.json b/package.json index e7b686e239f..28402d65e31 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "carbon-components-react": "^7.18.0" }, "dependencies": { + "@slack/web-api": "^5.11.0", "carbon-components-react": "^7.18.0", "classnames": "^2.2.6", "react": "^16.8.6" @@ -116,7 +117,7 @@ } }, "lint-staged": { - "*.{js,ts}": [ + "*.{js}": [ "eslint --fix" ], "*.{css,scss,json,html,yaml,md,mdx}": [ diff --git a/yarn.lock b/yarn.lock index 1ea18ebda2c..df7aac15ca1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2490,6 +2490,35 @@ resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.7.0.tgz#9a06f4f137ee84d7df0460c1fdb1135ffa6c50fd" integrity sha512-ONhaKPIufzzrlNbqtWFFd+jlnemX6lJAgq9ZeiZtS7I1PIf/la7CW4m83rTXRnVnsMbW2k56pGYu7AUFJD9Pow== +"@slack/logger@>=1.0.0 <3.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@slack/logger/-/logger-2.0.0.tgz#6a4e1c755849bc0f66dac08a8be54ce790ec0e6b" + integrity sha512-OkIJpiU2fz6HOJujhlhfIGrc8hB4ibqtf7nnbJQDerG0BqwZCfmgtK5sWzZ0TkXVRBKD5MpLrTmCYyMxoMCgPw== + dependencies: + "@types/node" ">=8.9.0" + +"@slack/types@^1.7.0": + version "1.8.0" + resolved "https://registry.yarnpkg.com/@slack/types/-/types-1.8.0.tgz#a5a0b31bace03f524174991dfc41c60311e6f32f" + integrity sha512-YvLCtxqbIdCCI+xMQBFH3GJVhRp8jJNl8BUE0RgJlZcDF+wXSB1wkcgLz7zHtD3oOF39GedYiE1e/rQrZ4Dr1A== + +"@slack/web-api@^5.11.0": + version "5.11.0" + resolved "https://registry.yarnpkg.com/@slack/web-api/-/web-api-5.11.0.tgz#6549ec71d13c2837cc672cbf7793a88eef22802f" + integrity sha512-4a/uj7IZjFLu7Qmq0nH74ecLqk1iI/9x3yRS/v6M5vXDyc5lEruRFp4d5/bz4eN5Bathlq4Bws0wioY516fPag== + dependencies: + "@slack/logger" ">=1.0.0 <3.0.0" + "@slack/types" "^1.7.0" + "@types/is-stream" "^1.1.0" + "@types/node" ">=8.9.0" + "@types/p-queue" "^2.3.2" + axios "^0.19.0" + eventemitter3 "^3.1.0" + form-data "^2.5.0" + is-stream "^1.1.0" + p-queue "^2.4.2" + p-retry "^4.0.0" + "@szmarczak/http-timer@^1.1.2": version "1.1.2" resolved "https://registry.yarnpkg.com/@szmarczak/http-timer/-/http-timer-1.1.2.tgz#b1665e2c461a2cd92f4c1bbf50d5454de0d4b421" @@ -2580,6 +2609,13 @@ dependencies: "@types/node" "*" +"@types/is-stream@^1.1.0": + version "1.1.0" + resolved "https://registry.yarnpkg.com/@types/is-stream/-/is-stream-1.1.0.tgz#b84d7bb207a210f2af9bed431dc0fbe9c4143be1" + integrity sha512-jkZatu4QVbR60mpIzjINmtS1ZF4a/FqdTUTBeQDVOQ2PYyidtwFKr0B5G6ERukKwliq+7mIXvxyppwzG5EgRYg== + dependencies: + "@types/node" "*" + "@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0": version "2.0.3" resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.3.tgz#4ba8ddb720221f432e443bd5f9117fd22cfd4762" @@ -2647,11 +2683,21 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-14.6.0.tgz#7d4411bf5157339337d7cff864d9ff45f177b499" integrity sha512-mikldZQitV94akrc4sCcSjtJfsTKt4p+e/s0AGscVA6XArQ9kFclP+ZiYUMnq987rc6QlYxXv/EivqlfSLxpKA== +"@types/node@>=8.9.0": + version "14.6.4" + resolved "https://registry.yarnpkg.com/@types/node/-/node-14.6.4.tgz#a145cc0bb14ef9c4777361b7bbafa5cf8e3acb5a" + integrity sha512-Wk7nG1JSaMfMpoMJDKUsWYugliB2Vy55pdjLpmLixeyMi7HizW2I/9QoxsPCkXl3dO+ZOVqPumKaDUv5zJu2uQ== + "@types/node@^8.5.7": version "8.10.62" resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.62.tgz#8d0d0db44a46ae7679d92e2e36d41bf1c3625d6a" integrity sha512-76fupxOYVxk36kb7O/6KtrAPZ9jnSK3+qisAX4tQMEuGNdlvl7ycwatlHqjoE6jHfVtXFM3pCrCixZOidc5cuw== +"@types/p-queue@^2.3.2": + version "2.3.2" + resolved "https://registry.yarnpkg.com/@types/p-queue/-/p-queue-2.3.2.tgz#16bc5fece69ef85efaf2bce8b13f3ebe39c5a1c8" + integrity sha512-eKAv5Ql6k78dh3ULCsSBxX6bFNuGjTmof5Q/T6PiECDq0Yf8IIn46jCyp3RJvCi8owaEmm3DZH1PEImjBMd/vQ== + "@types/parse-json@^4.0.0": version "4.0.0" resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" @@ -2688,6 +2734,11 @@ "@types/prop-types" "*" csstype "^3.0.2" +"@types/retry@^0.12.0": + version "0.12.0" + resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d" + integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA== + "@types/rimraf@^2.0.2": version "2.0.4" resolved "https://registry.yarnpkg.com/@types/rimraf/-/rimraf-2.0.4.tgz#403887b0b53c6100a6c35d2ab24f6ccc042fec46" @@ -3515,7 +3566,7 @@ axios@^0.18.1: follow-redirects "1.5.10" is-buffer "^2.0.2" -axios@^0.19.2: +axios@^0.19.0, axios@^0.19.2: version "0.19.2" resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27" integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA== @@ -7894,6 +7945,15 @@ forever-agent@~0.6.1: resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE= +form-data@^2.5.0: + version "2.5.1" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.5.1.tgz#f2cbec57b5e59e23716e128fe44d4e5dd23895f4" + integrity sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.6" + mime-types "^2.1.12" + form-data@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682" @@ -12866,6 +12926,11 @@ p-pipe@^1.1.0: resolved "https://registry.yarnpkg.com/p-pipe/-/p-pipe-1.2.0.tgz#4b1a11399a11520a67790ee5a0c1d5881d6befe9" integrity sha1-SxoROZoRUgpneQ7loMHViB1r7+k= +p-queue@^2.4.2: + version "2.4.2" + resolved "https://registry.yarnpkg.com/p-queue/-/p-queue-2.4.2.tgz#03609826682b743be9a22dba25051bd46724fc34" + integrity sha512-n8/y+yDJwBjoLQe1GSJbbaYQLTI7QHNZI2+rpmCDbe++WLf9HC3gf6iqj5yfPAV71W4UF3ql5W1+UBPXoXTxng== + p-queue@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/p-queue/-/p-queue-5.0.0.tgz#80f1741d5e78a6fa72fce889406481baa5617a3c" @@ -12885,6 +12950,14 @@ p-retry@^3.0.1: dependencies: retry "^0.12.0" +p-retry@^4.0.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/p-retry/-/p-retry-4.2.0.tgz#ea9066c6b44f23cab4cd42f6147cdbbc6604da5d" + integrity sha512-jPH38/MRh263KKcq0wBNOGFJbm+U6784RilTmHjB/HM9kH9V8WlCpVUcdOmip9cjXOh6MxZ5yk1z2SjDUJfWmA== + dependencies: + "@types/retry" "^0.12.0" + retry "^0.12.0" + p-timeout@^1.1.1: version "1.2.1" resolved "https://registry.yarnpkg.com/p-timeout/-/p-timeout-1.2.1.tgz#5eb3b353b7fce99f101a1038880bb054ebbea386"