Skip to content

Instantly share code, notes, and snippets.

@mako09

mako09/atmark.patch

Created Dec 13, 2014
Embed
What would you like to do?
patch for hubot-slack 3.1.0
--- 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
--- 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(/&amp;/g, '&')
+ .replace(/&lt;/g, '<')
+ .replace(/&gt;/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