Skip to content

Commit

Permalink
Merge pull request #49 from dswarbrick/sipgate
Browse files Browse the repository at this point in the history
Add support for Sipgate REST API v2 SMS send
  • Loading branch information
epels authored Sep 21, 2018
2 parents daaaa93 + d4f6b9b commit 2324475
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/sachet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -40,6 +41,7 @@ var config struct {
MediaBurst mediaburst.MediaBurstConfig
FreeMobile freemobile.Config
AspSms aspsms.Config
Sipgate sipgate.Config
}

Receivers []ReceiverConf
Expand Down
3 changes: 3 additions & 0 deletions cmd/sachet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ providers:
aspsms:
username: 'username'
password: 'password'
sipgate:
username: 'username'
password: 'password'

receivers:
- name: 'team-sms'
Expand Down
24 changes: 24 additions & 0 deletions provider/sipgate/README.md
Original file line number Diff line number Diff line change
@@ -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 <username>:<password> 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 `<wNNN>` with your user ID.

```
$ curl -X GET --user <username>:<password> https://api.sipgate.com/v2/<wNNN>/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.
73 changes: 73 additions & 0 deletions provider/sipgate/sipgate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package sipgate

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"

"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"`
}

// 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 {
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 {
params := payload{
SmsID: message.From,
Recipient: recipient,
Message: message.Text,
}

data, err := json.Marshal(params)
if err != nil {
return err
}

request, err := http.NewRequest("POST", sipgateURL, bytes.NewBuffer(data))
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
}

0 comments on commit 2324475

Please sign in to comment.