-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
155 lines (136 loc) · 5.21 KB
/
server.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
// server.js
//
// // BASE SETUP
// // =============================================================================
//
// // call the packages we need
var express = require("express");
var bodyParser = require('body-parser');
var RtmClient = require('@slack/client').RtmClient;
var math = require('mathjs');
var request = require('request');
var app = express();
// configure app to use bodyParser()
// // this will let us get the data from a POST
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.set('port', (process.env.PORT || 8080));
app.use(express.static(__dirname + "/public"));
// SLACK CONNECTIONS
var slackToken = process.env.SLACK_APIKEY || '';
var slackVerificationCode = process.env.SLACK_VERIFICATION_TOKEN || '';
//var rtm = new RtmClient(token, {logLevel: 'debug'});
//rtm.start();
// ROUTES FOR OUR API
// // =============================================================================
var router = express.Router(); // get an instance of the express Router
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
// slack calculator api
// slack Authentication
router.get("/calc/slack", function(req, res) {
var code = req.query.code;
// call auth to get the token from slack
request({
url: "https://slack.com/api/oauth.access",
qs: {
client_id: process.env.SLACK_CLIENT_ID,
client_secret: process.env.SLACK_APIKEY,
code: code
},
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: ""
}, function(error, response, body) {
if(error) {
res.status(400).json("Authentication failed with Slack!");
} else {
res.status(200).json(response);
}
});
});
router.post("/calc/slack", function(req, res) {
// api auth
//if (slackToken == req.body.token) {
var expression = req.body.text;
// check if the body is `help`
if (expression == 'help' || expression == '') {
res.status(200).json({"text": "*Hi!* Need to do a calculation while doing your work on Slack? Now, no need to leave slack at all!",
"username": "calcbot",
"mrkdwn": true,
"attachments": [
{
"author_name": "Sudhanshu Shekhar",
"author_link": "https://www.linkedin.com/in/sudhanshushekhar1",
"author_icon": "https://avatars0.githubusercontent.com/u/7766076?v=3&u=ecc19fccc2e6f1039d7e597b01064c43052fd2bb&s=140",
"title": "try `/calc (2.25 + 76.0) * 18` or simply `/calc 2 + 2`",
"text": "Supports all normal and scientific expressions! In Slack Calculator Documentation <https://slack-calc.herokuapp.com>",
"color": "good",
"mrkdwn_in": ["text", "pretext", "title"]
}
]});
} else {
try {
var value = math.round(math.eval(expression), 3);
res.status(200).json({"text": expression ,
"username": "calcbot",
"mrkdwn": true,
"attachments": [
{
"title": value,
"color": "good"
}
]});
} catch (err) {
res.status(200).json({"text": "`" + expression + "` seems to be broken! Would you mind having a second look and try again?",
"username": "calcbot",
"mrkdwn": true,
"attachments": [
{
"title": "NAN",
"color": "warning"
}
]});
}
}
/*} else {
res.status(200).json({"username": "calcbot",
"mrkdwn": true,
"attachments": [
{
"pretext": "Authentication failed",
"title": "NAN",
"color": "danger"
}
]});
}*/
});
// general test calc
router.post("/calc", function(req, res) {
// api auth
var headers = req.headers;
var apiKey = headers['x-igloo-apikey'];
if (apiKey == process.env.APIKEY) {
var expression = req.body.expression;
try {
var value = math.round(math.eval(expression), 3);
res.status(200).json({"status":1, "message":"expression `" + expression + "` calculated successfully", "value" : value});
} catch (err) {
res.status(500).json({"status":0, "message":"expression `" + expression + "` seems to be broken. Please have a look and try again!", "value" : "NAN"});
}
} else {
res.status(400).json({"status":0, "message":"Authentication failed", "value" : "NAN"});
}
});
// REGISTER OUR ROUTES -------------------------------
// // all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// // =============================================================================
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});