Skip to content

Commit

Permalink
Merge pull request #13415 from code-dot-org/fixSlack
Browse files Browse the repository at this point in the history
Move slackify from ChatClient to Slack.
  • Loading branch information
ashercodeorg committed Feb 23, 2017
2 parents ba3b8b1 + 9504a3d commit aa6e7c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
18 changes: 3 additions & 15 deletions lib/cdo/chat_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,11 @@ def self.message(room, message, options={})
post_to_hipchat(room, message, options)

channel = "\##{Slack::CHANNEL_MAP[room] || room}"
Slack.message slackify(message.to_s), channel: channel, username: @@name, color: options[:color]
end

def self.slackify(message)
# format with slack markdownish formatting instead of html
# https://slack.zendesk.com/hc/en-us/articles/202288908-Formatting-your-messages
message.strip!
message = "```#{message[7..-1]}```" if message =~ /^\/quote /
message.
gsub(/<\/?i>/, '_').
gsub(/<\/?b>/, '*').
gsub(/<\/?pre>/, '```').
gsub(/<a href=['"]([^'"]+)['"]>/, '<\1|').
gsub(/<\/a>/, '>').
gsub(/<br\/?>/, "\n")
Slack.message message.to_s, channel: channel, username: @@name, color: options[:color]
end

# TODO(asher): Deprecate this method. There appears to be a test dependency
# on this CDO.log.info output happening.
def self.post_to_hipchat(room, message, options={})
unless CDO.hip_chat_logging
# Output to standard log if HipChat isn't configured
Expand Down
22 changes: 19 additions & 3 deletions lib/cdo/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,24 @@ def self.update_topic(channel_name, new_topic, use_channel_map = false)
end

# @param text [String] The text to post in Slack.
# @param params [Hash] A hash of parameters to alter how the text is posted.
# @return [Boolean] Whether the text was posted to Slack successfully.
def self.message(text, params={})
return false unless CDO.slack_endpoint
slackified_text = slackify text

if params[:color]
payload = {
attachments: [{
fallback: text,
text: text,
fallback: slackified_text,
text: slackified_text,
mrkdwn_in: [:text],
color: COLOR_MAP[params[:color].to_sym] || params[:color]
}]
}.merge params
else
payload = {
text: text,
text: slackified_text,
unfurl_links: true
}.merge params
end
Expand Down Expand Up @@ -123,4 +125,18 @@ def self.message(text, params={})
end
nil
end

# Format with slack markdownish formatting instead of HTML.
# https://slack.zendesk.com/hc/en-us/articles/202288908-Formatting-your-messages
private_class_method def self.slackify(message)
message.strip!
message = "```#{message[7..-1]}```" if message =~ /^\/quote /
message.
gsub(/<\/?i>/, '_').
gsub(/<\/?b>/, '*').
gsub(/<\/?pre>/, '```').
gsub(/<a href=['"]([^'"]+)['"]>/, '<\1|').
gsub(/<\/a>/, '>').
gsub(/<br\/?>/, "\n")
end
end

0 comments on commit aa6e7c5

Please sign in to comment.