Created
December 13, 2014 13:29
-
-
Save mako09/6943959b08c3b26b001c to your computer and use it in GitHub Desktop.
patch for hubot-slack 3.1.0
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
--- slack.coffee.orig 2014-12-13 17:53:24.290393342 +0900 | |
+++ slack.coffee 2014-12-13 19:42:08.788464223 +0900 | |
@@ -8,6 +8,7 @@ | |
constructor: (robot) -> | |
@robot = robot | |
+ @botUserID = null | |
run: -> | |
# Take our options from the environment, and set otherwise suitable defaults | |
@@ -44,6 +45,13 @@ | |
loggedIn: (self, team) => | |
@robot.logger.info "Logged in as #{self.name} of #{team.name}, but not yet connected" | |
+ # Go through the list of known users and find the name that matches ours | |
+ for id, user of @client.users | |
+ if user.is_bot and user.name == self.name | |
+ @botUserID = id | |
+ break | |
+ @robot.logger.info "Bot's Slack user ID is #{@botUserID}" | |
+ | |
# Provide our name to Hubot | |
@robot.name = self.name | |
@@ -103,6 +111,11 @@ | |
# If this is a DM, pretend it was addressed to us | |
if msg.getChannelType() == 'DM' | |
txt = "#{@robot.name} #{txt}" | |
+ # Or, if we were @-mentioned | |
+ else if matches = txt.match(/<@([^>]+)>[:,]?\s?(.*)/) | |
+ [userID, someText] = matches[1..2] | |
+ if @botUserID and (userID == @botUserID) | |
+ txt = "#{@robot.name} #{someText}" | |
@receive new TextMessage user, txt, msg.ts | |
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
--- slack.coffee.orig 2014-12-13 17:53:24.290393342 +0900 | |
+++ slack.coffee 2014-12-13 17:53:51.421673532 +0900 | |
@@ -61,6 +61,24 @@ | |
@client.removeListener 'close', @.close | |
@client.removeListener 'message', @.message | |
+ ################################################################### | |
+ # HTML helpers. | |
+ ################################################################### | |
+ unescapeHtml: (string) -> | |
+ try | |
+ string | |
+ # Unescape entities | |
+ .replace(/&/g, '&') | |
+ .replace(/</g, '<') | |
+ .replace(/>/g, '>') | |
+ | |
+ # Convert markup into plain url string. | |
+ .replace(/<((\bhttps?)[^|]+)(\|(.*))+>/g, '$1') | |
+ .replace(/<((\bhttps?)(.*))?>/g, '$1') | |
+ catch e | |
+ @robot.logger.error "Failed to unescape HTML: #{e}" | |
+ return '' | |
+ | |
message: (msg) => | |
return if msg.hidden | |
return if not msg.text and not msg.attachments | |
@@ -96,7 +114,7 @@ | |
else | |
# Build message text to respond to, including all attachments | |
- txt = msg.getBody() | |
+ txt = @unescapeHtml msg.getBody() | |
@robot.logger.debug "Received message: '#{txt}' in channel: #{channel.name}, from: #{user.name}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment