gosms

package module
v0.0.0-...-2fbe398 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2019 License: MIT Imports: 10 Imported by: 0

README

gosms

This library is for SMS splitting in go.

Features:

  • Smart message splitting
    • Splitting is performed around spaces or after punctuation so that messages remain coherent if concatenation fails at the client.
  • Support for 1 or 2 byte reference numbers in user data headers
  • Easily extensible character encoding
    • Comes with support for GSM and UTF-16 character encodings
    • Encodings can be added by implementing the Encoder interface

Usage Example

package main

import (
    "fmt"
    "github.com/textnow/gosms"
)

func main() {
    var (
        from         = "from"
        to           = []string{"to"}
        message      = "This message should be split depending on the placement of spaces and " +
                       "punctuation. If the client fails to stitch the message segments back " +
                       "together, the user should still be able to read this text."
        messageBytes = 55
    )

	splitter := gosms.NewSplitter()
	splitter.SetMessageBytes(messageBytes)

    SMSs, _ := splitter.Split(from, to, message)

    for idx, sms := range SMSs {
        fmt.Printf("SMS #%d\n", idx + 1)
        fmt.Printf("from    : %s\n", sms.GetFrom())
        fmt.Printf("to      : %s\n", sms.GetTo())
        fmt.Printf("content : \"%s\"\n", sms.GetContent())
        fmt.Println()
    }
}

Output

SMS #1
from    : from
to      : to
content : "This message should be split depending on the placement "

SMS #2
from    : from
to      : to
content : "of spaces and punctuation. If the client fails to stitch"

SMS #3
from    : from
to      : to
content : " the message segments back together, the user should "

SMS #4
from    : from
to      : to
content : "still be able to read this text."

Documentation

Index

Constants

View Source
const (
	// EncoderNameGSM is the GSM Encoder Name
	EncoderNameGSM string = "GSM"

	// EncoderNameUTF16 is the UTF-16 Encoder Name
	EncoderNameUTF16 string = "UTF-16"
)
View Source
const (
	// DefaultSMSBytes is the default SMS size in bytes
	DefaultSMSBytes int = 140
)

Variables

View Source
var ErrNotEncodable = errors.New("one or more characters cannot be encoded with the given encoder")

ErrNotEncodable indicates that the supplied string or character cannot be encoded with the given encoder

View Source
var ErrNotSplittable = errors.New("the message cannot be split with the given encoder and message length")

ErrNotSplittable indicates that the given message cannot be split with the given encoder and message length

Functions

func SplitMessage

func SplitMessage(message []rune, encoder Encoder, messageLength int) ([]string, error)

SplitMessage splits a message into parts with a maximum length of messageLength code points. Word splitting is avoided.

Types

type Encoder

type Encoder interface {
	GetEncoderName() string
	GetCodePointBits() int
	GetCodePoints(rune) (int, error)
	CheckEncodability(string) bool
}

Encoder encapsulates encoder specific fields

func NewGSM

func NewGSM() Encoder

NewGSM returns a new gsm

func NewUTF16

func NewUTF16() Encoder

NewUTF16 returns a new UTF16

type GSM

type GSM struct{}

GSM implements the Encoder interface

func (*GSM) CheckEncodability

func (s *GSM) CheckEncodability(str string) bool

CheckEncodability returns true if str is encodable and false otherwise

func (*GSM) GetCodePointBits

func (s *GSM) GetCodePointBits() int

GetCodePointBits returns the number of bits that make a single GSM code point

func (*GSM) GetCodePoints

func (s *GSM) GetCodePoints(char rune) (int, error)

GetCodePoints returns the number of code points used to represent char in GSM

func (*GSM) GetEncoderName

func (s *GSM) GetEncoderName() string

GetEncoderName returns the GSM encoder name

type SMS

type SMS struct {
	// contains filtered or unexported fields
}

SMS structure with correctly sized message and appropriate UDH

func (*SMS) GetContent

func (s *SMS) GetContent() string

GetContent returns the SMS's content field

func (*SMS) GetFrom

func (s *SMS) GetFrom() string

GetFrom returns the SMS's from field

func (*SMS) GetTo

func (s *SMS) GetTo() string

GetTo returns the SMS's to field

func (*SMS) GetUDH

func (s *SMS) GetUDH() string

GetUDH returns the SMS's udh field

type Splitter

type Splitter struct {
	// contains filtered or unexported fields
}

Splitter splits messages into SMS structs

func NewSplitter

func NewSplitter() *Splitter

NewSplitter creates a new Splitter configured with default values

func (*Splitter) CheckEncodability

func (s *Splitter) CheckEncodability(message string) bool

CheckEncodability returns true if the message is encodable with the splitter's encoder and false otherwise

func (*Splitter) SetEncoder

func (s *Splitter) SetEncoder(encoder Encoder)

SetEncoder sets the encoder of the Splitter

func (*Splitter) SetMessageBytes

func (s *Splitter) SetMessageBytes(messageBytes int)

SetMessageBytes sets the messageBytes of the Splitter

func (*Splitter) SetShortReference

func (s *Splitter) SetShortReference(shortReference bool)

SetShortReference sets the shortReference of the Splitter

func (*Splitter) Split

func (s *Splitter) Split(from string, to []string, message string) ([]SMS, error)

Split generates SMSs with sizable message parts and appropriate UDHs

type UTF16

type UTF16 struct{}

UTF16 implements the Encoder interface

func (*UTF16) CheckEncodability

func (s *UTF16) CheckEncodability(str string) bool

CheckEncodability returns true if str is encodable and false otherwise

func (*UTF16) GetCodePointBits

func (s *UTF16) GetCodePointBits() int

GetCodePointBits returns the number of bits that make a single UTF-16 code point

func (*UTF16) GetCodePoints

func (s *UTF16) GetCodePoints(char rune) (int, error)

GetCodePoints returns the number of code points used to represent char in UTF-16

func (*UTF16) GetEncoderName

func (s *UTF16) GetEncoderName() string

GetEncoderName returns the UTF-16 encoder name

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL