diff --git a/cmd/sachet/config.go b/cmd/sachet/config.go index 747702af..f14aa437 100644 --- a/cmd/sachet/config.go +++ b/cmd/sachet/config.go @@ -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 { @@ -75,6 +76,7 @@ var config struct { Ghasedak ghasedak.Config Sfr sfr.Config TextMagic textmagic.Config + WebSms websms.Config } Receivers []ReceiverConf diff --git a/cmd/sachet/main.go b/cmd/sachet/main.go index 60d9b9f8..46525c0b 100644 --- a/cmd/sachet/main.go +++ b/cmd/sachet/main.go @@ -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 ( @@ -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) diff --git a/examples/config.yaml b/examples/config.yaml index 903a505f..eee229b7 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -104,6 +104,8 @@ providers: textmagic: username: 'username' api_key: 'TEXTMAGIC_API_KEY' + websms: + api_token: 'WEBSMS_API_TOKEN' templates: - telegram.tmpl @@ -152,3 +154,8 @@ receivers: to: - '09012345679' - '09123456789' + + - name: 'websms' + provider: 'websms' + to: + - '09012345679' diff --git a/provider/websms/README.md b/provider/websms/README.md new file mode 100644 index 00000000..fcb3a29a --- /dev/null +++ b/provider/websms/README.md @@ -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 \ No newline at end of file diff --git a/provider/websms/websms.go b/provider/websms/websms.go new file mode 100644 index 00000000..b6fa8dad --- /dev/null +++ b/provider/websms/websms.go @@ -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 +}