From 3fbcb0797f7e7e7acdb79fe452fcb6cd0f3059a7 Mon Sep 17 00:00:00 2001 From: Daniel Swarbrick Date: Tue, 18 Sep 2018 14:02:06 +0200 Subject: [PATCH 1/2] Add support for Sipgate REST API v2 SMS send Signed-off-by: Daniel Swarbrick --- cmd/sachet/config.go | 2 + cmd/sachet/main.go | 3 ++ examples/config.yaml | 4 ++ provider/sipgate/README.md | 24 ++++++++++++ provider/sipgate/sipgate.go | 77 +++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 provider/sipgate/README.md create mode 100644 provider/sipgate/sipgate.go diff --git a/cmd/sachet/config.go b/cmd/sachet/config.go index 749482d..ea42200 100644 --- a/cmd/sachet/config.go +++ b/cmd/sachet/config.go @@ -12,6 +12,7 @@ import ( "github.com/messagebird/sachet/provider/messagebird" "github.com/messagebird/sachet/provider/nexmo" "github.com/messagebird/sachet/provider/otc" + "github.com/messagebird/sachet/provider/sipgate" "github.com/messagebird/sachet/provider/telegram" "github.com/messagebird/sachet/provider/turbosms" "github.com/messagebird/sachet/provider/twilio" @@ -40,6 +41,7 @@ var config struct { MediaBurst mediaburst.MediaBurstConfig FreeMobile freemobile.Config AspSms aspsms.Config + Sipgate sipgate.Config } Receivers []ReceiverConf diff --git a/cmd/sachet/main.go b/cmd/sachet/main.go index 432b720..6fac5c7 100644 --- a/cmd/sachet/main.go +++ b/cmd/sachet/main.go @@ -20,6 +20,7 @@ import ( "github.com/messagebird/sachet/provider/messagebird" "github.com/messagebird/sachet/provider/nexmo" "github.com/messagebird/sachet/provider/otc" + "github.com/messagebird/sachet/provider/sipgate" "github.com/messagebird/sachet/provider/telegram" "github.com/messagebird/sachet/provider/turbosms" "github.com/messagebird/sachet/provider/twilio" @@ -163,6 +164,8 @@ func providerByName(name string) (sachet.Provider, error) { return freemobile.NewFreeMobile(config.Providers.FreeMobile), nil case "aspsms": return aspsms.NewAspSms(config.Providers.AspSms), nil + case "sipgate": + return sipgate.NewSipgate(config.Providers.Sipgate), nil } return nil, fmt.Errorf("%s: Unknown provider", name) diff --git a/examples/config.yaml b/examples/config.yaml index 21e8b49..8fcdaec 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -37,6 +37,10 @@ providers: aspsms: username: 'username' password: 'password' + sipgate: + username: 'username' + password: 'password' + url: "https://api.sipgate.com/v2/sessions/sms" receivers: - name: 'team-sms' diff --git a/provider/sipgate/README.md b/provider/sipgate/README.md new file mode 100644 index 0000000..2abd237 --- /dev/null +++ b/provider/sipgate/README.md @@ -0,0 +1,24 @@ +# Sipgate REST API v2 + +To configure the Sipgate provider, you need to specify the SMS ID which will be used to lookup +SMS sender name and number. If you do not know the SMS ID, you can find it out via the Sipgate API. + +First, obtain your user ID: + +``` +$ curl -X GET --user : https://api.sipgate.com/v2/authorization/userinfo +``` + +The relevant user ID is in the form "wNNN" where "NNN" are numerical digits. If your user is not +the main account owner, it may be in the "sub" attribute of the response. + +Next, get a list of SMS IDs, replacing the `` with your user ID. + +``` +$ curl -X GET --user : https://api.sipgate.com/v2//sms +``` + +The response should contain an array of one or more items, with IDs in the form "sNNN", where the +"NNN" are numerical digits (often the same as the "wNNN" user ID). + +Use the "sNNN" SMS ID as the "from" attribute in the "receivers" section of the configuration file. diff --git a/provider/sipgate/sipgate.go b/provider/sipgate/sipgate.go new file mode 100644 index 0000000..cadb9b9 --- /dev/null +++ b/provider/sipgate/sipgate.go @@ -0,0 +1,77 @@ +package sipgate + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "time" + + "github.com/messagebird/sachet" +) + +var sipgateHTTPClient = &http.Client{Timeout: time.Second * 20} + +// Config is the configuration struct for Sipgate provider +type Config struct { + Username string `yaml:"username"` + Password string `yaml:"password"` + URL string `yaml:"url"` +} + +// Sipgate contains the necessary values for the Sipgate provider +type Sipgate struct { + Config +} + +// NewSipgate creates and returns a new Sipgate struct +func NewSipgate(config Config) *Sipgate { + if config.URL == "" { + config.URL = "https://api.sipgate.com/v2/sessions/sms" + } + return &Sipgate{config} +} + +type payload struct { + SmsID string `json:"smsId"` + Recipient string `json:"recipient"` + Message string `json:"message"` +} + +// Send sends SMS to user registered in configuration +func (c *Sipgate) Send(message sachet.Message) error { + for _, recipient := range message.To { + var body bytes.Buffer + + params := payload{ + SmsID: message.From, + Recipient: recipient, + Message: message.Text, + } + + enc := json.NewEncoder(&body) + if err := enc.Encode(params); err != nil { + return err + } + + request, err := http.NewRequest("POST", c.URL, &body) + if err != nil { + return err + } + + request.SetBasicAuth(c.Username, c.Password) + request.Header.Set("Content-Type", "application/json") + request.Header.Set("User-Agent", "Sachet") + + response, err := sipgateHTTPClient.Do(request) + if err != nil { + return err + } + + if response.StatusCode != http.StatusNoContent { + return fmt.Errorf("Failed sending sms. statusCode: %d", response.StatusCode) + } + } + + return nil +} From d4f6b9b42b7d6a39be8edcecf909a867586542db Mon Sep 17 00:00:00 2001 From: Daniel Swarbrick Date: Tue, 18 Sep 2018 21:25:01 +0200 Subject: [PATCH 2/2] Address review comments in PR #49 Signed-off-by: Daniel Swarbrick --- examples/config.yaml | 1 - provider/sipgate/sipgate.go | 14 +++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/examples/config.yaml b/examples/config.yaml index 8fcdaec..5b323b4 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -40,7 +40,6 @@ providers: sipgate: username: 'username' password: 'password' - url: "https://api.sipgate.com/v2/sessions/sms" receivers: - name: 'team-sms' diff --git a/provider/sipgate/sipgate.go b/provider/sipgate/sipgate.go index cadb9b9..c58304a 100644 --- a/provider/sipgate/sipgate.go +++ b/provider/sipgate/sipgate.go @@ -10,13 +10,14 @@ import ( "github.com/messagebird/sachet" ) +const sipgateURL = "https://api.sipgate.com/v2/sessions/sms" + var sipgateHTTPClient = &http.Client{Timeout: time.Second * 20} // Config is the configuration struct for Sipgate provider type Config struct { Username string `yaml:"username"` Password string `yaml:"password"` - URL string `yaml:"url"` } // Sipgate contains the necessary values for the Sipgate provider @@ -26,9 +27,6 @@ type Sipgate struct { // NewSipgate creates and returns a new Sipgate struct func NewSipgate(config Config) *Sipgate { - if config.URL == "" { - config.URL = "https://api.sipgate.com/v2/sessions/sms" - } return &Sipgate{config} } @@ -41,20 +39,18 @@ type payload struct { // Send sends SMS to user registered in configuration func (c *Sipgate) Send(message sachet.Message) error { for _, recipient := range message.To { - var body bytes.Buffer - params := payload{ SmsID: message.From, Recipient: recipient, Message: message.Text, } - enc := json.NewEncoder(&body) - if err := enc.Encode(params); err != nil { + data, err := json.Marshal(params) + if err != nil { return err } - request, err := http.NewRequest("POST", c.URL, &body) + request, err := http.NewRequest("POST", sipgateURL, bytes.NewBuffer(data)) if err != nil { return err }