-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
113 lines (106 loc) · 3.16 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
const express = require('express')
const app = express()
const axios = require('axios')
const admin = require('./firebase-admin')
const models = require('./models')
app.use(express.static('public'))
app.use(require('body-parser').urlencoded({ extended: false }))
app.get('/', function(req, res) {
res.sendFile(__dirname + '/views/index.html')
})
async function postRequest(requesterId, responseUrl, description, type, payload) {
const ref = await admin.database().ref('bot/requests').push({
description,
type,
payload,
status: 'pending',
createdAt: admin.database.ServerValue.TIMESTAMP,
requesterId,
responseUrl
})
return {
// response_type: 'in_channel',
text: `<@${requesterId}> :ok_hand: Request \`${ref.key}\` to ${description} has been received.`
}
}
async function stupidBot(requesterId, responseUrl, text) {
const args = text.split(/\s+/).filter(x => x.trim())
const post = (description, type, payload) => postRequest(requesterId, responseUrl, description, type, payload)
if (args[0] === 'add') {
const out = []
text.replace(/<@(U\w+)/g, (a, x) => out.push(x))
if (out.length < 1) {
throw new Error(`Please tag the users you would like to add to your team.`)
}
return post(
`add ${out.map(x => `<@${x}>`)} to your team`,
'AddToTeam',
{ addeeIds: out }
)
} else if (args[0] === 'leave') {
return post(
`leave your current team`,
'LeaveTeam',
{ }
)
} else if (args[0] === 'set') {
const value = args.slice(2).join(' ')
return post(
`change your team’s \`${args[1]}\` to “${value}”`,
'SetTeamAttribute',
{ key: args[1], value: value }
)
} else if (args[0] === undefined || args[0] === 'info') {
return {
text: await models.getTeamInfo(requesterId)
}
} else if (args[0] === 'list') {
return {
text: await models.listAllTeams()
}
} else if (args[0] === 'fsck') {
return post(
`fsck`,
'Fsck',
{ }
)
} else if (args[0] === 'retry') {
const key = args[1]
const ref = admin.database().ref('bot/requests').child(key)
const item = await ref.once('value')
if (!item.exists()) {
throw new Error('Request does not exist')
}
if (!item.child('status').val() === 'pending') {
throw new Error('Request is already pending')
}
await ref.child('status').set('pending')
} else {
throw new Error('Unrecognized command...')
}
}
app.post('/stupid', async function(req, res, next) {
try {
const result = await stupidBot(req.body.user_id, req.body.response_url, req.body.text)
res.json(result)
} catch (err) {
axios.post(process.env.REPORTING_SLACK_WEBHOOK_URL, {
text: [
`Failed to run command: /team ${req.body.text}`,
`Triggered by <@${req.body.user_id}>`,
'',
'```',
String(err && err.stack),
'```',
].join('\n')
})
res.json({
response_type: 'ephemeral',
text: `Failed -- ${err}`
})
}
})
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port)
})
require('./queue-processor').start()