-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from dswarbrick/sipgate
Add support for Sipgate REST API v2 SMS send
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |