Skip to content

Commit

Permalink
Split up messages if longer than 4000 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
redhotvengeance committed Dec 9, 2014
1 parent 6afbc1b commit 6d7b44f
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,26 @@ class SlackBot extends Adapter
for msg in messages
@robot.logger.debug "Sending to #{envelope.room}: #{msg}"

channel.send msg
# If message is greater than 4000 chars, split it into multiple msessages
if msg.length > 4000
submessages = []

while msg.length > 0
# Split message at last line break, if it exists
chunk = msg.substring(0, 4000)
breakIndex = if chunk.lastIndexOf('\n') isnt -1 then chunk.lastIndexOf('\n') else 4000

submessages.push msg.substring(0, breakIndex)

# Skip char if split on line break
breakIndex++ if breakIndex isnt 4000

msg = msg.substring(breakIndex, msg.length)

channel.send m for m in submessages

else
channel.send msg

reply: (envelope, messages...) ->
@robot.logger.debug "Sending reply"
Expand Down

0 comments on commit 6d7b44f

Please sign in to comment.