-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
216 lines (191 loc) · 7.29 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const { App } = require("@slack/bolt");
const WebPageTest = require("webpagetest");
const slackHelpers = require("./utils/slackHelpers");
const wptHelpers = require("./utils/wptHelpers");
const connectDB = require("./config/db.js");
const colors = require("colors");
const orgAuth = require("./config/auth/store_user_org_install");
const workspaceAuth = require("./config/auth/store_user_workspace_install");
const dbQuery = require("./config/find_user");
require("dotenv").config();
//WPT config
let WPT_API_KEY;
let channel_temp_id;
let options = {
firstViewOnly: true,
runs: 1,
location: "ec2-us-east-1:Chrome",
connectivity: "4G",
pollResults: 5,
lighthouse: 1,
//timeout: 240, //Could be activated later
};
const devEnvCheck = process.env.ENVIRONMENT === "DEVELOPMENT";
if (devEnvCheck !== true) connectDB();
// Initializes your app with your bot token and signing secret
const app = new App({
token: devEnvCheck == true ? process.env.SLACK_BOT_TOKEN : undefined, //for development
signingSecret: process.env.SLACK_SIGNING_SECRET, //for production
clientId: devEnvCheck == false ? process.env.SLACK_CLIENT_ID : undefined, //for production
clientSecret: devEnvCheck == false ? process.env.SLACK_CLIENT_SECRET : undefined,
stateSecret: devEnvCheck == false ? "my-state-secret" : undefined,
redirectUri: `${process.env.HOST_URI}/slack/oauth_redirect`,
scopes: ["commands", "chat:write"],
installerOptions: {
redirectUriPath: "/slack/oauth_redirect",
directInstall: true,
},
installationStore: {
storeInstallation: async (installation) => {
if (installation.isEnterpriseInstall && installation.enterprise !== undefined) {
return orgAuth.saveUserOrgInstall(installation);
}
if (installation.team !== undefined) {
return workspaceAuth.saveUserWorkspaceInstall(installation);
}
throw new Error("Failed saving installation data to installationStore");
},
fetchInstallation: async (installQuery) => {
if (installQuery.isEnterpriseInstall && installQuery.enterpriseId !== undefined) {
return dbQuery.findUser(installQuery.enterpriseId);
}
if (installQuery.teamId !== undefined) {
return dbQuery.findUser(installQuery.teamId);
}
throw new Error("Failed fetching installation");
},
deleteInstallation: async (installQuery) => {
console.log("This part is not handeled yet");
},
},
customRoutes: [
{
path: "/",
method: ["GET"],
handler: (req, res) => {
res.writeHead(200);
res.end("WPT Slack server is running!!");
},
},
],
});
function validateUrl(value) {
return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(
value
);
}
// Test Modal
app.command("/webpagetest", async ({ ack, body, client, logger }) => {
await ack();
channel_temp_id = body.channel_id;
const wpt = new WebPageTest("www.webpagetest.org", "WPT_API_KEY"); // Your WPT API Key
const locationsResult = await wptHelpers.getLocations(wpt, options);
const allLocations = locationsResult.result.response.data.location;
try {
const result = await client.views.open({
trigger_id: body.trigger_id,
view: slackHelpers.dialogView(allLocations, body.text, WPT_API_KEY, body.channel_id),
});
} catch (error) {
logger.error(error);
}
});
// Key Update Modal
app.command("/updatekey", async ({ ack, body, client, logger }) => {
await ack();
channel_temp_id = body.channel_id;
WPT_API_KEY = body.text; //updating the key
try {
await client.chat.postMessage({
channel: body.channel_id,
text: body.text,
blocks: slackHelpers.keyUpdateBlock(body.channel_id),
});
} catch (error) {
if (error.error)
await client.chat.postMessage({
text: "Response from WPT",
channel: channel_temp_id,
blocks: slackHelpers.errorBlock(error.error.code, error.error.message),
});
else
await client.chat.postMessage({
text: "Response from WPT",
channel: channel_temp_id,
blocks: slackHelpers.errorBlock(error.statusCode, error.statusText),
});
}
});
// Submitting Test
app.view("SUBMIT_TEST", async ({ ack, payload, client }) => {
await ack();
try {
const { values } = payload.state;
let urls;
if (values.key) WPT_API_KEY = values.key.key.value;
const blocks = payload.blocks;
if (values.url) urls = values.url.url.value;
else {
for (let i = 0; i < blocks.length; i++) {
if (blocks[i].block_id == "url") {
urls = blocks[i].text.text;
}
}
}
urls.split(",").map(async (url) => {
//Valid URL check
if (!validateUrl(url.trim())) {
return await client.chat.postMessage({
text: "Response from WPT",
channel: channel_temp_id,
blocks: slackHelpers.errorBlock("400", `${url.trim()} is not a valid URL, Please enter a valid URL`),
});
}
options.location = values.location.location.selected_option.value;
options.runs = values.runs.runs.selected_option.value;
options.connectivity = values.connectivity.connectivity.selected_option.value;
options.emulateMobile = values.emulateMobile.emulateMobile.selected_option.value == "true" ? true : false;
options.device = values.mobiledevice.mobiledevice.selected_option.value;
const wpt = new WebPageTest("www.webpagetest.org", WPT_API_KEY); // Your WPT API Key
const wptPromise = wptHelpers.runTest(wpt, url.trim(), options);
await client.chat.postMessage({
text: "Response from WPT",
channel: channel_temp_id,
blocks: slackHelpers.testSubmissionBlock(url.trim()),
});
const wptResult = await wptPromise;
const wptResultLink = wptResult.result.data.summary;
const waterfallLink = wptResult.result.data.median.firstView.images.waterfall;
await client.chat.postMessage({
text: "Response from WPT",
channel: channel_temp_id,
blocks: slackHelpers.wptResponseBlock(url.trim(), wptResultLink, waterfallLink),
});
});
} catch (error) {
console.log(error);
if (error.error)
await client.chat.postMessage({
text: "Response from WPT",
channel: channel_temp_id,
blocks: slackHelpers.errorBlock(error.error.code, error.error.message),
});
else
await client.chat.postMessage({
text: "Response from WPT",
channel: channel_temp_id,
blocks: slackHelpers.errorBlock(error.statusCode, error.statusText),
});
}
});
//Catching button actions
app.action("button-action", async ({ body, ack, say }) => {
// Acknowledge the action
await ack();
// Here we can customize functionality for our buttons.
// Leaving it blank for now just for acknowledgement
});
(async () => {
await app.start(process.env.PORT || 3000);
console.log("⚡️ Webpagetest Slack app is running @ Port: " + process.env.PORT + " " + process.env.ENVIRONMENT);
})();