2016-03-18 20:05:21 -04:00
|
|
|
config = require '../server_config'
|
|
|
|
request = require 'request'
|
|
|
|
log = require 'winston'
|
|
|
|
|
|
|
|
roomChannelMap =
|
|
|
|
main: '#general'
|
|
|
|
artisans: '#artisan'
|
2016-04-13 14:39:17 -04:00
|
|
|
|
|
|
|
module.exports.sendChangedSlackMessage = (options) ->
|
|
|
|
message = "#{options.creator.get('name')} saved a change to #{options.target.get('name')}: #{options.target.get('commitMessage') or '(no commit message)'} #{options.docLink}"
|
2016-06-27 12:49:07 -04:00
|
|
|
@sendSlackMessage message, ['artisans']
|
2016-03-18 20:05:21 -04:00
|
|
|
|
2016-04-13 14:39:17 -04:00
|
|
|
module.exports.sendSlackMessage = (message, rooms=['tower'], options={}) ->
|
2016-04-08 12:24:38 -04:00
|
|
|
unless config.isProduction
|
|
|
|
log.info "Slack msg: #{message}"
|
|
|
|
return
|
2016-03-18 20:05:21 -04:00
|
|
|
unless token = config.slackToken
|
|
|
|
log.info "No Slack token."
|
|
|
|
return
|
|
|
|
for room in rooms
|
|
|
|
channel = roomChannelMap[room] ? room
|
|
|
|
form =
|
|
|
|
channel: channel
|
|
|
|
token: token
|
|
|
|
text: message
|
2016-03-21 17:10:26 -04:00
|
|
|
as_user: true
|
2016-03-22 12:51:18 -04:00
|
|
|
unfurl_links: false
|
|
|
|
unfurl_media: false
|
2016-03-18 20:05:21 -04:00
|
|
|
if options.papertrail
|
|
|
|
secondsFromEpoch = Math.floor(new Date().getTime() / 1000)
|
|
|
|
link = "https://papertrailapp.com/groups/488214/events?time=#{secondsFromEpoch}"
|
|
|
|
form.text += " #{link}"
|
|
|
|
# https://api.slack.com/docs/formatting
|
|
|
|
form.text = form.text.replace('&', '&').replace('<', '<').replace('>', '>')
|
|
|
|
url = "https://slack.com/api/chat.postMessage"
|
|
|
|
request.post {uri: url, form: form}, (err, res, body) ->
|
2016-03-22 12:32:28 -04:00
|
|
|
try
|
|
|
|
response = JSON.parse(body)
|
|
|
|
return log.error('Error sending Slack message:', err) if err
|
|
|
|
return log.error("Slack returned error: #{response.error}") unless response.ok
|
|
|
|
log.warn("Slack returned warning: #{response.warning}") if response.warning
|
|
|
|
# log.info "Got Slack message response:", body
|
|
|
|
catch error
|
|
|
|
log.error("Slack response parse error: #{error}")
|