-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
86 lines (63 loc) · 2.09 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
"use strict";
require('dotenv').config();
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const battle = require('./battle.js');
const formatted = require('./format_output.js');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname, 'landing-page/dist')));
const apikey = process.env.SLACK_API_TOKEN;
app.set('port', (process.env.PORT || 9001));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/landing-page/dist/index.html');
});
app.get('/hello', (req, res) => {
res.send('hello');
});
app.post('/', (req, res) => {
const userText = req.body.text.split(" ");
const firstUser = userText[0];
var secondUser = "";
if (userText.length > 1){
//check how many words user entered
secondUser = userText[1];
}
if (!firstUser || !secondUser || firstUser.toLowerCase() === "help" || secondUser.toLowerCase() == "help" ){
//check if user input is "help" or empty and return help
const help = getHelp();
let data = {
response_type: "ephemeral",
"text": "" ,
"mrkdwn": true,
"attachments": [
{
"text": help}]};
res.json(data);
}
else{
battle.getData([firstUser, secondUser]).then(users => {
const winObj = battle.getWinner(users);
const body = formatted.outputMessage(users, winObj);
res.send(body);
}).catch(err => {
console.error('An error occurred making this request');
console.error(err);
res.json({
response_type: "ephemeral",
"text": "" ,
"mrkdwn": true,
"attachments": [
{
"text": "Please enter a valid github username(s)"}]});
});
}
});
function getHelp(){
return "Enter two correct Github usernames to compare their stats and determine winner! Check the example below:\n\n/btfbot wesbos torvalds"
}
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});