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}) ``` diff --git a/src/bot.coffee b/src/bot.coffee index 448407fc..24acebc2 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 @@ -156,7 +157,7 @@ class SlackBot extends Adapter Message received from Slack ### message: (message) => - {text, 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 @@ -181,7 +182,10 @@ 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) + 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 0872ed8f..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 ### @@ -43,6 +45,8 @@ class SlackClient @rtm.on name, (message) => {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/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..cf54e719 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,21 @@ 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', + returnRawText: true + } + @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 @@ -188,8 +204,8 @@ 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' } - should.equal (@stubs._received instanceof TextMessage), true + @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', -> @slackbot.message { subtype: 'bot_message', user: @stubs.self, channel: @stubs.channel, text: 'Ignore me' }