package main
import (
"context"
"fmt"
"github.com/sujit-baniya/go-smpp"
"github.com/sujit-baniya/go-smpp/coding"
"github.com/sujit-baniya/go-smpp/pdu"
"math/rand"
"net"
"strings"
"time"
"unicode"
"unicode/utf8"
)
func main() {
connect()
handshake(smppVersion)
rs := send()
fmt.Println(rs)
wait()
}
var (
n = time.Duration(10)
conn = &smpp.Conn{}
smppVersion = pdu.SMPPVersion50
)
func connect() {
parent, err := net.Dial("tcp", "smscsim.melroselabs.com:2775")
if err != nil {
panic(err)
}
conn = smpp.NewConn(context.Background(), parent)
conn.WriteTimeout = n * time.Second // set write timeout (optional, default 15 minutes)
conn.ReadTimeout = n * time.Second // set read timeout (optional, default 15 minutes)
go conn.Watch() // start watchdog
}
func handshake(smppVersion pdu.InterfaceVersion) {
resp, err := conn.Submit(context.Background(), &pdu.BindTransceiver{
SystemID: "444320",
Password: "5691c2",
SystemType: "",
Version: smppVersion,
})
if err != nil {
panic(err)
}
r := resp.(*pdu.BindTransceiverResp)
if r.Header.CommandStatus == 0 {
// start keep-alive
go conn.EnquireLink(time.Minute, time.Minute)
}
}
type Response interface {}
func send() []Response {
msg := "Hello World! Hello World!Hello World! HelHello World! Hello World!Hello World! HelHello World! Hello World!Hello World! HelHello World! Hello World!Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! "
shortMessages, err := compose(msg)
if err != nil {
panic(err)
}
var responses []Response
ctx := context.Background()
for key, shortMessage := range shortMessages {
fmt.Println(key)
packet := &pdu.SubmitSM{
SourceAddr: parseSrcPhone("00919821"),
DestAddr: parseDestPhone("99919821"),
ESMClass: pdu.ESMClass{UDHIndicator: true},
RegisteredDelivery: pdu.RegisteredDelivery{
MCDeliveryReceipt: 1,
SMEOriginatedAcknowledgment: 1,
IntermediateNotification: true,
Reserved: 7,
},
Message: shortMessage,
}
resp, err := conn.Submit(ctx, packet)
if err != nil {
panic(err)
}
fmt.Println(resp)
// responses = append(responses, resp)
}
return responses
}
func wait() {
for {
packet := <-conn.PDU()
switch pd := packet.(type) {
case *pdu.DeliverSM:
fmt.Println(pd)
case pdu.Responsable:
fmt.Println(pd)
err := conn.Send(pd.Resp())
if err != nil {
fmt.Println(err)
}
}
}
}
func compose(msg string) ([]pdu.ShortMessage, error) {
reference := uint16(rand.Intn(0xFFFF))
var dataCoding coding.DataCoding
if smppVersion == pdu.SMPPVersion50 {
dataCoding = coding.BestCoding(msg)
} else {
dataCoding = coding.BestSafeCoding(msg)
}
return pdu.ComposeMultipartShortMessage(msg, dataCoding, reference)
}
func parseSrcPhone(phone string) pdu.Address {
if strings.HasPrefix(phone, "+") {
return pdu.Address{TON: 1, NPI: 1, No: phone}
}
if utf8.RuneCountInString(phone) <= 5 {
return pdu.Address{TON: 3, NPI: 0, No: phone}
}
if IsLetter(phone) {
return pdu.Address{TON: 5, NPI: 0, No: phone}
}
return pdu.Address{TON: 1, NPI: 1, No: phone}
}
func parseDestPhone(phone string) pdu.Address {
if strings.HasPrefix(phone, "+") {
return pdu.Address{TON: 1, NPI: 1, No: phone}
}
return pdu.Address{TON: 0, NPI: 1, No: phone}
}
func IsLetter(s string) bool {
for _, r := range s {
if !unicode.IsLetter(r) {
return false
}
}
return true
}
With above setup, it only send first short message and other messages are stuck on line resp, err := conn.Submit(ctx, packet)
.