-
Notifications
You must be signed in to change notification settings - Fork 640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split up messages if longer than 4000 chars #107
Split up messages if longer than 4000 chars #107
Conversation
Cool. Couple small suggestions
|
@evansolomon Sure, I'll make those tweaks. I'll need to slightly rework the logic to hit your second note, because otherwise it'll always split up messages with line breaks, even if they are less than 4000 chars. I'll push an update in a bit. |
Good point re: sub-4k line breaks. In that case, maybe a better approach is just to handle sub-4k messages and return early. Something like... return channel.send msg if msg.length <= 4000
while ... Mostly I just think it's nice to avoid big blocks of indented code when it's reasonable to do it because it makes thing a little harder to read. Edit: I just realized even that part is insider of another loop (so returning will cause problems). So maybe my whole idea about no conditionals is just not worth it here. |
@evansolomon Alright, I've pushed the update. When it comes to the conditional in question, I swapped the order so that the larger block occurs on the else, making it a bit more readable when it comes to the single message sending line. I figured that was the best compromise after exploring the options (since, as you noted, using a return wasn't doable since it was nested inside another loop). |
Thinking about this a little more, I think the sub-4k chunk problem still exists. For example, let's say the message is "Hello [3994 characters...] foo\nbar". I think we'll end up with 3 messages when we only want 2. We should get one message with "Hello [bunch of characters]" and a second message with "foo\nbar". But instead the second message will be split on the last index of a newline, so we'll get "foo" then "bar". We should check to see if the substring'd chunk is <= 4k characters, and if it is skip the whole line break thing. |
@evansolomon Great catch. Fixed in latest push. |
Thanks! |
Split up messages if longer than 4000 chars
@evansolomon Thanks right back! |
Squashed commit of the following: commit 4fad58d9ed107b52f2caf3a6242d0fa59fda5c6f Author: Ben Robbins <[email protected]> Date: Mon Feb 23 14:44:39 2015 -0600 merge fixes commit f7b62edcf39e21d284c8a097e6e9e98c4cb337a0 Author: Ben Robbins <[email protected]> Date: Mon Feb 23 14:29:44 2015 -0600 fix merge commit 286bcdf31ee7ddd297d59cf7c1d4854c13b22581 Merge: a9ddb13 743fa06 Author: Ben Robbins <[email protected]> Date: Mon Feb 23 14:24:29 2015 -0600 Merge branch 'master' of github.com:slackhq/hubot-slack into hubot-slack Conflicts: README.md package.json src/slack.coffee test/slack.coffee commit 743fa06 Author: Evan Solomon <[email protected]> Date: Tue Feb 17 11:06:27 2015 -0800 Make min message length a static property and add a test Fixes slackapi#156 commit 1773263 Author: bouzuya <[email protected]> Date: Tue Feb 17 21:26:41 2015 +0900 ignore empty message commit 7b2bd56 Merge: db92398 dc2ded2 Author: Paul Hammond <[email protected]> Date: Fri Jan 30 16:35:04 2015 -0800 Merge pull request slackapi#145 from charlessuh/real-name Store real name in Hubot brain commit db92398 Merge: c4b1b91 4d4a0b4 Author: Paul Hammond <[email protected]> Date: Fri Jan 30 16:21:55 2015 -0800 Merge pull request slackapi#141 from shokai/error_tostring use JSON.stringify to print Error object commit dc2ded2 Author: Charles Suh <[email protected]> Date: Fri Jan 23 12:51:48 2015 -0800 Store real name in Hubot brain commit 4d4a0b4 Author: Sho Hashimoto <[email protected]> Date: Sun Jan 11 16:51:17 2015 +0900 use JSON.stringify to print Error object commit c4b1b91 Merge: 3c915db 1c49e4d Author: Paul Hammond <[email protected]> Date: Mon Jan 5 18:37:44 2015 -0800 Merge pull request slackapi#133 from kballard/bot-messages Create new Message types for slack messages commit 1c49e4d Author: Kevin Ballard <[email protected]> Date: Mon Dec 22 16:31:40 2014 -0800 Use Hubot.Message directly with Hubot >= 2.11.0 Hubot 2.11.0 now exports Message directly, so we can stop pulling out TextMessage's superclass. We still need the workaround when using older versions of Hubot. commit 3c915db Merge: 4d8bc12 2256e70 Author: Paul Hammond <[email protected]> Date: Mon Dec 22 11:57:32 2014 -0800 Merge pull request slackapi#130 from paulhammond/close-fix Rename close handler (fixes slackapi#126) commit e823160 Author: Kevin Ballard <[email protected]> Date: Fri Dec 19 00:30:14 2014 -0800 Add a bunch more message tests commit 5b86509 Author: Kevin Ballard <[email protected]> Date: Fri Dec 19 00:29:18 2014 -0800 Tweak SlackTextMessage, SlackRawMessage to have .rawMessage Add `rawMessage` property to `SlackTextMessage` that contains the original message. This is in addition to `rawText` which we're keeping. Change `SlackRawMessage` to use the same `rawMessage` property instead of copying all the message properties onto itself. commit f02fd01 Author: Kevin Ballard <[email protected]> Date: Thu Dec 18 18:39:24 2014 -0800 Add a couple of basic tests for SlackTextMessage More tests are coming. commit a8ce172 Author: Kevin Ballard <[email protected]> Date: Thu Dec 18 18:38:28 2014 -0800 Tweak SlackRawMessage to only copy own properties SlackRawMessage copies its properties from the slack-client Message object. It should only be copying own properties, though, or it ends up with functions that Message defines (which it turns out are enumerable). commit 9ad99ee Author: Kevin Ballard <[email protected]> Date: Thu Dec 18 18:37:01 2014 -0800 Expand the test stubs * Add more information to the stubs. * Tweak the stubbed functions to behave slightly more like the real ones (e.g. test channel name / id before returning). * Fix the channel name used in some of the tests to match our stub channel. * Attach a real Hubot Brain to the stub robot. * Call SlackBot.loggedIn before each test, to ensure more of the bot is initialized. commit c522e95 Author: Kevin Ballard <[email protected]> Date: Thu Dec 18 18:36:39 2014 -0800 Fix some miscellaneous typos in the test descriptions commit 04ae085 Author: Kevin Ballard <[email protected]> Date: Thu Dec 18 14:14:23 2014 -0800 Extract test stubs into their own file commit 4f79aec Author: Kevin Ballard <[email protected]> Date: Thu Dec 18 14:02:24 2014 -0800 Convert test stubs to @vars This makes it possible to add more test files later and still get at the stubs. commit 178896a Author: Kevin Ballard <[email protected]> Date: Thu Dec 18 12:49:03 2014 -0800 Add some comments on how to use the new listeners commit 47b4c0b Author: Kevin Ballard <[email protected]> Date: Wed Dec 17 21:37:07 2014 -0800 Create new Message types for slack messages Regular text messages now use a TextMessage subclass called SlackTextMessage, which includes both the parsed text (as `text`) and the unparsed text (as `rawText`). Other messages that used to be ignored are now received as either SlackRawMessage or SlackBotMessage. These do not descend from TextMessage so they won't be seen by normal `hear` or `respond` listeners. A few new Listener classes were created, SlackRawListener and SlackBotListener, to make it slightly easier to write code tailored for these new message types. commit 4d8bc12 Merge: 64d7c57 ea562ae Author: Paul Hammond <[email protected]> Date: Wed Dec 17 22:36:50 2014 -0800 Merge pull request slackapi#131 from slackhq/better-formatting-removal Better formatting code commit ea562ae Author: Paul Hammond <[email protected]> Date: Wed Dec 17 19:05:39 2014 -0800 Correctly decode entities from messages commit a01dbc0 Author: Paul Hammond <[email protected]> Date: Wed Dec 17 19:01:24 2014 -0800 Much better link formatting based on feedback from @kballard commit 2256e70 Author: Paul Hammond <[email protected]> Date: Wed Dec 17 18:45:28 2014 -0800 Rename close handler (fixes slackapi#126) commit 64d7c57 Merge: f3a288b d9c8d1c Author: Paul Hammond <[email protected]> Date: Wed Dec 17 11:57:51 2014 -0800 Merge pull request slackapi#125 from paulhammond/mailto-fix Strip mailto: from mailto: links commit d9c8d1c Author: Paul Hammond <[email protected]> Date: Wed Dec 17 09:25:57 2014 -0800 Strip mailto: from mailto: links commit f3a288b Author: Myles Grant <[email protected]> Date: Tue Dec 16 21:34:02 2014 -0800 release 3.2.1 commit ea8c35b Merge: d8c2342 c9f6870 Author: Paul Hammond <[email protected]> Date: Tue Dec 16 21:30:55 2014 -0800 Merge pull request slackapi#124 from paulhammond/issue-123-fix Handle existing hubot brains with incomplete user data commit c9f6870 Author: Paul Hammond <[email protected]> Date: Tue Dec 16 21:21:10 2014 -0800 Handle existing hubot brains with incomplete user data commit d8c2342 Author: Myles Grant <[email protected]> Date: Tue Dec 16 14:16:28 2014 -0800 release 3.2.0 commit 7284bba Author: Paul Hammond <[email protected]> Date: Tue Dec 16 14:09:22 2014 -0800 Document HUBOT_AUTH_ADMIN (fixes slackapi#95) commit 08ac424 Merge: 86163dc 426f16d Author: Paul Hammond <[email protected]> Date: Tue Dec 16 14:07:51 2014 -0800 Merge pull request slackapi#122 from paulhammond/remove-formatting More formatting fixes commit 426f16d Author: Paul Hammond <[email protected]> Date: Tue Dec 16 13:59:46 2014 -0800 Better comments on link regex commit c1fec0c Author: Paul Hammond <[email protected]> Date: Tue Dec 16 13:50:51 2014 -0800 Remove formatting around non-slack links commit 274dc28 Author: Paul Hammond <[email protected]> Date: Tue Dec 16 13:25:34 2014 -0800 Remove all special slack formatting, not just <@U1234> commit 86163dc Merge: 29fd355 7d7aa62 Author: Evan Solomon <[email protected]> Date: Tue Dec 16 13:14:07 2014 -0800 Merge pull request slackapi#121 from bnied/master terminate Hubot when Slack disconnects commit 29fd355 Merge: ff7685e 703533d Author: Paul Hammond <[email protected]> Date: Tue Dec 16 13:03:41 2014 -0800 Merge pull request slackapi#120 from paulhammond/reply-fix Fix @user mentions commit 703533d Author: Paul Hammond <[email protected]> Date: Tue Dec 16 08:46:43 2014 -0800 Get users from client when removing message formatting commit 90205dc Author: Paul Hammond <[email protected]> Date: Mon Dec 15 19:36:34 2014 -0800 Remove <@U1234> formatting before passing message onto hubot commit ff7685e Merge: aadb994 db20579 Author: Paul Hammond <[email protected]> Date: Mon Dec 15 16:08:59 2014 -0800 Merge pull request slackapi#113 from paulhammond/user-changes User changes (fixes slackapi#56, slackapi#94, slackapi#118) commit aadb994 Author: Paul Hammond <[email protected]> Date: Mon Dec 15 15:59:52 2014 -0800 Better documentation on channel management commit db20579 Author: Paul Hammond <[email protected]> Date: Mon Dec 15 15:53:38 2014 -0800 Merge users with hubot brain instead of overwriting commit 7d7aa62 Author: Benjamin Nied <[email protected]> Date: Sun Dec 14 15:26:15 2014 -0500 terminate Hubot when Slack disconnects us commit 4693ccd Author: Paul Hammond <[email protected]> Date: Wed Dec 10 21:53:50 2014 -0800 Remove outdated attachments docs (this will be coming back, see slackapi#108 for details) commit d9fc654 Author: Paul Hammond <[email protected]> Date: Tue Dec 9 23:08:14 2014 -0800 Better fetching & checking of message users commit df9519c Author: Paul Hammond <[email protected]> Date: Tue Dec 9 22:36:16 2014 -0800 Store all users in hubot brain on login, keep them updated as they change commit cf2db2f Merge: 940fad0 5338f91 Author: Evan Solomon <[email protected]> Date: Wed Dec 10 00:32:29 2014 -0800 Merge pull request slackapi#112 from slackhq/die-on-error Fail harder on errors commit 5338f91 Author: Evan Solomon <[email protected]> Date: Wed Dec 10 00:29:40 2014 -0800 Wait 1 second to exit to give in flight requests some chance to succeed commit 940fad0 Author: Evan Solomon <[email protected]> Date: Tue Dec 9 22:28:28 2014 -0800 This trailing space was really annoying me commit 107752f Author: Evan Solomon <[email protected]> Date: Tue Dec 9 22:19:28 2014 -0800 Fail harder on errors Currently if the client emits an error we log it but just hang around. The app is in an undefined state at that point so it's hard to say what exactly will happen, but it's probably not ideal. At least this way a process monitor can tell that it died and restart it, ship the logs somewhere, or something. commit 12c1f5e Merge: c57f7ec 3ef146f Author: Evan Solomon <[email protected]> Date: Tue Dec 9 22:16:50 2014 -0800 Merge pull request slackapi#111 from slackhq/long-messages Improve long message splitting and add tests commit 3ef146f Author: Evan Solomon <[email protected]> Date: Tue Dec 9 22:11:04 2014 -0800 Try to split long messages on word breaks if there are no line breaks commit 6fec098 Author: Evan Solomon <[email protected]> Date: Tue Dec 9 21:47:18 2014 -0800 Add tests for long message splitting commit c57f7ec Merge: ae6f0ad c6982ee Author: Evan Solomon <[email protected]> Date: Tue Dec 9 21:45:04 2014 -0800 Merge pull request slackapi#107 from redhotvengeance/split-long-messages Split up messages if longer than 4000 chars commit ae6f0ad Merge: f6a7c71 40de33f Author: Paul Hammond <[email protected]> Date: Tue Dec 9 21:42:32 2014 -0800 Merge pull request slackapi#110 from paulhammond/users Better handling of users (fixes slackapi#93) commit 40de33f Author: Paul Hammond <[email protected]> Date: Tue Dec 9 21:31:10 2014 -0800 Better handling of users (fixes slackapi#93) commit c6982ee Author: Ian Lollar <[email protected]> Date: Tue Dec 9 19:37:18 2014 -0800 Avoid splitting messages w/ new lines <= 4000 char commit 218012d Author: Ian Lollar <[email protected]> Date: Tue Dec 9 17:57:37 2014 -0800 Refactor and reorganize message splitting commit 5f8ba72 Merge: 6d7b44f f6a7c71 Author: Ian Lollar <[email protected]> Date: Tue Dec 9 16:13:22 2014 -0800 Merge branch 'master' into split-long-messages commit 6d7b44f Author: Ian Lollar <[email protected]> Date: Tue Dec 9 15:53:56 2014 -0800 Split up messages if longer than 4000 chars commit f6a7c71 Author: Paul Hammond <[email protected]> Date: Tue Dec 9 15:28:53 2014 -0800 Add hubot to dev dependencies so tests pass on travis commit 6afbc1b Author: Myles Grant <[email protected]> Date: Tue Dec 9 07:17:58 2014 -0800 release 3.1.0 commit 9ff48a5 Merge: dd265c8 cf62a2a Author: Myles Grant <[email protected]> Date: Tue Dec 9 07:16:39 2014 -0800 Merge branch 'master' of github.com:slackhq/hubot-slack commit cf62a2a Merge: 1b79c65 3320c36 Author: Myles Grant <[email protected]> Date: Tue Dec 9 07:15:21 2014 -0800 Merge pull request slackapi#103 from danielsmink/patch-1 Update package.json commit 3320c36 Author: Daniël Smink <[email protected]> Date: Tue Dec 9 07:12:27 2014 +0100 Update package.json Update version number. commit 1b79c65 Merge: 15c84de 2c916ce Author: Evan Solomon <[email protected]> Date: Mon Dec 8 14:35:47 2014 -0800 Merge pull request slackapi#101 from wingrunr21/fix_topic_signature Fix topic function signature commit 15c84de Merge: b0d49f0 0ae3f90 Author: Paul Hammond <[email protected]> Date: Mon Dec 8 11:53:03 2014 -0800 Merge pull request slackapi#100 from technicalpickles/patch-1 Update Upgrade instructions to use npm from npm instead of from git commit 0ae3f90 Author: Josh Nichols <[email protected]> Date: Mon Dec 8 14:45:05 2014 -0500 Update Upgrade instructions to use npm from npm instead of from git commit 2c916ce Author: Stafford Brunk <[email protected]> Date: Mon Dec 8 14:40:39 2014 -0500 Fix topic function signature commit dd265c8 Author: Myles Grant <[email protected]> Date: Mon Dec 8 10:12:34 2014 -0800 release 3.0.0 commit b0d49f0 Author: Paul Hammond <[email protected]> Date: Mon Dec 8 09:06:54 2014 -0800 Update URLs to github.com/slackhq commit 79acbcf Author: Paul Hammond <[email protected]> Date: Sat Dec 6 16:48:05 2014 -0800 Begin to update tests commit 6a9e10e Author: Paul Hammond <[email protected]> Date: Sat Dec 6 16:00:31 2014 -0800 Update README to reflect new yeoman based hubot install system commit 0e2eaf7 Author: Paul Hammond <[email protected]> Date: Thu Oct 23 11:15:39 2014 -0700 Handle Topic changes correctly commit d52c86b Author: Paul Hammond <[email protected]> Date: Thu Oct 23 10:53:36 2014 -0700 Don't crash when recieving file_comment messages commit e55a305 Author: Paul Hammond <[email protected]> Date: Wed Oct 22 11:36:11 2014 -0700 Don't log the secret token commit 8950b23 Author: Paul Hammond <[email protected]> Date: Wed Oct 22 11:35:49 2014 -0700 Fail gracefully if someone provides a old integration token commit 9a7f5ba Author: Paul Hammond <[email protected]> Date: Wed Oct 22 11:23:58 2014 -0700 Update README with upgrade instructions commit 0a0c0d3 Author: Paul Hammond <[email protected]> Date: Wed Oct 22 10:22:08 2014 -0700 Don't crash when recieving a bot_message commit ef1e2c7 Author: Paul Hammond <[email protected]> Date: Tue Oct 21 19:37:13 2014 -0700 Update README commit c599dde Author: Paul Hammond <[email protected]> Date: Tue Oct 21 19:29:27 2014 -0700 Update Copyright statements to reflect our new legal name commit 76a00f6 Author: Paul Hammond <[email protected]> Date: Mon Sep 22 09:36:26 2014 -0700 Remove dev path hacks commit f2a62bf Author: Paul Hammond <[email protected]> Date: Mon Sep 22 09:36:00 2014 -0700 slack-client is a runtime dependency commit 6e425ad Author: Myles Grant <[email protected]> Date: Fri Sep 19 22:08:30 2014 -0700 We want the most recent version of node-slack commit 9386ffa Author: Myles Grant <[email protected]> Date: Thu Aug 28 16:21:55 2014 -0700 Pretend that DM messages were really addressed to us commit 101f526 Author: Myles Grant <[email protected]> Date: Thu Aug 28 14:17:50 2014 -0700 Add some TODOs commit 24eaa9f Author: Myles Grant <[email protected]> Date: Wed Jul 30 10:02:27 2014 -0700 Pedantry commit 09aa88c Author: Myles Grant <[email protected]> Date: Sat May 10 16:54:19 2014 -0700 Whoops, process slack user into hubot user before running through the enter/leave/topic functions commit 3e237a5 Author: Myles Grant <[email protected]> Date: Sat May 10 13:52:47 2014 -0700 Fix support for channel/group join/leave, and add support for channel/group topic notifications commit 02b45b8 Author: Myles Grant <[email protected]> Date: Sat May 10 13:39:20 2014 -0700 Support for channel/group join/leave notifications commit 7095a76 Author: Myles Grant <[email protected]> Date: Fri May 9 22:53:44 2014 -0700 Fix typo references to the client commit f7cec4e Author: Myles Grant <[email protected]> Date: Fri May 9 22:52:23 2014 -0700 Some cleanup, and a hacky hubot path to get around stupid local npm issues commit d95097a Author: Myles Grant <[email protected]> Date: Mon May 5 10:52:44 2014 -0700 Clean commit 326b3f0 Author: Myles Grant <[email protected]> Date: Sun May 4 20:32:46 2014 -0700 Sending messages, setting topics commit c14b4d1 Author: Myles Grant <[email protected]> Date: Sun May 4 19:31:51 2014 -0700 Dumping almost everything and starting over. Have the basics of connecting and receiving messages working commit f181a19 Author: Myles Grant <[email protected]> Date: Sun Apr 13 10:26:49 2014 -0700 Starting to move things around for the new client adapter
This PR addresses and fixes #104.
Presently, if a message longer than 4000 chars / 16k bytes in size is sent, the RTM API will reject the message and close the connection to Hubot. This is easily reproducible by calling
hubot help
once a sizable amount of scripts are added to the bot. Hubot will still be alive, but it stays disconnected from Slack and requires a restart before we can start talking to our robotic friend again.This PR checks to see if a message is longer than 4000 chars before it is sent, and if it is longer than 4000 chars, it will split the message into chunks and send each one at a time. It will also attempt to split the message at the last occurring line break (within the 4000 char limit) to try to result in cleaner message chunks.