From ad634a5b19ba454375c3b491a2048e2c7a45a637 Mon Sep 17 00:00:00 2001 From: Misty De Meo Date: Fri, 24 Mar 2017 16:38:59 +1100 Subject: [PATCH 1/3] Return rawText and rawMessage objects Prior to 900402ea254b0fdf5a819fd3073325c68f1692a9, hubot-slack returned a message object which included the raw text from the Slack API along with the raw Slack Message object. This was useful for middleware and scripts which reformatted Slack text in different ways than hubot-slack's own default formatter. For example, a middleware might try to detect URLs that were autoexpanded by the Slack API and restore them to their original format; this is only possible if the raw text is included, so the unparsed URLs can be read. --- src/bot.coffee | 5 +++-- src/client.coffee | 1 + src/slack-message.coffee | 13 +++++++++++++ test/bot.coffee | 17 ++++++++++++++++- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/slack-message.coffee diff --git a/src/bot.coffee b/src/bot.coffee index 10a67f5a..3f707f98 100644 --- a/src/bot.coffee +++ b/src/bot.coffee @@ -2,6 +2,7 @@ SlackClient = require './client' ReactionMessage = require './reaction-message' +SlackTextMessage = require './slack-message' # Public: Adds a Listener for ReactionMessages with the provided matcher, # options, and callback @@ -148,7 +149,7 @@ class SlackBot extends Adapter Message received from Slack ### message: (message) => - {text, user, channel, subtype, topic, bot} = message + {text, rawText, user, channel, subtype, topic, bot} = message return if user && (user.id == @self.id) # Ignore anything we sent, or anything from an unknown user return if bot && (bot.id == @self.bot_id) # Ignore anything we sent, or anything from an unknown bot @@ -173,7 +174,7 @@ class SlackBot extends Adapter when 'message', 'bot_message' @robot.logger.debug "Received message: '#{text}' in channel: #{channel.name}, from: #{user.name}" - textMessage = new TextMessage(user, text, message.ts) + textMessage = new SlackTextMessage(user, text, rawText, message) textMessage.thread_ts = message.thread_ts @receive textMessage diff --git a/src/client.coffee b/src/client.coffee index 0872ed8f..65d5b8e7 100644 --- a/src/client.coffee +++ b/src/client.coffee @@ -43,6 +43,7 @@ class SlackClient @rtm.on name, (message) => {user, channel, bot_id} = message + message.rawText = message.text message.text = @format.incoming(message) message.user = @rtm.dataStore.getUserById(user) if user message.bot = @rtm.dataStore.getBotById(bot_id) if bot_id diff --git a/src/slack-message.coffee b/src/slack-message.coffee new file mode 100644 index 00000000..e05f1fad --- /dev/null +++ b/src/slack-message.coffee @@ -0,0 +1,13 @@ +{TextMessage} = require.main.require 'hubot' + +class SlackTextMessage extends TextMessage + # Represents a TextMessage created from the Slack adapter + # + # user - The User object + # text - The parsed message text + # rawText - The unparsed message text + # rawMessage - The Slack Message object + constructor: (@user, @text, @rawText, @rawMessage) -> + super @user, @text, @rawMessage.ts + +module.exports = SlackTextMessage diff --git a/test/bot.coffee b/test/bot.coffee index 3d1807a9..c2dfca3c 100644 --- a/test/bot.coffee +++ b/test/bot.coffee @@ -2,6 +2,7 @@ should = require 'should' {Adapter, TextMessage, EnterMessage, LeaveMessage, TopicMessage, Message, CatchAllMessage, Robot, Listener} = require.main.require 'hubot' ReactionMessage = require '../src/reaction-message' SlackClient = require '../src/client' +SlackTextMessage = require '../src/slack-message' describe 'Adapter', -> it 'Should initialize with a robot', -> @@ -123,6 +124,20 @@ describe 'Handling incoming messages', -> @slackbot.message {text: 'foo', user: @stubs.user, channel: @stubs.DM} @stubs._received.text.should.equal "#{@slackbot.robot.name} foo" + it 'Should return a message object with raw text and message', -> + messageData = { + subtype: 'message', + user: @stubs.user, + channel: @stubs.channel, + text: 'foo http://www.example.com bar', + rawText: 'foo bar' + } + @slackbot.message messageData + should.equal (@stubs._received instanceof SlackTextMessage), true + should.equal @stubs._received.text, "foo http://www.example.com bar" + should.equal @stubs._received.rawText, "foo bar" + should.equal @stubs._received.rawMessage, messageData + it 'Should handle channel_join events as envisioned', -> @slackbot.message {subtype: 'channel_join', user: @stubs.user, channel: @stubs.channel} should.equal (@stubs._received instanceof EnterMessage), true @@ -189,7 +204,7 @@ describe 'Handling incoming messages', -> it 'Should not crash with bot messages', -> @slackbot.message { subtype: 'bot_message', bot: @stubs.bot, channel: @stubs.channel, text: 'Pushing is the answer' } - should.equal (@stubs._received instanceof TextMessage), true + should.equal (@stubs._received instanceof SlackTextMessage), true it 'Should ignore messages it sent itself', -> @slackbot.message { subtype: 'bot_message', user: @stubs.self, channel: @stubs.channel, text: 'Ignore me' } From d34fc7dd75486d13624709b600f1e8b7a05a4199 Mon Sep 17 00:00:00 2001 From: Misty De Meo Date: Mon, 3 Jul 2017 16:15:58 -0700 Subject: [PATCH 2/3] SlackClient: allow returning raw text to be disabled --- src/bot.coffee | 7 +++++-- src/client.coffee | 3 +++ test/bot.coffee | 5 +++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/bot.coffee b/src/bot.coffee index 3f707f98..fc2885ae 100644 --- a/src/bot.coffee +++ b/src/bot.coffee @@ -149,7 +149,7 @@ class SlackBot extends Adapter Message received from Slack ### message: (message) => - {text, rawText, user, channel, subtype, topic, bot} = message + {text, rawText, returnRawText, user, channel, subtype, topic, bot} = message return if user && (user.id == @self.id) # Ignore anything we sent, or anything from an unknown user return if bot && (bot.id == @self.bot_id) # Ignore anything we sent, or anything from an unknown bot @@ -174,7 +174,10 @@ class SlackBot extends Adapter when 'message', 'bot_message' @robot.logger.debug "Received message: '#{text}' in channel: #{channel.name}, from: #{user.name}" - textMessage = new SlackTextMessage(user, text, rawText, message) + if returnRawText + textMessage = new SlackTextMessage(user, text, rawText, message) + else + textMessage = new TextMessage(user, text, message.ts) textMessage.thread_ts = message.thread_ts @receive textMessage diff --git a/src/client.coffee b/src/client.coffee index 65d5b8e7..729354fc 100644 --- a/src/client.coffee +++ b/src/client.coffee @@ -25,6 +25,8 @@ class SlackClient # Track listeners for easy clean-up @listeners = [] + @returnRawText = !options.noRawText + ### Open connection to the Slack RTM API ### @@ -44,6 +46,7 @@ class SlackClient {user, channel, bot_id} = message message.rawText = message.text + message.returnRawText = @returnRawText message.text = @format.incoming(message) message.user = @rtm.dataStore.getUserById(user) if user message.bot = @rtm.dataStore.getBotById(bot_id) if bot_id diff --git a/test/bot.coffee b/test/bot.coffee index c2dfca3c..cf54e719 100644 --- a/test/bot.coffee +++ b/test/bot.coffee @@ -130,7 +130,8 @@ describe 'Handling incoming messages', -> user: @stubs.user, channel: @stubs.channel, text: 'foo http://www.example.com bar', - rawText: 'foo bar' + rawText: 'foo bar', + returnRawText: true } @slackbot.message messageData should.equal (@stubs._received instanceof SlackTextMessage), true @@ -203,7 +204,7 @@ describe 'Handling incoming messages', -> should.equal (@stubs._received instanceof CatchAllMessage), true it 'Should not crash with bot messages', -> - @slackbot.message { subtype: 'bot_message', bot: @stubs.bot, channel: @stubs.channel, text: 'Pushing is the answer' } + @slackbot.message { subtype: 'bot_message', bot: @stubs.bot, channel: @stubs.channel, text: 'Pushing is the answer', returnRawText: true } should.equal (@stubs._received instanceof SlackTextMessage), true it 'Should ignore messages it sent itself', -> From 921f9a1a12786126fdfbae3c8b82a649e19016fc Mon Sep 17 00:00:00 2001 From: TonioOoOo Date: Wed, 12 Jul 2017 18:42:28 +0200 Subject: [PATCH 3/3] Fix documentation sample --- docs/_pages/basic_usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_pages/basic_usage.md b/docs/_pages/basic_usage.md index f67f981f..90d285b6 100644 --- a/docs/_pages/basic_usage.md +++ b/docs/_pages/basic_usage.md @@ -105,6 +105,6 @@ module.exports = (robot) -> # There are better ways to post messages of course # Notice the _required_ arguments `channel` and `text`, and the _optional_ arguments `as_user`, and `unfurl_links` - robot.adapter.client.chat.postMessage(res.user.room, "This is a message!", {as_user: true, unfurl_links: false}) + robot.adapter.client.web.chat.postMessage(res.message.room, "This is a message!", {as_user: true, unfurl_links: false}) ```