aboutsummaryrefslogtreecommitdiff
path: root/src/server/routes/slack.js
blob: e256f142cdf2706fe1dc5fe32e7f302933b8deb6 (plain)
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
const express = require('express')
const router = express.Router()

const getUserIndex = require('../lib/getUserIndex')

function generateResponse(query) {
  return getUserIndex().then(users => {
    const user =
      users.filter(user => user.value === query)[0]

    if (!user) {
      return {
        "response_type": "ephemeral",
        "mrkdwn": true,
        "text": `Sorry, I tried my best, but I couldn't find _${query}_`
      }
    }

    return {
      "response_type": "in_channel",
      "text": `Here is the schedule of _${query}_`,
      "mrkdwn": true,
      "attachments": [
        {
          "fallback": `https://beta.rooster.hetmml.nl/${user.type}/${user.value}`,
          "image_url": `https://beta.rooster.hetmml.nl/get/${user.type}/${user.value}.png`
        }
      ]
    }
  })
}

router.all('/', function (req, res, next) {
  const query = req.body.text || req.query.text

  if (query.indexOf('!') === 0) {
    switch (query) {
      case "!help":
        res.json({
          "response_type": "ephemeral",
          "mrkdwn": true,
          "text": "Here are some examples on how you can use me\n>/rooster 18561\n\n>/rooster akh\n\n>/rooster 6-5H2\n\n>/rooster 008-mk\n\nPlease note that the following does not work (yet)",
          "attachments": [{
            "text": "/rooster 5h2",
            "color": "danger"
          }]
        })
      default:
        res.json({
          "response_type": "ephemeral",
          "text": "Unrecognized command, try !help"
        })
        break;
    } 
  }

  generateResponse(query).then((json) => res.json(json))
})

module.exports = router