Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.
/ pr-police Public archive

Commit

Permalink
feat: add support for bot commands
Browse files Browse the repository at this point in the history
This allows the bot to listen to predefined commands, so that we can
call his output in a public channel.
  • Loading branch information
rogeriopvl committed May 2, 2017
1 parent b080425 commit 4767f3e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = [
'what needs review?',
'!review',
]
13 changes: 12 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const commands = require('./commands')

const isDirectMessage = function isDirectMessage (msg) {
// slack direct messages channel id start with D
return msg.type === 'message' && msg.channel.charAt(0) === 'D'
Expand All @@ -7,4 +9,13 @@ const isBotMessage = function isBotMessage (msg) {
return msg.subtype && msg.subtype === 'bot_message'
}

module.exports = { isDirectMessage, isBotMessage }
const isMessage = function isMessage (msg) {
return msg.type === 'message'
}

// for now all commands execute the same operation
const isBotCommand = function isBotCommand (msg) {
return commands.some((command) => msg.text === command)
}

module.exports = { isDirectMessage, isBotMessage, isMessage, isBotCommand }
12 changes: 8 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const Slackbot = require('slackbots')
const pullhub = require('pullhub')
const { isDirectMessage, isBotMessage } = require('./helpers')
const {
isDirectMessage,
isBotMessage,
isMessage,
isBotCommand
} = require('./helpers')

module.exports = function server () {
const env = process.env
Expand Down Expand Up @@ -32,9 +37,8 @@ module.exports = function server () {
})

bot.on('message', (data) => {
if (isDirectMessage(data) && !isBotMessage(data)) {
console.log(data)

if ((isMessage(data) && isBotCommand(data)) ||
(isDirectMessage(data) && !isBotMessage(data))) {
getPullRequests()
.then(buildMessage)
.then((message) => {
Expand Down

0 comments on commit 4767f3e

Please sign in to comment.