smpp-otp/internal/infrastructure/smpp.go

58 lines
1.2 KiB
Go
Raw Normal View History

2024-03-28 11:00:22 +00:00
package smpp
import (
"log"
"smpp-otp/internal/config"
"github.com/fiorix/go-smpp/smpp"
"github.com/fiorix/go-smpp/smpp/pdu"
"github.com/fiorix/go-smpp/smpp/pdu/pdutext"
)
type SMPPClient struct {
Transceiver *smpp.Transceiver
}
func NewSMPPClient(cfg *config.Config) (*SMPPClient, error) {
smppCfg := cfg.SMPP
2024-03-28 12:37:22 +00:00
log.Println("SMPP Config:", smppCfg) // This will print the SMPP configuration
2024-03-28 11:00:22 +00:00
tx := &smpp.Transceiver{
Addr: smppCfg.Addr,
User: smppCfg.User,
Passwd: smppCfg.Pass,
Handler: func(p pdu.Body) {},
}
2024-03-28 12:37:22 +00:00
log.Println("Attempting to bind to SMPP server at address:", smppCfg.Addr)
2024-03-28 11:00:22 +00:00
connStatus := tx.Bind()
go func() {
for c := range connStatus {
if c.Status() != smpp.Connected {
log.Println("Failed to connect:", c.Error())
}
}
}()
return &SMPPClient{Transceiver: tx}, nil
}
func (c *SMPPClient) SendSMS(cfg *config.Config, dest, text string) error {
src := cfg.SMPP.Src_Phone_Number
params := &smpp.ShortMessage{
Src: src,
Dst: dest,
Text: pdutext.Raw(text),
Register: 0, // No delivery receipt
}
resp, err := c.Transceiver.Submit(params)
if err != nil {
log.Println("Failed to send SMS:", err)
return err
}
log.Println("SMS sent successfully, response:", resp)
return nil
}