Skip to content

Commit

Permalink
Added null checking to prevent bot from crashing if user name, text o…
Browse files Browse the repository at this point in the history
…r channel cannot be determined.
  • Loading branch information
James Elliott committed Feb 24, 2015
1 parent 8386d51 commit e223ead
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions examples/simple_reverse.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,27 @@ slack.on 'message', (message) ->
channelName = if channel.is_channel then '#' else ''
channelName += channel.name

userName = if user?.name? then "@#{user.name}" else "UNKNOWN_USER"

console.log """
Received: #{type} #{channelName} @#{user.name} #{ts} "#{text}"
Received: #{type} #{channelName} #{userName} #{ts} "#{text}"
"""

# Respond to messages with the reverse of the text received.

if type is 'message'
if type is 'message' and text? and channel?
response = text.split('').reverse().join('')
channel.send response
console.log """
@#{slack.self.name} responded with "#{response}"
"""
else
textError = if not text? then 'text was undefined.' else ''
channelError = if not channel? then 'channel was undefined.' else ''

console.log """
@#{slack.self.name} could not respond. #{textError} #{channelError}
"""


slack.on 'error', (error) ->
Expand Down

0 comments on commit e223ead

Please sign in to comment.