Wednesday, 12 April 2017

Javascript - Slack Chat Exceeding Message Size Limit In Bytes

I have a bot integration with Slack Chat running on a Node.js server and am trying to trim my messages down to make sure they don't exceed Slack's size limit of each message of 4,000 bytes (https://api.slack.com/docs/message-formatting#message_formatting). The issue is that some of my messages I count at ~3,800 bytes are still getting broken into multiple messages. Below is an example of a message that is getting broken into multiple messages. I've also included the function I'm using to trim my messages by byte count.

JSON (3,900 bytes when saved to disk):

{
    "text": "The last customer message for case #16 is:``````---------- Forwarded message ----------\nFrom: Jon Fake \nDate: Apr 10, 2017, 8:28 PM -0500\nTo: cases.TSTDRV1515115_1.32102a1111@cases.na2.netsuite.com\nSubject: Fwd: [Slack] aaaaaaaaa updates for the week of April 2nd, 2017\r\n  Jon Fake | ERP Consultant\n  [aaaaaaaa] [Twitter] [Facebook] [YouTube] [LinkedIn]\n  1111 American Blvd W, aaaaaaaaaaa, MN 11111\n  Office: 111.111.111 Mobile: 111.111.111\n  linkedin.com/jonfake\r\n  ---------- Forwarded message ----------\n  From: Slack \n  Date: Apr 10, 2017, 10:01 AM -0500\n  To: jon.fake@aaaaaaaaa.com\n  Subject: [Slack] aaaaaaaaa updates for the week of April 2nd, 2017\r\n    This message originated from outside your organization\n    --------------------------------------------------------------------\r\n    aaaaaaaaa’s Weekly Summary\r\n    Sunday, April 2nd – Saturday, April 8th\r\n    Hope you had a good weekend! Here's a summary of what happened on your team last week:\r\n    Your team sent a total of 460 messages last week (that's 43 more than the week before). Of those, 8% were in public channels and 92% were direct messages. Your team also uploaded 11 files (that's 2 more than the week before).\r\n    Looking for more stats? Check out your team's stats page.\r\n    The busiest hour last week was 1-2am on Sunday, when your team sent 179 messages. Holy speedtyping!\n    Your team has 1 owner: Jon Fake. In total there are 9 people on your team (that's not including 8 disabled accounts).\r\n    Remember: it's important to keep the list of owners and admins up to date since they control your team's settings.\r\n    Your team is on Slack's Free plan, which is free to use for as long as you want for teams of all sizes. Interested in unlimited archive access and integrations, single sign-on, custom data retention, and more? Check out our paid plans to learn all about it.\r\n    This email is sent to Team Owners and Admins of active teams. If you'd prefer not to receive these emails, you can unsubscribe here.\r\n    Made by Slack Technologies, Inc  •  Our Blog\n    155 5th Street, 6th Floor  •  San Francisco, CA  •  94103 \r\n   \r\n---------- Forwarded message ----------\nFrom: Jon Fake \nDate: Apr 10, 2017, 8:28 PM -0500\nTo: cases.TSTDRV1515115_1.32102a1111@cases.na2.netsuite.com\nSubject: Fwd: [Slack] aaaaaaaaa updates for the week of April 2nd, 2017\r\n  Jon Fake | ERP Consultant\n  [aaaaaaaa] [Twitter] [Facebook] [YouTube] [LinkedIn]\n  3800 American Blvd W, Bloomington, MN 55341\n  Office: 111.111.111 Mobile: 111.111.111\n  linkedin.com/jonfake\r\n  ---------- Forwarded message ----------\n  From: Slack \n  Date: Apr 10, 2017, 10:01 AM -0500\n  To: jon.fake@aaaaaaaaa.com\n  Subject: [Slack] aaaaaaaaa updates for the week of April 2nd, 2017\r\n    This message originated from outside your organization\n    --------------------------------------------------------------------\r\n    aaaaaaaaa’s Weekly Summary\r\n    Sunday, April 2nd – Saturday, April 8th\r\n    Hope you had a good weekend! Here's a summary of what happened on your team last week:\r\n    Your team sent a total of 460 messages last week (that's 43 more than the week before). Of those, 8% were in public channels and 92% were direct messages. Your team also uploaded 11 files (that's 2 more than the week before).\r\n    Looking for more stats? Check out your team's stats page.\r\n    The busiest hour last week was 1-2am on Sunday, when your team sent 179 messages. Holy speedtyping!\r\n    Your team has 1 owner: Jon Fake. In total there are 9 people on your team (that's not including 8 disabled accounts).\r\n    Remember: it's important to keep the list of owners and admins up to date since they control your team's settings.\r\n    Your team is on Slack's Free plan, which is free to use for as long as you want for teams of all sizes. Interested in unlimited archive access and integrations, single sign-on, custom data  (more)...```"
}

Byte Trimming Function from another SO answer:

function cutInUTF8(str, n) {
    var len = Math.min(n, str.length);
    var i, cs, c = 0, bytes = 0;
    for (i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        cs = 1;
        if (c >= 128) cs++;
        if (c >= 2048) cs++;
        if (n < (bytes += cs)) break;
    }
    return str.substr(0, i);
}

Substr based on bytes rather than character count



via Jon Lamb

No comments:

Post a Comment