Last active
March 1, 2018 09:02
-
-
Save kana/234a8c54fb427c59b1e7d4335f37a2b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bodyParser = require('body-parser') | |
const express = require('express') | |
const moment = require('moment-timezone') | |
const slack = require('slack') | |
const VERIFICATION_TOKEN = process.env.VERIFICATION_TOKEN | |
const OAUTH_ACCESS_TOKEN = process.env.OAUTH_ACCESS_TOKEN | |
const app = express() | |
app.use(bodyParser.json()) | |
app.set('port', process.env.PORT || 5000) | |
app.get('/', (req, res) => { | |
res.send('Hello') | |
}) | |
app.post('/', async (req, res) => { | |
if (!req.body) { | |
res.end() | |
console.log('missing body') | |
return | |
} | |
if (req.body.token !== VERIFICATION_TOKEN) { | |
res.end() | |
console.log('invalid token') | |
return | |
} | |
if (req.body.challenge) { | |
return res.send(req.body.challenge) | |
} | |
if (req.body.event && req.body.event.type === 'message') { | |
res.end() | |
await handleMessage(req.body.event.text || '') | |
return | |
} | |
res.end() | |
}) | |
app.listen(app.get('port'), () => { | |
console.log('Node app is running on port', app.get('port')) | |
}) | |
async function handleMessage (text) { | |
if (!text.match(/\b(kana|@kana|U1UP30NRL)\b/)) { | |
return | |
} | |
const p = await usersProfileGet() | |
const statusText = p.profile.status_text || '' | |
const m = statusText.match(/^(\d+)個レビューしてる\((.*)\)$/) || [] | |
const statusCount = parseInt(m[1] || '0', 10) | |
const currentDate = moment().tz('Japan').format('M/D') | |
const statusDate = m[2] || currentDate | |
const moreCount = (text.match(/https:\/\/github\.com\/YOUR_ORGANIZATION\/[^\/]+\/pull\/\d+/g) || []).length | |
const newCount = currentDate === statusDate ? statusCount + moreCount : moreCount | |
const emojiTable = { | |
0: ':zero:', | |
1: ':one:', | |
2: ':two:', | |
3: ':three:', | |
4: ':four:', | |
5: ':five:', | |
6: ':six:', | |
7: ':seven:', | |
8: ':eight:', | |
9: ':nine:', | |
10: ':keycap_ten:' | |
} | |
const emoji = emojiTable[newCount] || ':infinity:' | |
usersProfileSet({ | |
status_emoji: emoji, | |
status_text: `${newCount}個レビューしてる(${currentDate})` | |
}) | |
} | |
async function usersProfileGet () { | |
return await slack.users.profile.get({ | |
token: OAUTH_ACCESS_TOKEN | |
}); | |
} | |
async function usersProfileSet (statusData) { | |
return await slack.users.profile.set({ | |
token: OAUTH_ACCESS_TOKEN, | |
profile: JSON.stringify(statusData) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment