Skip to content

Commit

Permalink
Merge pull request #413 from github/restore_rawText
Browse files Browse the repository at this point in the history
Return rawText and rawMessage objects
  • Loading branch information
aoberoi authored Aug 1, 2017
2 parents 6ef2ac4 + d34fc7d commit 7a2fca3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/bot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -148,7 +149,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
Expand All @@ -173,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 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

Expand Down
4 changes: 4 additions & 0 deletions src/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SlackClient
# Track listeners for easy clean-up
@listeners = []

@returnRawText = !options.noRawText

###
Open connection to the Slack RTM API
###
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/slack-message.coffee
Original file line number Diff line number Diff line change
@@ -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
20 changes: 18 additions & 2 deletions test/bot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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', ->
Expand Down Expand Up @@ -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 <http://www.example.com> 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 <http://www.example.com> 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
Expand Down Expand Up @@ -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' }
Expand Down

0 comments on commit 7a2fca3

Please sign in to comment.