This repo contains a package for SMS and RCS segment calculations. The package is released as a nodeJS package as well as a browser script. Want to see it in action? Try the live Segment Calculator to see how any message is encoded and how many segments it takes.
The package can be installed using:
npm install --save sms-segments-calculatorSample usage:
const { SegmentedMessage, RcsSegmentedMessage } = require('sms-segments-calculator');
const segmentedMessage = new SegmentedMessage('Hello World');
console.log(segmentedMessage.encodingName); // "GSM-7"
console.log(segmentedMessage.segmentsCount); // "1"
const rcsMessage = new RcsSegmentedMessage('Hello RCS', 'us');
console.log(rcsMessage.messageType); // "Rich"
console.log(rcsMessage.segmentsCount); // "1"You can add the library to your page using the CDN file:
<script src="https://cdn.jsdelivr.net/gh/TwilioDevEd/message-segment-calculator/docs/scripts/segmentsCalculator.js" integrity="sha256-e2498331ca98c0d43952423be7ff75944f7f29d201da4ee466bd06e59e8054cb" crossorigin="anonymous"></script>Alternatively you can add the library to your page using the file segmentsCalculator.js provided in docs/scripts/ and adding it to your page:
<script type="text/javascript" src="scripts/segmentsCalculator.js"></script>Once loaded, the browser bundle exposes SegmentedMessage and RcsSegmentedMessage as globals.
An example of usage can be find in docs/index.html
This is the main class exposed by the package
Arguments:
message: Body of the SMSencoding: Optional: encoding. It can beGSM-7,UCS-2,auto. Default value:auto
Returns the name of the calculated encoding for the message: GSM-7 or UCS-2
Total size of the message in bits (including User Data Header if present)
Total size of the message in bits (excluding User Data Header if present)
Number of segment(s)
Note: Multi-segment messages carry 153 characters per segment for GSM-7 and 67 for UCS-2. As an exception, toll-free multi-segment SMS sent to the US or Canada support 152 characters per segment for GSM-7 and 66 for UCS-2. This calculator uses the standard 153/67 limits. See SMS character limits.
Return an array with the non GSM-7 characters in the body. It can be used to replace character and reduce the number of segments
RCS always uses UTF-8. For US destinations, messages are billed per 160 UTF-8 byte βRichβ segment. For international destinations, there is no segmentation: <=160 bytes is billed as Basic, and >160 bytes is billed as Single.
Arguments:
message: Body of the RCS messageregion:usorinternational(default:us)
Always returns "UTF-8".
Number of UTF-8 bytes in the message body.
Total size of the message body in bits.
Number of RCS segment(s) the message is split into for billing.
An array with one entry per segment. Each entry contains index, capacity, and used (bytes).
Returns Rich, Basic, or Single based on the region and UTF-8 length.
The region used for calculation: "us" or "international".
If you want to test the library you can use the script provided in playground/index.js. Install the dependencies (npm install) and then run:
node playground/index.js "π Hello World π"This code is open source and welcomes contributions. All contributions are subject to our Code of Conduct.
The source code for the library is all contained in the src folder. Before submitting a PR:
- Run linter using
npm run lintcommand and make sure there are no linter error - Compile the code using
npm run buildcommand and make sure there are no errors - Execute the test using
npm testand make sure all tests pass - Transpile the code using
npm run webpackand test the web page indocs/index.html
The docs UI follows Paste-aligned accessibility guidelines:
- All color meaning includes a text label (encoding, fill state)
- Stats updates use
aria-live="polite"for screen reader announcements - Inputs include visible focus styles and associated labels
- Segment bars use
role=\"meter\"with value attributes
We used the Kimoby SMS Segment Counter as visual inspiration: https://www.kimoby.com/calculators/sms-segment-counter
This project is provided for educational and estimation purposes only. No warranty expressed or implied. Software is as is.
Segment counts and encodings produced by this library and the browser calculator are estimates and are not a guarantee of how messages will be segmented or billed. Actual segmentation can vary by sender type, destination, and carrier. For a sent SMS, check Twilio Console Messaging Logs for the actual segment count. Refer to your Twilio billing usage and invoices for actual charges.