2018-02-14 11:37:49 +00:00
|
|
|
// ISC License
|
|
|
|
|
|
|
|
// Copyright (c) 2017, Tom McKenzie <tom@chillidonut.com>
|
|
|
|
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
const RtmClient = require('@slack/client').RtmClient
|
|
|
|
const WebClient = require('@slack/client').WebClient
|
|
|
|
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS
|
|
|
|
const RTM_EVENTS = require('@slack/client').RTM_EVENTS
|
|
|
|
|
|
|
|
const BOT_TOKEN = process.env.SLACK_BOT_TOKEN || ''
|
2018-01-23 14:27:25 +00:00
|
|
|
|
2018-02-22 20:58:54 +00:00
|
|
|
module.exports = {
|
|
|
|
BotUserName: 'probot',
|
|
|
|
setup: _setup,
|
|
|
|
sendMessage: _sendMessage
|
|
|
|
}
|
2018-02-13 16:06:55 +00:00
|
|
|
|
2018-02-22 20:58:54 +00:00
|
|
|
function _setup (robot, connectCallback) {
|
2018-02-14 11:37:49 +00:00
|
|
|
if (!BOT_TOKEN) {
|
|
|
|
robot.log.error('SLACK_BOT_TOKEN missing, skipping Slack integration')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
function emit (payload) {
|
|
|
|
robot.receive({
|
|
|
|
event: 'slack',
|
|
|
|
payload: {
|
|
|
|
...payload,
|
|
|
|
installation: {}, // We need to add an 'installation' property, otherwise node_modules/probot/lib/robot.js:100 will throw an exception
|
|
|
|
slack: SlackAPI,
|
|
|
|
slackWeb: SlackWebAPI
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot.log.trace('Slack connecting...')
|
|
|
|
|
|
|
|
// game start!
|
|
|
|
const SlackAPI = new RtmClient(BOT_TOKEN)
|
|
|
|
const SlackWebAPI = new WebClient(BOT_TOKEN)
|
|
|
|
|
|
|
|
// The client will emit an RTM.AUTHENTICATED event on successful connection, with the `rtm.start` payload
|
|
|
|
SlackAPI.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
|
|
|
|
robot.log.trace('Slack successfully authenticated')
|
|
|
|
|
|
|
|
emit({
|
|
|
|
action: 'authenticated',
|
|
|
|
payload: rtmStartData
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// you need to wait for the client to fully connect before you can send messages
|
|
|
|
SlackAPI.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, () => {
|
|
|
|
robot.log.info('Slack connected')
|
|
|
|
|
|
|
|
emit({
|
|
|
|
action: 'connected'
|
|
|
|
})
|
|
|
|
|
|
|
|
connectCallback(SlackAPI)
|
|
|
|
})
|
|
|
|
|
|
|
|
// bind to all supported events <https://api.slack.com/events>
|
|
|
|
for (const event in RTM_EVENTS) {
|
|
|
|
SlackAPI.on(event, (payload) => {
|
|
|
|
emit({
|
|
|
|
action: event,
|
|
|
|
payload
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// now connect
|
|
|
|
SlackAPI.connect('https://slack.com/api/rtm.connect')
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:58:54 +00:00
|
|
|
async function _sendMessage (robot, room, message) {
|
2018-01-23 14:27:25 +00:00
|
|
|
// Send message to Slack
|
2018-02-14 11:37:49 +00:00
|
|
|
if (robot.slack) {
|
2018-02-12 18:43:12 +00:00
|
|
|
// TODO BOUNTY migrate away from datastore:
|
|
|
|
// https://github.com/slackapi/node-slack-sdk/wiki/DataStore-v3.x-Migration-Guide
|
2018-02-14 11:37:49 +00:00
|
|
|
const channel = robot.slack.dataStore.getChannelByName(room)
|
2018-01-23 14:27:25 +00:00
|
|
|
try {
|
2018-01-28 08:24:30 +00:00
|
|
|
if (process.env.DRY_RUN) {
|
|
|
|
robot.log.debug(`Would have sent '${message}' to '${room}' channel`)
|
|
|
|
} else {
|
2018-02-14 11:37:49 +00:00
|
|
|
await robot.slack.sendMessage(message, channel.id)
|
2018-01-23 18:38:25 +00:00
|
|
|
}
|
2018-01-28 08:24:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
robot.log.error(`Failed to send Slack message to '${room}' channel`)
|
2018-01-23 14:27:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-28 08:24:30 +00:00
|
|
|
robot.log.debug('Slack client not available')
|
2018-01-23 14:27:25 +00:00
|
|
|
}
|
|
|
|
}
|