-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlackFlowConnector.js
More file actions
71 lines (56 loc) · 1.74 KB
/
Copy pathSlackFlowConnector.js
File metadata and controls
71 lines (56 loc) · 1.74 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
69
70
71
const querystring = require('querystring');
const fetch = require('node-fetch');
const SLACK_SLASH_TOKEN = "SLACK_SLASH_TOKEN";
const FLOW_URL = "FLOW_URL";
function parseHookBody(body) {
const qs = querystring.parse(body);
return qs && qs.token && qs.token == SLACK_SLASH_TOKEN &&
qs.command && qs.text;
}
function getFlowArguments(body) {
const split = body.split(" ");
const icmId = split.shift();
const icmType = split.join(" ");
return {
icm_id: icmId,
icm_type: icmType
};
}
function submitFlowRequest(args) {
return fetch(FLOW_URL, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(args)
});
}
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const slashInput = req.body && parseHookBody(req.body) || null;
if (slashInput) {
context.log("Input successfully validated.");
const flowArgs = getFlowArguments(slashInput);
const flowRequest = submitFlowRequest(flowArgs);
flowRequest.then((response) => {
context.log("Flow request complete.");
context.log(response);
context.res = {
// status: 200, /* Defaults to 200 */
body: "Flow completed with status code " + response.status
};
context.done();
response.text().then((text) => {
context.log("Response: " + text);
});
});
}
else {
context.log("Input could not be validated.");
context.res = {
status: 400,
body: "Bad request"
};
context.done();
}
};