-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
81 lines (72 loc) · 2.25 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
const express = require('express');
const bodyParser = require('body-parser');
const { Client } = require('pg');
const config = require('./pg-config');
const server = express();
server.use(
bodyParser.urlencoded({
extended: false,
type: 'application/x-www-form-urlencoded',
})
);
const save = (name, id) => {
const postgres = new Client(config);
postgres.connect();
const statement =
'insert into helpers values ($1, $2) on conflict (slackid) do update set name=$1';
const values = [name, id];
postgres.query(statement, values, (err, res) => {
console.log('save log: ', err ? err.stack : res);
postgres.end();
});
};
const fetchName = id =>
new Promise((resolve, reject) => {
const postgres = new Client(config);
postgres.connect();
const statement = 'select name from helpers where slackid=$1';
const values = [id];
postgres.query(statement, values, (err, res) => {
if (err) {
reject(err);
} else {
const result = res.rows.length ? res.rows[0].name : undefined;
resolve(result);
}
postgres.end();
});
});
server.post('/slash/', (req, res) => {
if (req.body.token === process.env.slackSlashToken) {
const [subcmd, ...params] = req.body.text.split(' ');
if (subcmd === 'register') {
if (params.length > 0) {
const name = params[0];
const response = {
text: `I'll add you (${name}) to the list of helpers. Thanks 🙇`,
};
res.json(response);
save(name, req.body.user_id);
} else {
const response = {
text:
"✋ Tell me the name you'll use to signup for lunch duties in the spreadsheet. \nFor example: `/lunch register Adam`",
};
res.json(response);
}
} else if (subcmd === 'whoami') {
const result = fetchName(req.body.user_id).then(name => {
const response = {
text: name
? `You are: 🆔 \`${name}\``
: `Looks like you aren't signed up. Go ahead and register via:\n \`/lunch register NAME\``,
};
res.json(response);
});
}
} else {
res.status(500).end();
}
});
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`🚀 listening on ${port}`));