Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add websms provider (websms.com) #120

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/sachet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/messagebird/sachet/provider/textmagic"
"github.com/messagebird/sachet/provider/turbosms"
"github.com/messagebird/sachet/provider/twilio"
"github.com/messagebird/sachet/provider/websms"
)

type ReceiverConf struct {
Expand Down Expand Up @@ -75,6 +76,7 @@ var config struct {
Ghasedak ghasedak.Config
Sfr sfr.Config
TextMagic textmagic.Config
WebSms websms.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 @@ -42,6 +42,7 @@ import (
"github.com/messagebird/sachet/provider/textmagic"
"github.com/messagebird/sachet/provider/turbosms"
"github.com/messagebird/sachet/provider/twilio"
"github.com/messagebird/sachet/provider/websms"
)

var (
Expand Down Expand Up @@ -147,6 +148,8 @@ func providerByName(name string) (sachet.Provider, error) {
return sfr.NewSfr(config.Providers.Sfr), nil
case "textmagic":
return textmagic.NewTextMagic(config.Providers.TextMagic), nil
case "websms":
return websms.NewWebSms(config.Providers.WebSms), nil
}

return nil, fmt.Errorf("%s: Unknown provider", name)
Expand Down
7 changes: 7 additions & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ providers:
textmagic:
username: 'username'
api_key: 'TEXTMAGIC_API_KEY'
websms:
api_token: 'WEBSMS_API_TOKEN'

templates:
- telegram.tmpl
Expand Down Expand Up @@ -152,3 +154,8 @@ receivers:
to:
- '09012345679'
- '09123456789'

- name: 'websms'
provider: 'websms'
to:
- '09012345679'
14 changes: 14 additions & 0 deletions provider/websms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# websms.com (Link Mobility)

This sachet provider uses the *websms* SMS gateway service (websms.com). This service is popular in Austria and other European countries.

See https://www.websms.com/ and https://developer.websms.com/web-api/


## Configuration

This provieder needs an API key to access the REST API. To create one login to your websms account got to "API administration" and "API access data". There you can create a new API token for use in this provider.

Note: use a dedicate API token for access by sachet. **DONT use tokens created for productive systems and services**.

You can specifiy a list of target phone numbers which will reveive messages. You have to specificy those numbers in E.164/MSISDN format, e.g. "49123456789" (otherwise often written as "+49123456789" or "0049123456789"). See https://en.wikipedia.org/wiki/MSISDN
77 changes: 77 additions & 0 deletions provider/websms/websms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package websms

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

"github.com/messagebird/sachet"
)

const websmsBaseURL = "https://api.websms.com/rest/"

var httpClient = &http.Client{Timeout: 20 * time.Second}

// Config is the configuration struct for websms provider.
type Config struct {
Token string `yaml:"api_token"`
}

var _ (sachet.Provider) = (*WebSms)(nil)

// Websms contains the necessary values for the WebSms provider.
type WebSms struct {
Config
}

// NewWebSms creates and returns a new Websms struct.
func NewWebSms(config Config) *WebSms {
return &WebSms{config}
}

type TextSmsSendRequest struct {
SmsID string `json:"clientMessageId"`
RecipientList []string `json:"recipientAddressList"`
Message string `json:"messageContent"`
}

// Send sends SMS to user registered in configuration.
func (c *WebSms) Send(message sachet.Message) error {

// Prepare SMS request payload
smsReq := TextSmsSendRequest{
RecipientList: message.To,
Message: message.Text,
}

jsonSmsReq, err := json.Marshal(smsReq)
if err != nil {
return err
}

// Create HTTP POST request
request, err := http.NewRequest("POST", websmsBaseURL + "smsmessaging/text", bytes.NewBuffer(jsonSmsReq))
if err != nil {
return err
}

request.Header.Set("Content-Type", "application/json")
request.Header.Set("User-Agent", "Sachet")
request.Header.Set("Authorization", "Bearer " + c.Token)

// Send HTTP request
response, err := httpClient.Do(request)
if err != nil {
return err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return fmt.Errorf("Failed to send sms. statusCode: %d", response.StatusCode)
}

// Messages successfully sent
return nil
}