-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (89 loc) · 2.91 KB
/
index.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
require('dotenv').config();
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var app = express();
var urlencodedParser = bodyParser.urlencoded({
extended: false
});
// Slack Utilities
const {
RTMClient
} = require('@slack/rtm-api');
const {
WebClient
} = require('@slack/web-api');
const token = process.env.SLACK_TOKEN;
const rtm = new RTMClient(token);
const web = new WebClient(token);
const {
createMessageAdapter
} = require('@slack/interactive-messages');
const slackInteractions = createMessageAdapter(process.env.SLACK_SIGNING_SECRET);
const port = process.env.PORT || 3000;
// Cloud Services
var gcpCompute = require('./gcloud/compute/compute.js');
// Utils Services
var utils = require('./lib/utils.js');
var utilsSlack = require('./lib/utilsSlack.js');
// Connect to MongoDB
mongoose
.connect(
'mongodb://mongo:27017/app', {
useNewUrlParser: true
}
)
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));
rtm.start()
.catch(console.error);
rtm.on('channel_joined', (event) => {
utils.getHelp(rtm, web, event.channel.id);
});
rtm.on('message', (event) => {
//console.log(event);
if (event.type !== 'message' ||
(event.hasOwnProperty('username') && event.username == "splink" &&
event.hasOwnProperty('subtype') && event.subtype == "bot_message")) {
return;
}
if (event.text.includes("splink help")) {
utils.getHelp(rtm, web, event.channel);
}
if (event.text.startsWith("splink vm create")) {
utilsSlack.openDialogForCreateVM();
//gcpCreate.createVirtualMachine(process.env.GCP_SECRETS, 'eloquent-walker-177701', 'test');
}
if (event.text.startsWith("splink vm list")) {
gcpCompute.listVMs().then(function (response) {
utilsSlack.slackResponse(rtm, web, event.channel, response, 'Summary of Available Resources on Google Cloud Platform');
});
}
});
var rawBodySaver = function (req, res, buf, encoding) {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8');
}
};
app.use(bodyParser.json({
verify: rawBodySaver
}));
app.use(bodyParser.urlencoded({
verify: rawBodySaver,
extended: true
}));
app.use(bodyParser.raw({
verify: rawBodySaver,
type: function () {
return true
}
}));
// Interactive Messages
app.post('/slack/actions', urlencodedParser, (req, res) => {
res.status(200).end() // best practice to respond with 200 status
if (utils.isValidSlackRequest(req)) {
var actionJSONPayload = JSON.parse(req.body.payload) // parse URL-encoded payload JSON string
utils.delegateRequestForAction(actionJSONPayload.trigger_id, actionJSONPayload.callback_id, actionJSONPayload.actions, actionJSONPayload.response_url);
}
});
app.listen(port, () => console.log("Server running..."));