sms-toolkit

A collection of tools to work with SMS messages.


License
MIT
Install
pip install sms-toolkit==1.0.9

Documentation

sms-toolkit

A collection of tools used to send SMS messages.

Test Status

Tools

Message Profiling

Accepts a raw SMS message string and determines its most efficient encoding, then determines how many segments would be used to send it.

Largely based on this tool (code found here).

The segmenting logic for GSM-7 and UCS-2 encoding follows these standards

Here is an example with simple ascii input, which will be profiled as GSM-7 format:

from sms_toolkit.messages.profiling import profile_message
profile_message("Sup chonus")

'''
# Outputs -
{
   "num_segments": 1,
   "segments": [
       {
           "message": "Sup chonus",
           "total_segment_length": 10,
           "unicode_character_list": [
               "S", "u", "p", " ", "c", "h", "o", "n", "u", "s"
           ],
           "byte_groups": [
               [83], [117], [112], [32], [99], [104], [111], [110], [117], [115]
           ]
       }
   ],
   "message_length": 10,
   "max_segment_size": 160
}
'''

Here is an example with non-ascii input, containing characters from BMP (represented as 2 bytes byte-group) and non-BMP (representing as 4 bytes in the byte-group) ranges which will be profiled as UCS-2 format:

from sms_toolkit.messages.profiling import profile_message
profile_message("fuel ⛽ fiⓇe 🔥 emojis 😉")

'''
# Outputs -
{
    'num_segments': 1,
    'segments': [
        {
            'message': 'fuel ⛽ fiⓇe 🔥 emojis 😉',
            'unicode_character_list': [
                'f', 'u', 'e', 'l', ' ', '⛽', ' ', 'f', 'i', 'Ⓡ', 'e', ' ', '🔥', ' ', 'e', 'm', 'o', 'j', 'i', 's', ' ', '😉'
            ],
            'byte_groups': [
                [0, 102], [0, 117], [0, 101], [0, 108], [0, 32], [38, 253], [0, 32], [0, 102], [0, 105], [36, 199], [0, 101], [0, 32], [216, 61, 221, 37], [0, 32], [0, 101], [0, 109], [0, 111], [0, 106], [0, 105], [0, 115], [0, 32], [216, 61, 222, 9]
            ],
            'total_segment_length': 24
        }
    ],
    'message_length': 24,
    'max_segment_size': 70
}
'''

Message Truncating

Accepts a raw SMS message string, and the maximum length and truncates it. This also detects the right encoding and adjusts the character length accordingly.

from sms_toolkit import truncate_message
truncate_message("fuel ⛽ fiⓇe 🔥 emojis 😉", 22)

'''
# Outputs -
'fuel ⛽ fiⓇe 🔥 emojis '
'''

Testing

This library needs is tested against python 2 and 3. Both interpreters need to be available to tox

pyenv versions  # shows all versions available
pyenv local 2.7.* 3.7.*

Run tests:

tox .

Acknowledgements

Klaviyo has adapted the original vobject packaged in to this package. We have only kept the vcard class from the original package, removing calendar functionality, etc.